class MyJohnDeereApi::Client

Constants

DEFAULTS

Attributes

api_key[R]
api_secret[R]
contribution_definition_id[RW]
http_retry_options[R]
token_hash[R]

Public Class Methods

new(api_key, api_secret, options = {}) click to toggle source

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

# 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

Public Instance Methods

accessor() click to toggle source

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

# 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_uri() click to toggle source

Returns the URI for the Contribution Definiton ID, if provided

# 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_products() click to toggle source

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

# 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) click to toggle source

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

# 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) click to toggle source

generic user-specific GET request method that returns JSON

# File lib/my_john_deere_api/client.rb, line 81
def get resource
  response = accessor.get(resource, headers: headers)

  JSON.parse(response.body)
end
organizations() click to toggle source

organizations associated with this access

# 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) click to toggle source

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

# 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) click to toggle source

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

# 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
site() click to toggle source

Returns the base url for requests

# File lib/my_john_deere_api/client.rb, line 73
def site
  return @site if defined?(@site)
  @site = accessor.client.site
end

Private Instance Methods

headers() click to toggle source
# File lib/my_john_deere_api/client.rb, line 152
def headers
  @headers ||= {accept: 'application/vnd.deere.axiom.v3+json'}
end
oauth_client() click to toggle source

Returns an oAuth client which can be used to build requests

# File lib/my_john_deere_api/client.rb, line 147
def oauth_client
  return @oauth_client if defined?(@oauth_client)
  @oauth_client = MyJohnDeereApi::Consumer.new(@api_key, @api_secret, environment: environment).platform_client
end
post_headers() click to toggle source
# File lib/my_john_deere_api/client.rb, line 156
def post_headers
  @post_headers ||= headers.merge({'Content-Type' => 'application/vnd.deere.axiom.v3+json'})
end