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 credentials are optional, but user-specific API requests are only possible if they are supplied.

options:

:environment

:sandbox or :live

:contribution_definition_id

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

:access

an array with two elements, the access_token and the access_secret of the given user



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

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

  @api_key = api_key
  @api_secret = api_secret

  if options.has_key?(:access) && options[:access].is_a?(Array)
    @access_token, @access_secret = options[:access]
  end

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

Instance Attribute Details

#access_secretObject (readonly)

Returns the value of attribute access_secret.



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

def access_secret
  @access_secret
end

#access_tokenObject (readonly)

Returns the value of attribute access_token.



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

def access_token
  @access_token
end

#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

Instance Method Details

#accessorObject

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



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

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

  @accessor = NetHttpRetry::Decorator.new(
    OAuth::AccessToken.new(
      consumer.user_get,
      access_token,
      access_secret
    ),
    http_retry_options
  )
end

#contribution_productsObject

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



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

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



105
106
107
108
109
110
111
112
113
114
115
# File 'lib/my_john_deere_api/client.rb', line 105

def delete resource
  resource = resource.to_s
  resource = "/#{resource}" unless resource =~ /^\//
  response = accessor.delete(resource, 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



64
65
66
67
68
69
70
# File 'lib/my_john_deere_api/client.rb', line 64

def get resource
  resource = resource.to_s
  resource = "/#{resource}" unless resource =~ /^\//
  response = accessor.get(resource, headers)

  JSON.parse(response.body)
end

#organizationsObject

organizations associated with this access



120
121
122
123
# File 'lib/my_john_deere_api/client.rb', line 120

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



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/my_john_deere_api/client.rb', line 75

def post resource, body
  resource = resource.to_s
  resource = "/#{resource}" unless resource =~ /^\//
  response = accessor.post(resource, camelize(body).to_json, 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



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

def put resource, body
  resource = resource.to_s
  resource = "/#{resource}" unless resource =~ /^\//
  response = accessor.put(resource, camelize(body).to_json, post_headers)

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