Sha256: 210fbfda730fe0b5f051087e1c42bdb21c18fabedf7b5d481a97620ec3d795d9

Contents?: true

Size: 1.25 KB

Versions: 4

Compression:

Stored size: 1.25 KB

Contents

require "rest-client"
require "discountnetwork/response"
require "discountnetwork/configuration"

module DiscountNetwork
  class Client
    attr_reader :http_method, :end_point, :attributes

    def initialize(http_method, end_point, attributes = {})
      @http_method = http_method
      @end_point = end_point
      @attributes = attributes
    end

    def execute
      Response.new(execute_api_request).parse
    end

    private

    def execute_api_request
      RestClient::Request.execute(
        method: http_method,
        url: api_end_point,
        payload: attributes,
        headers: custom_headers
      )
    end

    def api_end_point
      [DiscountNetwork.configuration.api_host, end_point].join("/")
    end

    def custom_headers
      {
        "DN-API-KEY" => DiscountNetwork.configuration.api_key,
        "Authorization" =>
        "Token token=\"#{DiscountNetwork.configuration.auth_token}\""
      }
    end
  end

  def self.get_resource(end_point, attributes = {})
    Client.new(:get, end_point, attributes).execute
  end

  def self.post_resource(end_point, attributes)
    Client.new(:post, end_point, attributes).execute
  end

  def self.put_resource(end_point, attributes)
    Client.new(:put, end_point, attributes).execute
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
discountnetwork-0.1.3 lib/discountnetwork/client.rb
discountnetwork-0.1.2 lib/discountnetwork/client.rb
discountnetwork-0.1.1 lib/discountnetwork/client.rb
discountnetwork-0.1.0 lib/discountnetwork/client.rb