Class: MyJohnDeereApi::Consumer

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

Constant Summary collapse

URLS =

valid API urls

{
  sandbox: 'https://sandboxapi.deere.com',
  live: 'https://partnerapi.deere.com',
}
DEFAULTS =
{
  environment: :live
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Consumer.



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

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

  @api_key = api_key
  @api_secret = api_secret

  self.environment = options[:environment]
  @site = options[:site] || URLS[@environment]
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



6
7
8
# File 'lib/my_john_deere_api/consumer.rb', line 6

def api_key
  @api_key
end

#api_secretObject (readonly)

Returns the value of attribute api_secret.



6
7
8
# File 'lib/my_john_deere_api/consumer.rb', line 6

def api_secret
  @api_secret
end

#environmentObject (readonly)

Returns the value of attribute environment.



6
7
8
# File 'lib/my_john_deere_api/consumer.rb', line 6

def environment
  @environment
end

#siteObject (readonly)

Returns the value of attribute site.



6
7
8
# File 'lib/my_john_deere_api/consumer.rb', line 6

def site
  @site
end

Instance Method Details

#auth_clientObject

oAuth client for user authentication



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/my_john_deere_api/consumer.rb', line 46

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

  # We build this without the `client` method because the authorization links
  # require an extra API call to JD that is only needed for authorization.

  @auth_client = OAuth2::Client.new(
    api_key,
    api_secret,
    site: site,
    authorize_url: authorization_links[:authorization],
    token_url: authorization_links[:token],
    raise_errors: false,
  )
end

#platform_clientObject

oAuth client for platform requests



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/my_john_deere_api/consumer.rb', line 31

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

  @platform_client = OAuth2::Client.new(
    api_key,
    api_secret,
    site: site,
    headers: headers,
    raise_errors: false,
  )
end