Sha256: 638f88cd5d88d584b5c9e3409a0522360c465e5d6e6c0cd14a7be74974bc6cf9

Contents?: true

Size: 1.57 KB

Versions: 2

Compression:

Stored size: 1.57 KB

Contents

require "httparty"

module LinkshareAPI
  # For implementation details please visit
  # https://rakutenlinkshare.zendesk.com/hc/en-us/articles/201483905-Merchandiser-Query-Tool-API-Guidelines
  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 https://rakutenlinkshare.zendesk.com/hc/en-us/articles/200992487-What-is-a-Web-Services-Token-Feed-Token- for details."
        )
      end

      self.class.default_timeout @api_timeout
    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
        )
      rescue Timeout::Error, Net::OpenTimeout
        raise ConnectionError.new("Timeout error (#{api_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

2 entries across 2 versions & 1 rubygems

Version Path
linkshare_api-0.3.2 lib/linkshare_api/product_search.rb
linkshare_api-0.3.1 lib/linkshare_api/product_search.rb