Sha256: 16fe2e7408ab0fdcbd9adfa463a34bae34abe823b412c258b8c893baa4d2ff84

Contents?: true

Size: 1.39 KB

Versions: 3

Compression:

Stored size: 1.39 KB

Contents

require "whiplash/app/api_config"
require "whiplash/app/caching"
require "whiplash/app/connections"
require "whiplash/app/finder_methods"
require "whiplash/app/signing"
require "whiplash/app/version"
require "oauth2"
require "faraday_middleware"

module Whiplash

  module App
    class << self
      include Whiplash::App::ApiConfig
      include Whiplash::App::Caching
      include Whiplash::App::Connections
      include Whiplash::App::FinderMethods
      include Whiplash::App::Signing

      attr_accessor :customer_id, :shop_id

      def client
        OAuth2::Client.new(ENV["WHIPLASH_CLIENT_ID"], ENV["WHIPLASH_CLIENT_SECRET"], site: api_url)
      end

      def connection(version = "api/v2")
        out = Faraday.new [api_url, version].join("/") do |conn|
          conn.request :oauth2, 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

      def refresh_token!
        oauth_token = client.client_credentials.get_token(scope: ENV["WHIPLASH_CLIENT_SCOPE"])
        new_token = oauth_token.token
        cache_store["whiplash_api_token"] = new_token
      end

      def token
        refresh_token! unless cache_store["whiplash_api_token"]
        return cache_store["whiplash_api_token"]
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
whiplash-app-0.3.5 lib/whiplash/app.rb
whiplash-app-0.3.4 lib/whiplash/app.rb
whiplash-app-0.3.3 lib/whiplash/app.rb