Class: MyJohnDeereApi::Authorize
- Inherits:
-
Object
- Object
- MyJohnDeereApi::Authorize
- Includes:
- Helpers::EnvironmentHelper
- Defined in:
- lib/my_john_deere_api/authorize.rb
Constant Summary collapse
- DEFAULTS =
{ environment: :live }
Instance Attribute Summary collapse
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
-
#api_secret ⇒ Object
readonly
Returns the value of attribute api_secret.
-
#environment ⇒ Object
readonly
Returns the value of attribute environment.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#token_hash ⇒ Object
readonly
Returns the value of attribute token_hash.
Instance Method Summary collapse
-
#authorize_url ⇒ Object
Url which may be used to obtain a verification code from the oauth server.
-
#initialize(api_key, api_secret, options = {}) ⇒ Authorize
constructor
Create an Authorize object.
-
#oauth_client ⇒ Object
API client that makes authentication requests.
-
#refresh_from_hash(old_token_hash) ⇒ Object
Use an old token hash to generate a new token hash.
-
#verify(code) ⇒ Object
Turn a verification code into access token.
Constructor Details
#initialize(api_key, api_secret, options = {}) ⇒ Authorize
Create an Authorize object.
This is used to obtain authentication an access key/secret on behalf of a user.
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/my_john_deere_api/authorize.rb', line 17 def initialize(api_key, api_secret, = {}) @options = DEFAULTS.merge() @api_key = api_key @api_secret = api_secret self.environment = @options[:environment] # This is only set upon verification @token_hash = nil end |
Instance Attribute Details
#api_key ⇒ Object (readonly)
Returns the value of attribute api_key.
5 6 7 |
# File 'lib/my_john_deere_api/authorize.rb', line 5 def api_key @api_key end |
#api_secret ⇒ Object (readonly)
Returns the value of attribute api_secret.
5 6 7 |
# File 'lib/my_john_deere_api/authorize.rb', line 5 def api_secret @api_secret end |
#environment ⇒ Object (readonly)
Returns the value of attribute environment.
5 6 7 |
# File 'lib/my_john_deere_api/authorize.rb', line 5 def environment @environment end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
5 6 7 |
# File 'lib/my_john_deere_api/authorize.rb', line 5 def @options end |
#token_hash ⇒ Object (readonly)
Returns the value of attribute token_hash.
5 6 7 |
# File 'lib/my_john_deere_api/authorize.rb', line 5 def token_hash @token_hash end |
Instance Method Details
#authorize_url ⇒ Object
Url which may be used to obtain a verification code from the oauth server.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/my_john_deere_api/authorize.rb', line 32 def return @authorize_url if defined?(@authorize_url) = .slice(:redirect_uri, :state, :scope) if .key?(:scopes) [:scopes] << 'offline_access' unless [:scopes].include?('offline_access') [:scope] = [:scopes].join(' ') end # generate a default unique-ish "state" key if not provided unless .key?(:state) [:state] = (rand(8000) + 1000).to_s end @authorize_url = oauth_client.auth_code.() end |
#oauth_client ⇒ Object
API client that makes authentication requests
53 54 55 56 |
# File 'lib/my_john_deere_api/authorize.rb', line 53 def oauth_client return @oauth_client if defined?(@oauth_client) @oauth_client = MyJohnDeereApi::Consumer.new(@api_key, @api_secret, environment: environment).auth_client end |
#refresh_from_hash(old_token_hash) ⇒ Object
Use an old token hash to generate a new token hash.
71 72 73 74 75 76 |
# File 'lib/my_john_deere_api/authorize.rb', line 71 def refresh_from_hash(old_token_hash) old_token = OAuth2::AccessToken.from_hash(oauth_client, old_token_hash) new_token = old_token.refresh! new_token.to_hash end |
#verify(code) ⇒ Object
Turn a verification code into access token.
61 62 63 64 65 66 |
# File 'lib/my_john_deere_api/authorize.rb', line 61 def verify(code) token = oauth_client.auth_code.get_token(code, redirect_uri: [:redirect_uri]) # normalize hash @token_hash = JSON.parse(token.to_hash.to_json) end |