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.



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

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

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

Instance Attribute Details

#access_secretObject (readonly)

Returns the value of attribute access_secret.



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

def access_secret
  @access_secret
end

#access_tokenObject (readonly)

Returns the value of attribute access_token.



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

def access_token
  @access_token
end

#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

#request_secretObject (readonly)

Returns the value of attribute request_secret.



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

def request_secret
  @request_secret
end

#request_tokenObject (readonly)

Returns the value of attribute request_token.



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

def request_token
  @request_token
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
# File 'lib/my_john_deere_api/authorize.rb', line 32

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

  request_options = options.slice(:oauth_callback)

  requester = consumer.get_request_token(request_options)
  @request_token = requester.token
  @request_secret = requester.secret

  @authorize_url = requester.authorize_url(request_options)
end

#consumerObject

API consumer that makes non-user-specific GET requests



47
48
49
50
# File 'lib/my_john_deere_api/authorize.rb', line 47

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

#verify(code, token = nil, secret = nil) ⇒ Object

Turn a verification code into access tokens. If this is run from a separate process than the one that created the initial RequestToken, the request token/secret can be passed in.



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

def verify(code, token=nil, secret=nil)
  token ||= request_token
  secret ||= request_secret

  requester = OAuth::RequestToken.new(consumer, token, secret)
  access_object = requester.get_access_token(oauth_verifier: code)
  @access_token = access_object.token
  @access_secret = access_object.secret
  nil
end