Class: Whiplash::App
- Inherits:
-
Object
- Object
- Whiplash::App
- Extended by:
- Signing
- Includes:
- ApiConfig, Caching, Connections, FinderMethods
- Defined in:
- lib/whiplash/app.rb,
lib/whiplash/app/caching.rb,
lib/whiplash/app/signing.rb,
lib/whiplash/app/version.rb,
lib/whiplash/app/api_config.rb,
lib/whiplash/app/connections.rb,
lib/whiplash/app/finder_methods.rb
Defined Under Namespace
Modules: ApiConfig, Caching, Connections, FinderMethods, Signing
Constant Summary collapse
- VERSION =
"0.6.1"
Instance Attribute Summary collapse
-
#customer_id ⇒ Object
Returns the value of attribute customer_id.
-
#shop_id ⇒ Object
Returns the value of attribute shop_id.
-
#token ⇒ Object
Returns the value of attribute token.
Class Method Summary collapse
Instance Method Summary collapse
- #client ⇒ Object
- #connection ⇒ Object
-
#initialize(token = nil, options = {}) ⇒ App
constructor
A new instance of App.
- #refresh_token! ⇒ Object
- #token_expired? ⇒ Boolean
Methods included from Signing
Methods included from FinderMethods
#count, #create, #destroy, #find, #find_all, #update
Methods included from Connections
#app_request, #app_request!, #base_app_request, #delete, #delete!, #error_codes, #error_response, #get, #get!, #get_body_or_empty, #get_context, #post, #post!, #put, #put!, #sanitize_headers, #select_error, #store_whiplash_error!
Methods included from Caching
Methods included from ApiConfig
#api_url, #production_url, #rate_limit, #testing_url
Constructor Details
#initialize(token = nil, options = {}) ⇒ App
Returns a new instance of App.
21 22 23 24 25 26 27 |
# File 'lib/whiplash/app.rb', line 21 def initialize(token=nil, ={}) opts = .with_indifferent_access token ||= cache_store["whiplash_api_token"] @token = format_token(token) unless token.nil? @customer_id = [:customer_id] @shop_id = [:shop_id] end |
Instance Attribute Details
#customer_id ⇒ Object
Returns the value of attribute customer_id.
19 20 21 |
# File 'lib/whiplash/app.rb', line 19 def customer_id @customer_id end |
#shop_id ⇒ Object
Returns the value of attribute shop_id.
19 20 21 |
# File 'lib/whiplash/app.rb', line 19 def shop_id @shop_id end |
#token ⇒ Object
Returns the value of attribute token.
19 20 21 |
# File 'lib/whiplash/app.rb', line 19 def token @token end |
Class Method Details
.whiplash_api_token ⇒ Object
29 30 31 32 33 |
# File 'lib/whiplash/app.rb', line 29 def self.whiplash_api_token store = Moneta.new(:Redis, host: ENV["REDIS_HOST"], port: ENV["REDIS_PORT"], password: ENV["REDIS_PASSWORD"], expires: 7200) cache_store = Moneta::Namespace.new store, Whiplash::App::Caching.namespace_value cache_store["whiplash_api_token"] end |
Instance Method Details
#client ⇒ Object
35 36 37 |
# File 'lib/whiplash/app.rb', line 35 def client OAuth2::Client.new(ENV["WHIPLASH_CLIENT_ID"], ENV["WHIPLASH_CLIENT_SECRET"], site: api_url) end |
#connection ⇒ Object
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/whiplash/app.rb', line 39 def connection out = Faraday.new [api_url, "api/v2"].join("/") do |conn| conn.request :oauth2, token.token, token_type: "bearer" conn.request :json conn.response :json, :content_type => /\bjson$/ conn.use :instrumentation conn.adapter Faraday.default_adapter end return out end |
#refresh_token! ⇒ Object
54 55 56 57 58 59 60 61 62 63 |
# File 'lib/whiplash/app.rb', line 54 def refresh_token! if token.blank? # If we're storing locally, grab a new token and cache it access_token = client.client_credentials.get_token(scope: ENV["WHIPLASH_CLIENT_SCOPE"]) new_token = access_token.to_hash cache_store["whiplash_api_token"] = new_token else access_token = token.refresh! end self.token = access_token end |
#token_expired? ⇒ Boolean
65 66 67 68 69 70 |
# File 'lib/whiplash/app.rb', line 65 def token_expired? return token.expired? unless token.blank? return true unless cache_store.key?("whiplash_api_token") return true if cache_store["whiplash_api_token"].blank? false end |