Class: MicrosoftTranslator::AzureAuthentication

Inherits:
Object
  • Object
show all
Defined in:
lib/microsoft_translator/azure_authentication.rb

Constant Summary collapse

AUTH_URL =
'https://datamarket.accesscontrol.windows.net/v2/OAuth2-13'
API_SCOPE =
'http://api.microsofttranslator.com'
GRANT_TYPE =
'client_credentials'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client_id, client_secret) ⇒ AzureAuthentication

Returns a new instance of AzureAuthentication.



10
11
12
13
14
# File 'lib/microsoft_translator/azure_authentication.rb', line 10

def initialize(client_id, client_secret)
  @client_id = client_id
  @client_secret = client_secret
  renew_token
end

Instance Attribute Details

#tokenObject (readonly)

Returns the value of attribute token.



7
8
9
# File 'lib/microsoft_translator/azure_authentication.rb', line 7

def token
  @token
end

#token_expires_atObject (readonly)

Returns the value of attribute token_expires_at.



8
9
10
# File 'lib/microsoft_translator/azure_authentication.rb', line 8

def token_expires_at
  @token_expires_at
end

Instance Method Details

#current_tokenObject



16
17
18
19
20
21
# File 'lib/microsoft_translator/azure_authentication.rb', line 16

def current_token
  if @token_expires_at < Time.now
    renew_token
  end
  @token
end

#renew_tokenObject



24
25
26
27
28
29
# File 'lib/microsoft_translator/azure_authentication.rb', line 24

def renew_token
   auth_response = RestClient.post(AUTH_URL, auth_params)
   parsed_json = JSON.parse(auth_response.body)
   @token_expires_at = Time.now + parsed_json['expires_in'].to_i
   @token = parsed_json['access_token']
end