Sha256: 1c09aa0d9a6f7175d72c3589d53d03c78fff4f4675685aa0ad01bf6e6307e2cc

Contents?: true

Size: 855 Bytes

Versions: 3

Compression:

Stored size: 855 Bytes

Contents

module Workarea
  module Orderbot
    module Authentication
      def token
        response = get_token

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

      private

      def get_token
        Rails.cache.fetch(token_cache_key, expires_in: 5.minutes) do
          conn = Faraday.new(url: rest_endpoint)
          conn.basic_auth(api_user_name, api_password)
          conn.get do |req|
            req.url '/accesstoken'
            req.headers['Content-Type'] = 'application/json'
          end
        end
      end

      def api_user_name
        options[:api_user_name]
      end

      def api_password
        options[:api_password]
      end

      def test
        options[:test]
      end

      def token_cache_key
        Digest::MD5.hexdigest "#{api_user_name}#{api_password}#{test}"
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
workarea-orderbot-1.0.2 lib/workarea/orderbot/authentication.rb
workarea-orderbot-1.0.1 lib/workarea/orderbot/authentication.rb
workarea-orderbot-1.0.0 lib/workarea/orderbot/authentication.rb