lib/ebay/shopping.rb in ebay-ruby-0.2.0 vs lib/ebay/shopping.rb in ebay-ruby-0.3.0

- old
+ new

@@ -1,10 +1,167 @@ -require 'ebay/request' +# frozen_string_literal: true +require 'http' + +require 'ebay/config' +require 'ebay/sandboxable' + module Ebay - class Shopping < Request - host 'open.api.ebay.com' - path '/shopping' - headers 'X-EBAY-API-APP-ID' => Config.app_id, - 'X-EBAY-API-VERSION' => 799 + # The eBay Shopping API makes it easy to search for things on eBay. + # + # @see https://developer.ebay.com/Devzone/shopping/docs/Concepts/ShoppingAPI_FormatOverview.html + # @see https://developer.ebay.com/Devzone/shopping/docs/CallRef/index.html + class Shopping + include Sandboxable + + SANDBOX_ENDPOINT = 'https://open.api.sandbox.ebay.com/shopping' + PRODUCTION_ENDPOINT = 'https://open.api.ebay.com/shopping' + + # @return [String] + attr_reader :app_id + + # @return [String, nil] + attr_reader :response_encoding + + # @return [String, nil] + attr_reader :site_id + + # @return [String] + attr_reader :version + + # @return [String, nil] + attr_reader :version_handling + + # @return [String, nil] + attr_reader :tracking_id + + # @return [String, nil] + attr_reader :tracking_partner_code + + # @return [String, nil] + attr_reader :affiliate_user_id + + # Returns a Finding API request instance + # + # @param [String] app_id + # @param [String] response_encoding + # @param [String] site_id + # @param [String] version + # @param [String] version_handling + # @param [String] tracking_id + # @param [String] tracking_partner_code + # @param [String] affiliate_user_id + def initialize(app_id: Config.app_id, response_encoding: 'JSON', + site_id: nil, version: '1119', + version_handling: nil, tracking_id: nil, + tracking_partner_code: nil, affiliate_user_id: nil) + @app_id = app_id + @response_encoding = response_encoding + @site_id = site_id + @version = version + @version_handling = version_handling + @tracking_id = tracking_id + @tracking_partner_code = tracking_partner_code + @affiliate_user_id = affiliate_user_id + end + + # Returns one or more eBay catalog products based on a query string or + # product ID value + # + # @param [Hash] payload + # @return [HTTP::Response] + def find_products(payload = {}) + request('FindProducts', payload) + end + + # Retrieves high-level data for a specified eBay category + # + # @param [String] category_id + # @param [Hash] payload + # @return [HTTP::Response] + def get_category_info(category_id, payload = {}) + payload.update('CategoryID' => category_id) + request('GetCategoryInfo', payload) + end + + # Gets the official eBay system time in GMT + # + # @param [Hash] payload + # @return [HTTP::Response] + def get_ebay_time(payload = {}) + request('GeteBayTime', payload) + end + + # Retrieves the current status of up to 20 eBay listings + # + # @overload get_item_status(*item_ids, payload = {}) + # @param [String] item_ids + # @param [Hash] payload + # @return [HTTP::Response] + def get_item_status(*item_ids) + payload = item_ids.last.is_a?(Hash) ? item_ids.pop : {} + payload.update('ItemID' => item_ids.join(',')) + request('GetItemStatus', payload) + end + + # Retrieves publicly available data for one or more listings + # + # @overload get_multiple_items(*item_ids, payload = {}) + # @param [String] item_ids + # @param [Hash] payload + # @return [HTTP::Response] + def get_multiple_items(*item_ids) + payload = item_ids.last.is_a?(Hash) ? item_ids.pop : {} + payload.update('ItemID' => item_ids.join(',')) + + request('GetMultipleItems', payload) + end + + # Gets shipping costs for a listing + # + # @param [String] item_id + # @param [Hash] payload + # @return [HTTP::Response] + def get_shipping_costs(item_id, payload = {}) + payload.update('ItemID' => item_id) + request('GetShippingCosts', payload) + end + + # Gets publicly visible details about one listing + # + # @param [String] item_id + # @param [Hash] payload + # @return [HTTP::Response] + def get_single_item(item_id, payload = {}) + payload.update('ItemID' => item_id) + request('GetSingleItem', payload) + end + + # Retrieves user information + # + # @param [String] user_id + # @param [Hash] payload + # @return [HTTP::Response] + def get_user_profile(user_id, payload = {}) + payload.update('UserID' => user_id) + request('GetUserProfile', payload) + end + + private + + def request(operation, payload = {}) + endpoint = sandbox? ? SANDBOX_ENDPOINT : PRODUCTION_ENDPOINT + params = { 'appid' => app_id, + 'callname' => operation, + 'requestencoding' => 'JSON', + 'responseencoding' => response_encoding, + 'siteid' => site_id, + 'version' => version, + 'versionhandling' => version_handling, + 'affiliateuserid' => affiliate_user_id, + 'trackingid' => tracking_id, + 'trackingpartnercode' => tracking_partner_code }.compact + + HTTP.post(endpoint, params: params, body: JSON.dump(payload)) + end end end