Class: MyJohnDeereApi::Client
- Inherits:
-
Object
- Object
- MyJohnDeereApi::Client
- Defined in:
- lib/my_john_deere_api/client.rb
Constant Summary collapse
- DEFAULTS =
{ environment: :live, http_retry: {} }
Instance Attribute Summary collapse
-
#access_secret ⇒ Object
readonly
Returns the value of attribute access_secret.
-
#access_token ⇒ Object
readonly
Returns the value of attribute access_token.
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
-
#api_secret ⇒ Object
readonly
Returns the value of attribute api_secret.
-
#contribution_definition_id ⇒ Object
Returns the value of attribute contribution_definition_id.
-
#http_retry_options ⇒ Object
readonly
Returns the value of attribute http_retry_options.
Attributes included from Helpers::EnvironmentHelper
Instance Method Summary collapse
-
#accessor ⇒ Object
Returns an oAuth AccessToken object which can be used to make user-specific API requests.
-
#contribution_products ⇒ Object
contribution products associated with this app (not user-specific).
-
#delete(resource) ⇒ Object
generic user-specific DELETE request method that returns JSON or response.
-
#get(resource) ⇒ Object
generic user-specific GET request method that returns JSON.
-
#initialize(api_key, api_secret, options = {}) ⇒ Client
constructor
Creates the client with everything it needs to perform API requests.
-
#organizations ⇒ Object
organizations associated with this access.
-
#post(resource, body) ⇒ Object
generic user-specific POST request method that returns JSON or response.
-
#put(resource, body) ⇒ Object
generic user-specific PUT request method that returns JSON or response.
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, = {}) = DEFAULTS.merge() @api_key = api_key @api_secret = api_secret if .has_key?(:access) && [:access].is_a?(Array) @access_token, @access_secret = [:access] end self.environment = [:environment] @contribution_definition_id = [:contribution_definition_id] @http_retry_options = [:http_retry] end |
Instance Attribute Details
#access_secret ⇒ Object (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_token ⇒ Object (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_key ⇒ Object (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_secret ⇒ Object (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_id ⇒ Object
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_options ⇒ Object (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 end |
Instance Method Details
#accessor ⇒ Object
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 ), ) end |
#contribution_products ⇒ Object
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 |
#organizations ⇒ Object
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 |