class MsParaphrase::TokenManager
Constants
- MAX_TRANSLATIONS
Attributes
token[RW]
token_expiration[RW]
Public Class Methods
new()
click to toggle source
# File lib/ms_paraphrase.rb, line 39 def initialize self.token = nil end
Public Instance Methods
get_token()
click to toggle source
# File lib/ms_paraphrase.rb, line 43 def get_token if token.nil? || !is_token_valid?(token) self.token = JSON.parse(RestClient.post MsParaphrase.configuration.token_api, {:client_id => MsParaphrase.configuration.client_id, :client_secret => MsParaphrase.configuration.client_secret, :scope => MsParaphrase.configuration.scope, :grant_type => MsParaphrase.configuration.grant_type}) if is_credentials_provided? elsif is_token_valid?(token) return token end end
Private Instance Methods
is_credentials_provided?()
click to toggle source
# File lib/ms_paraphrase.rb, line 56 def is_credentials_provided? if MsParaphrase.configuration.token_api.nil? raise TranslateApiException.new('No token API endpoint provided.') elsif MsParaphrase.configuration.client_id.nil? raise TranslateApiException.new('No client ID was provided.') elsif MsParaphrase.configuration.client_secret.nil? raise TranslateApiException.new('No client secret was provided.') elsif MsParaphrase.configuration.scope.nil? raise TranslateApiException.new('No scope was provided.') elsif MsParaphrase.configuration.grant_type.nil? raise TranslateApiException.new('No grant type provided.') else return true end end
is_token_valid?(token)
click to toggle source
# File lib/ms_paraphrase.rb, line 73 def is_token_valid?(token) token['expires_in'].to_i < 10 ? false : true end