Sha256: baf82769345090f51a4845d487c4191d3b61a7443a95a79d693700f0d2b0dc87

Contents?: true

Size: 1.36 KB

Versions: 1

Compression:

Stored size: 1.36 KB

Contents

# frozen_string_literal: true

require 'http'

require 'ebay/config'
require 'ebay/sandboxable'

module Ebay
  module Oauth
    # Mints an access token to use in API requests
    #
    # @see https://developer.ebay.com/api-docs/static/oauth-client-credentials-grant.html
    class ClientCredentialsGrant
      include Sandboxable

      SANDBOX_ENDPOINT = 'https://api.sandbox.ebay.com/identity/v1/oauth2/token'
      PRODUCTION_ENDPOINT = 'https://api.ebay.com/identity/v1/oauth2/token'

      # @return [String]
      attr_reader :app_id

      # @return [String]
      attr_reader :cert_id

      # @param [String] app_id
      # @param [String] cert_id
      def initialize(app_id: Config.app_id, cert_id: Config.cert_id)
        @app_id = app_id
        @cert_id = cert_id
      end

      # Mints a new access token
      #
      # @return [String]
      def mint_access_token
        JSON.parse(request).fetch('access_token')
      end

      # Requests a client credentials grant
      #
      # @return [HTTP::Response]
      def request
        HTTP.basic_auth(user: app_id, pass: cert_id)
            .post(url, form: payload)
      end

      private

      def url
        sandbox? ? SANDBOX_ENDPOINT : PRODUCTION_ENDPOINT
      end

      def payload
        { grant_type: 'client_credentials',
          scope: 'https://api.ebay.com/oauth/api_scope' }
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ebay-ruby-0.3.0 lib/ebay/oauth/client_credentials_grant.rb