Class: SpreePluggto::Api::Oauth
- Inherits:
-
Object
- Object
- SpreePluggto::Api::Oauth
- Defined in:
- app/services/spree_pluggto/api/oauth.rb
Instance Attribute Summary collapse
-
#settings ⇒ Object
Returns the value of attribute settings.
Instance Method Summary collapse
- #get_token ⇒ Object
-
#initialize ⇒ Oauth
constructor
A new instance of Oauth.
- #refresh_token! ⇒ Object
- #valid_token? ⇒ Boolean
Constructor Details
#initialize ⇒ Oauth
Returns a new instance of Oauth.
5 6 7 8 |
# File 'app/services/spree_pluggto/api/oauth.rb', line 5 def initialize @settings = PluggtoSettings.first_or_create refresh_token! unless valid_token? end |
Instance Attribute Details
#settings ⇒ Object
Returns the value of attribute settings.
3 4 5 |
# File 'app/services/spree_pluggto/api/oauth.rb', line 3 def settings @settings end |
Instance Method Details
#get_token ⇒ Object
10 11 12 |
# File 'app/services/spree_pluggto/api/oauth.rb', line 10 def get_token settings.access_token end |
#refresh_token! ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'app/services/spree_pluggto/api/oauth.rb', line 18 def refresh_token! grant_type = settings.refresh_token.present? ? "refresh_token" : "password" body = { client_id: settings.client_id, client_secret: settings.client_secret, username: settings.username, password: settings.password, refresh_token: settings.refresh_token, grant_type: grant_type } response = oauth_request.post("/oauth/token", body) settings.update( access_token: response.body['access_token'], refresh_token: response.body['refresh_token'], token_expires_at: response.body['expires_in'].to_i.seconds.from_now ) if response.success? @settings = settings end |
#valid_token? ⇒ Boolean
14 15 16 |
# File 'app/services/spree_pluggto/api/oauth.rb', line 14 def valid_token? settings.token_expires_at.present? && settings.token_expires_at > (DateTime.now + 5.minutes) end |