Class: MyJohnDeereApi::Client

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

Constant Summary collapse

DEFAULTS =
{
  environment: :live,
  http_retry: {}
}

Instance Attribute Summary collapse

Attributes included from Helpers::EnvironmentHelper

#environment

Instance Method Summary collapse

Constructor Details

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

Creates the client with everything it needs to perform API requests. User-specific token_hash is optional, but user-specific API requests are only possible if it is supplied.

options:

:environment

:sandbox or :live

:contribution_definition_id

optional, but needed for some requests like asset create/update

:token_hash

a hash used to re-create the access token



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

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

  @api_key = api_key
  @api_secret = api_secret

  if options.has_key?(:token_hash) && options[:token_hash].is_a?(Hash)
    @token_hash = options[:token_hash]
  end

  self.environment = options[:environment]
  @contribution_definition_id = options[:contribution_definition_id]
  @http_retry_options = options[:http_retry]
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



7
8
9
# File 'lib/my_john_deere_api/client.rb', line 7

def api_key
  @api_key
end

#api_secretObject (readonly)

Returns the value of attribute api_secret.



7
8
9
# File 'lib/my_john_deere_api/client.rb', line 7

def api_secret
  @api_secret
end

#contribution_definition_idObject

Returns the value of attribute contribution_definition_id.



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

def contribution_definition_id
  @contribution_definition_id
end

#http_retry_optionsObject (readonly)

Returns the value of attribute http_retry_options.



7
8
9
# File 'lib/my_john_deere_api/client.rb', line 7

def http_retry_options
  @http_retry_options
end

#token_hashObject (readonly)

Returns the value of attribute token_hash.



7
8
9
# File 'lib/my_john_deere_api/client.rb', line 7

def token_hash
  @token_hash
end

Instance Method Details

#accessorObject

Returns an oAuth AccessToken object which can be used to make user-specific API requests



47
48
49
50
51
52
53
54
# File 'lib/my_john_deere_api/client.rb', line 47

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

  @accessor = NetHttpRetry::Decorator.new(
    OAuth2::AccessToken.from_hash(oauth_client, token_hash),
    http_retry_options
  )
end

#contribution_definition_uriObject

Returns the URI for the Contribution Definiton ID, if provided



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

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

  @contribution_definition_uri =
    if contribution_definition_id
      "#{site}/contributionDefinitions/#{contribution_definition_id}"
    else
      nil
    end
end

#contribution_productsObject

contribution products associated with this app (not user-specific)



137
138
139
140
# File 'lib/my_john_deere_api/client.rb', line 137

def contribution_products
  return @contribution_products if defined?(@contribution_products)
  @contribution_products = MyJohnDeereApi::Request::Collection::ContributionProducts.new(self)
end

#delete(resource) ⇒ Object

generic user-specific DELETE request method that returns JSON or response



116
117
118
119
120
121
122
123
124
# File 'lib/my_john_deere_api/client.rb', line 116

def delete resource
  response = accessor.delete(resource, headers: headers)

  if response.body && response.body.size > 0
    JSON.parse(response.body)
  else
    response
  end
end

#get(resource) ⇒ Object

generic user-specific GET request method that returns JSON



81
82
83
84
85
# File 'lib/my_john_deere_api/client.rb', line 81

def get resource
  response = accessor.get(resource, headers: headers)

  JSON.parse(response.body)
end

#organizationsObject

organizations associated with this access



129
130
131
132
# File 'lib/my_john_deere_api/client.rb', line 129

def organizations
  return @organizations if defined?(@organizations)
  @organizations = MyJohnDeereApi::Request::Collection::Organizations.new(self)
end

#post(resource, body) ⇒ Object

generic user-specific POST request method that returns JSON or response



90
91
92
93
94
95
96
97
98
# File 'lib/my_john_deere_api/client.rb', line 90

def post resource, body
  response = accessor.post(resource, body: camelize(body).to_json, headers: post_headers)

  if response.body && response.body.size > 0
    JSON.parse(response.body)
  else
    response
  end
end

#put(resource, body) ⇒ Object

generic user-specific PUT request method that returns JSON or response



103
104
105
106
107
108
109
110
111
# File 'lib/my_john_deere_api/client.rb', line 103

def put resource, body
  response = accessor.put(resource, body: camelize(body).to_json, headers: post_headers)

  if response.body && response.body.size > 0
    JSON.parse(response.body)
  else
    response
  end
end

#siteObject

Returns the base url for requests



73
74
75
76
# File 'lib/my_john_deere_api/client.rb', line 73

def site
  return @site if defined?(@site)
  @site = accessor.client.site
end