Sha256: f7909add03274e9c21816bfd150605cef23b5237cb7cd23625e7ae4c0d1c1c97

Contents?: true

Size: 1.49 KB

Versions: 1

Compression:

Stored size: 1.49 KB

Contents

require "addressable/uri"
require "httparty"

module LinkshareAPI
  # For implementation details please visit
  # http://helpcenter.linkshare.com/publisher/questions.php?questionid=652
  class ProductSearch
    include HTTParty

    attr_reader :api_base_url, :api_timeout, :keyword, :token

    def initialize
      @token        = LinkshareAPI.token
      @api_base_url = LinkshareAPI::WEB_SERVICE_URIS[:product_search]
      @api_timeout  = LinkshareAPI.api_timeout

      if @token.nil?
        raise AuthenticationError.new(
          "No token. Set your token by using 'LinkshareAPI.token = <TOKEN>'. " +
          "You can retrieve your token from LinkhShare's Web Services page under the Links tab. " +
          "See http://helpcenter.linkshare.com/publisher/questions.php?questionid=648 for details."
        )
      end
    end

    def query(params)
      raise ArgumentError, "Hash expected, got #{params.class} instead" unless params.is_a?(Hash)

      params.merge!(token: token)
      begin
        response = self.class.get(
          api_base_url,
          query: params,
          timeout: api_timeout
        )
      rescue Timeout::Error
        raise ConnectionError.new("Timeout error (#{timeout}s)")
      end

      if response.code != 200
        raise Error.new(response.message, response.code)
      end
      error = response["result"]["Errors"]
      raise InvalidRequestError.new(error["ErrorText"], error["ErrorID"].to_i) if error

      Response.new(response, :product_search)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
linkshare_api-0.2.0 lib/linkshare_api/product_search.rb