Class: MyJohnDeere::AccessToken
- Inherits:
-
Object
- Object
- MyJohnDeere::AccessToken
- Defined in:
- lib/myjohndeere/access_token.rb
Instance Attribute Summary collapse
-
#oauth_access_token ⇒ Object
Returns the value of attribute oauth_access_token.
Class Method Summary collapse
-
.get_request_token ⇒ Object
Use this if you need to get the verifier code.
- .oauth_consumer(options = {}) ⇒ Object
Instance Method Summary collapse
- #execute_request(method, path, options = {}) ⇒ Object
-
#initialize(options = {}) ⇒ AccessToken
constructor
A new instance of AccessToken.
- #secret ⇒ Object
- #token ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ AccessToken
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/myjohndeere/access_token.rb', line 26 def initialize( = {}) = [:request_token_token, :request_token_secret, :verifier_code] = [:oauth_access_token_token, :oauth_access_token_secret] if .all? { |i| .key?(i) } then request_token = OAuth::RequestToken.from_hash(self.class.oauth_consumer, { oauth_token: [:token], oauth_token_secret: [:token_secret] }) self.oauth_access_token = request_token.get_access_token(oauth_verifier: [:verifier_code]) elsif .all? { |i| .key?(i) } then self.oauth_access_token = OAuth::AccessToken.from_hash( self.class.oauth_consumer, { oauth_token: [:oauth_access_token_token], oauth_token_secret: [:oauth_access_token_secret] } ) else raise ArgumentError.new("You must specify either request token options [#{request_token_options.join(',')}] or [#{oauth_token_options.join(',')}]") end end |
Instance Attribute Details
#oauth_access_token ⇒ Object
Returns the value of attribute oauth_access_token.
3 4 5 |
# File 'lib/myjohndeere/access_token.rb', line 3 def oauth_access_token @oauth_access_token end |
Class Method Details
.get_request_token ⇒ Object
Use this if you need to get the verifier code. You’ll need to use this along with the request_token.authorize_url to have the user sign in and provide you with a verifier code. Makes a request to get the request_token.
18 19 20 21 22 23 24 |
# File 'lib/myjohndeere/access_token.rb', line 18 def self.get_request_token() consumer = self.oauth_consumer( authorize_url: MyJohnDeere::AUTHORIZE_URL, http_method: :get ) return consumer.get_request_token({}) end |
.oauth_consumer(options = {}) ⇒ Object
5 6 7 8 9 10 11 12 13 |
# File 'lib/myjohndeere/access_token.rb', line 5 def self.oauth_consumer( = {}) OAuth::Consumer.new( MyJohnDeere.configuration.app_id, MyJohnDeere.configuration.shared_secret, .merge( site: MyJohnDeere.configuration.endpoint, header: {Accept: MyJohnDeere::JSON_CONTENT_HEADER_VALUE} )) end |
Instance Method Details
#execute_request(method, path, options = {}) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/myjohndeere/access_token.rb', line 56 def execute_request(method, path, = {}) path, headers, body = Util.build_path_headers_and_body(method, path, headers: [:headers], body: [:body], etag: [:etag]) response = nil MyJohnDeere.logger.debug("Sending request with body: #{body}\n headers: #{headers}") if REQUEST_METHODS_TO_PUT_PARAMS_IN_URL.include?(method) response = self.oauth_access_token.send(method, path, headers) else # permit the body through since it'll be in the response = self.oauth_access_token.send(method, path, body, headers) end MyJohnDeere.logger.info("JohnDeere token response: #{response.body}") Util.handle_response_error_codes(response) return Response.new(response) end |
#secret ⇒ Object
52 53 54 |
# File 'lib/myjohndeere/access_token.rb', line 52 def secret return oauth_access_token.nil? ? nil : oauth_access_token.secret end |
#token ⇒ Object
48 49 50 |
# File 'lib/myjohndeere/access_token.rb', line 48 def token return oauth_access_token.nil? ? nil : oauth_access_token.token end |