Sha256: 83683c7705018ebe05051bf9a9b09dbaf8f5764fe67d1ea81fc8a16e183eaa28

Contents?: true

Size: 1.71 KB

Versions: 3

Compression:

Stored size: 1.71 KB

Contents

# frozen_string_literal: true

require 'http'

module Ebay
  # Adds an HTTP client and ability to switch to the eBay Sandbox environment
  module Requestable
    class << self
      private

      def included(base)
        class << base
          attr_accessor :endpoint
        end
      end
    end

    # @return [HTTP::Client]
    attr_writer :http

    # @!attribute [r] headers
    # @return [Hash]
    attr_accessor :headers

    # Sets the eBay Market
    #
    # @param [String]
    def market_id=(market_id)
      @headers ||= {}
      @headers['X-EBAY-SOA-GLOBAL-ID'] = market_id
    end

    # @!attribute [r] http
    # @return [HTTP::Client]
    def http
      @http ||= HTTP::Client.new
    end

    # @!attribute [r] endpoint
    # @return [String]
    def endpoint
      @endpoint ||= self.class.endpoint
    end

    # Switches to the eBay Sandbox environment
    #
    # @return [self]
    def sandbox
      @endpoint = endpoint.sub('ebay', 'sandbox.ebay')
      self
    end

    # Flags request as persistent
    #
    # @param [Integer] timeout
    # @return [self]
    def persistent(timeout: 5)
      self.http = http.persistent(endpoint, timeout: timeout)
      self
    end

    # @!method use(*features)
    #   Turns on {https://github.com/httprb/http HTTP} features
    #
    #   @param features
    #   @return [self]
    #
    # @!method via(*proxy)
    #   Makes a request through an HTTP proxy
    #
    #   @param [Array] proxy
    #   @raise [HTTP::Request::Error] if HTTP proxy is invalid
    #   @return [self]
    %i[timeout via through use].each do |method_name|
      define_method(method_name) do |*args, &block|
        self.http = http.send(method_name, *args, &block)
        self
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ebay-ruby-0.4.1 lib/ebay/requestable.rb
ebay-ruby-0.4.0 lib/ebay/requestable.rb
ebay-ruby-0.3.5 lib/ebay/requestable.rb