Sha256: b7f8fe97260ab3ed61f60555d63818c585be2b341ce70b80a441d1de16935bab

Contents?: true

Size: 1.47 KB

Versions: 4

Compression:

Stored size: 1.47 KB

Contents

module Workarea
  module Listrak
    class DataApi
      def initialize(client_id:, client_secret:, open_timeout: nil, read_timeout: nil)
        @client_id = client_id
        @client_secret = client_secret
        @open_timeout = open_timeout
        @read_timeout = read_timeout
      end

      def request(http_request)
        token = Listrak::Oauth.token(client_id: client_id, client_secret: client_secret)

        http_request.add_field 'Authorization', "Bearer #{token}"
        http_request.add_field 'Content-Type', 'application/json'
        http_request['Accept'] = 'application/json'

        response = http.start do |conn|
          conn.request http_request
        end

        case response
        when ::Net::HTTPSuccess
          response
        else
          raise Listrak::HttpError, response
        end
      end

      def customers
        Customers.new self
      end

      def orders
        Orders.new self
      end

      def products
        Products.new self
      end

      private

        attr_reader :client_id, :client_secret, :open_timeout, :read_timeout

        def http
          Net::HTTP.new(uri.host, uri.port).tap do |conn|
            conn.use_ssl = true
            conn.read_timeout = read_timeout if read_timeout
            conn.open_timeout = open_timeout if open_timeout
          end
        end

        def base_url
          "https://api.listrak.com/email/"
        end

        def uri
          URI base_url
        end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
workarea-listrak-5.0.4 app/services/workarea/listrak/data_api.rb
workarea-listrak-5.0.3 app/services/workarea/listrak/data_api.rb
workarea-listrak-5.0.2 app/services/workarea/listrak/data_api.rb
workarea-listrak-5.0.1 app/services/workarea/listrak/data_api.rb