Class: MyJohnDeereApi::Authorize

Inherits:
Object
  • Object
show all
Includes:
Helpers::EnvironmentHelper
Defined in:
lib/my_john_deere_api/authorize.rb

Constant Summary collapse

DEFAULTS =
{
  environment: :live
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key, api_secret, options = {}) ⇒ Authorize

Create an Authorize object.

This is used to obtain authentication an access key/secret on behalf of a user.



17
18
19
20
21
22
23
24
25
26
# File 'lib/my_john_deere_api/authorize.rb', line 17

def initialize(api_key, api_secret, options = {})
  @options = DEFAULTS.merge(options)

  @api_key = api_key
  @api_secret = api_secret
  self.environment = @options[:environment]

  # This is only set upon verification
  @token_hash = nil
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



5
6
7
# File 'lib/my_john_deere_api/authorize.rb', line 5

def api_key
  @api_key
end

#api_secretObject (readonly)

Returns the value of attribute api_secret.



5
6
7
# File 'lib/my_john_deere_api/authorize.rb', line 5

def api_secret
  @api_secret
end

#environmentObject (readonly)

Returns the value of attribute environment.



5
6
7
# File 'lib/my_john_deere_api/authorize.rb', line 5

def environment
  @environment
end

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/my_john_deere_api/authorize.rb', line 5

def options
  @options
end

#token_hashObject (readonly)

Returns the value of attribute token_hash.



5
6
7
# File 'lib/my_john_deere_api/authorize.rb', line 5

def token_hash
  @token_hash
end

Instance Method Details

#authorize_urlObject

Url which may be used to obtain a verification code from the oauth server.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/my_john_deere_api/authorize.rb', line 32

def authorize_url
  return @authorize_url if defined?(@authorize_url)

  request_options = options.slice(:redirect_uri, :state, :scope)

  if options.key?(:scopes)
    options[:scopes] << 'offline_access' unless options[:scopes].include?('offline_access')
    request_options[:scope] = options[:scopes].join(' ')
  end

  # generate a default unique-ish "state" key if not provided
  unless request_options.key?(:state)
    request_options[:state] = (rand(8000) + 1000).to_s
  end

  @authorize_url = oauth_client.auth_code.authorize_url(request_options)
end

#oauth_clientObject

API client that makes authentication requests



53
54
55
56
# File 'lib/my_john_deere_api/authorize.rb', line 53

def oauth_client
  return @oauth_client if defined?(@oauth_client)
  @oauth_client = MyJohnDeereApi::Consumer.new(@api_key, @api_secret, environment: environment).auth_client
end

#refresh_from_hash(old_token_hash) ⇒ Object

Use an old token hash to generate a new token hash.



71
72
73
74
75
76
# File 'lib/my_john_deere_api/authorize.rb', line 71

def refresh_from_hash(old_token_hash)
  old_token = OAuth2::AccessToken.from_hash(oauth_client, old_token_hash)
  new_token = old_token.refresh!

  new_token.to_hash
end

#verify(code) ⇒ Object

Turn a verification code into access token.



61
62
63
64
65
66
# File 'lib/my_john_deere_api/authorize.rb', line 61

def verify(code)
  token = oauth_client.auth_code.get_token(code, redirect_uri: options[:redirect_uri])

  # normalize hash
  @token_hash = JSON.parse(token.to_hash.to_json)
end