Sha256: f789b18dbb92d8254b3318ac6d4028cf98f1f2323d95886911a005a71a69bcf0

Contents?: true

Size: 1.07 KB

Versions: 7

Compression:

Stored size: 1.07 KB

Contents

module Workarea
  module Sezzle
    module Authentication
      class AuthenticationError < StandardError; end
      def token
        response = get_token
        body = JSON.parse(response.body)

        raise AuthenticationError, response.body.to_s unless response.success?
        body['token']
      end

      private

      def get_token
        Rails.cache.fetch(token_cache_key, expires_in: 5.minutes) do

          body = {
            public_key: api_public_key,
            private_key: api_private_key
          }

          conn = Faraday.new(url: rest_endpoint)
          conn.post do |req|
            req.url 'v2/authentication'
            req.headers['Content-Type'] = 'application/json'
            req.body = body.to_json
          end
        end
      end

      def api_public_key
        options[:api_public_key]
      end

      def api_private_key
        options[:api_private_key]
      end

      def test
        options[:test]
      end

      def token_cache_key
        Digest::MD5.hexdigest "#{api_public_key}#{api_private_key}#{test}"
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
workarea-sezzle-1.0.6 lib/workarea/sezzle/authentication.rb
workarea-sezzle-1.0.5 lib/workarea/sezzle/authentication.rb
workarea-sezzle-1.0.4 lib/workarea/sezzle/authentication.rb
workarea-sezzle-1.0.3 lib/workarea/sezzle/authentication.rb
workarea-sezzle-1.0.2 lib/workarea/sezzle/authentication.rb
workarea-sezzle-1.0.1 lib/workarea/sezzle/authentication.rb
workarea-sezzle-1.0.0 lib/workarea/sezzle/authentication.rb