Class: MyJohnDeere::AccessToken

Inherits:
Object
  • Object
show all
Defined in:
lib/myjohndeere/access_token.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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(options = {})
  request_token_options = [:request_token_token, :request_token_secret, :verifier_code]
  oauth_token_options = [:oauth_access_token_token, :oauth_access_token_secret]
  if request_token_options.all? { |i| options.key?(i) } then
    request_token = OAuth::RequestToken.from_hash(self.class.oauth_consumer, {
      oauth_token: options[:token],
      oauth_token_secret: options[:token_secret]
    })
    self.oauth_access_token = request_token.get_access_token(oauth_verifier: options[:verifier_code])
  elsif oauth_token_options.all? { |i| options.key?(i) } then
    self.oauth_access_token = OAuth::AccessToken.from_hash(
      self.class.oauth_consumer, 
      {
        oauth_token: options[:oauth_access_token_token],
        oauth_token_secret: options[: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_tokenObject

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_tokenObject

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(options = {})
  OAuth::Consumer.new(
    MyJohnDeere.configuration.app_id, 
    MyJohnDeere.configuration.shared_secret,
    options.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, options = {})
  path, headers, body = Util.build_path_headers_and_body(method, path,
    headers: options[:headers],
    body: options[:body],
    etag: options[: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

#secretObject



52
53
54
# File 'lib/myjohndeere/access_token.rb', line 52

def secret
  return oauth_access_token.nil? ? nil : oauth_access_token.secret
end

#tokenObject



48
49
50
# File 'lib/myjohndeere/access_token.rb', line 48

def token
  return oauth_access_token.nil? ? nil : oauth_access_token.token
end