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
}

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



28
29
30
31
32
33
34
35
36
37
38
39
40
# 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?(:access) && options[:access].is_a?(Array)
    @access_token, @access_secret = options[:access]
  end

  self.environment = options[:environment]
  @contribution_definition_id = options[:contribution_definition_id]
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

Instance Method Details

#accessorObject

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



46
47
48
49
# File 'lib/my_john_deere_api/client.rb', line 46

def accessor
  return @accessor if defined?(@accessor)
  @accessor = OAuth::AccessToken.new(consumer.user_get, access_token, access_secret)
end

#contribution_productsObject

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



118
119
120
121
# File 'lib/my_john_deere_api/client.rb', line 118

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



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/my_john_deere_api/client.rb', line 95

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



54
55
56
57
58
59
60
# File 'lib/my_john_deere_api/client.rb', line 54

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



110
111
112
113
# File 'lib/my_john_deere_api/client.rb', line 110

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



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/my_john_deere_api/client.rb', line 65

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



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/my_john_deere_api/client.rb', line 80

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