Sha256: cffe64acfcbf1925a372ca9d21982b576f11b4492eeb8298e9429f4f392e86c4

Contents?: true

Size: 962 Bytes

Versions: 2

Compression:

Stored size: 962 Bytes

Contents

module Workarea
  module Yotpo
    module Authentication
      class NoTokenError < StandardError; end

      def token
        response = get_token
        raise NoTokenError unless response.present? && response.success?

        body = JSON.parse(response.body)
        body['access_token']
      end

      private

        def get_token
          conn = Faraday.new(url: auth_endpoint)
          conn.post do |req|
            req.url '/oauth/token', token_params
            req.headers['Content-Type'] = 'application/json'
          end
        end

        def token_params
          {
            client_id: client_id,
            client_secret: client_secret,
            grant_type: 'client_credentials'
          }
        end

        def auth_endpoint
          "https://api.yotpo.com"
        end

        def client_id
          options[:app_key]
        end

        def client_secret
          options[:secret_key]
        end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
workarea-yotpo-1.0.1 lib/workarea/yotpo/authentication.rb
workarea-yotpo-1.0.0 lib/workarea/yotpo/authentication.rb