class BestBuy::Request
Represents an unfulfilled request to the BestBuy
API. Fullfillment of the request can be triggered by invoking Request#call
. Request
also provides methods to inspect the request that will be made to the BestBuy
API
Constants
- VALID_ENDPOINTS
Endpoints that are supported by this API client
Public Class Methods
@param api_key The API key required by the BestBuy
API @param endpoint The endpoint of the API that this request will be made against. Must be one of VALID_ENDPOINTS
@param
# File lib/bestbuy/request.rb, line 16
def initialize(api_key:, endpoint:, affiliate_tracking_id: nil, filters: [])
unless VALID_ENDPOINTS.include? endpoint
fail APIError, "The endpoint \"#{endpoint}\" is currently unsupported. Supported endpoints are: #{VALID_ENDPOINTS.join(", ")}"
end
@endpoint = endpoint
@filters = filters
@affiliate_tracking_id = affiliate_tracking_id
@api_key = api_key
@show_params = []
end
Public Instance Methods
Synchronously executes a request to the BestBuy
API. This will block until results are ready and have been parsed
@return Array<Hash> The results returned from the API.
# File lib/bestbuy/request.rb, line 55 def call resp = ::Net::HTTP.get URI(to_s) ::MultiJson.decode(resp)['products'] end
Sets the request to only return the specified fields from the API
@example Pluck attributes
bby = BestBuy::Client.new(api_key: '12345') bby.products(upc: '004815162342').pluck(:name).call #=> [{:name => "MegaProduct 5000"}]
@param requested_fields [Array<Symbol>] The requested fields that should be included in product results @return BestBuy::Request
The augmented request
# File lib/bestbuy/request.rb, line 47 def pluck(*requested_fields) @show_params = requested_fields.map(&:to_s) self end
Returns the query string that will be used for this request. Query string parameters are returned in alphabetical order.
@return String The query string for this request
# File lib/bestbuy/request.rb, line 63 def query_string [ api_key_param, format_param, show_param, affiliate_param, ].compact.sort.join("&") end
Converts the request into a cURL request for debugging purposes
@returns [String] An eval-able string that will make a request using cURL to the BestBuy
API
# File lib/bestbuy/request.rb, line 35 def to_curl "curl #{to_s}" end
@returns [String] The URL that will be used for this Request
# File lib/bestbuy/request.rb, line 28 def to_s "https://api.bestbuy.com/v1/#{@endpoint}(#{@filters.join('|')})?#{query_string}" end
Private Instance Methods
Inserts the query string parameter responsible for crediting affiliate links
# File lib/bestbuy/request.rb, line 75 def affiliate_param if @affiliate_tracking_id "LID=#{@affiliate_tracking_id}" end end
Controls the apiKey query string parameter
# File lib/bestbuy/request.rb, line 82 def api_key_param "apiKey=#{@api_key}" end
Changes the response format. The API accepts XML, but this gem requres JSON
# File lib/bestbuy/request.rb, line 87 def format_param "format=json" end
Controls the fields returned for API response items. Corresponsds to the “show” query parameter
# File lib/bestbuy/request.rb, line 92 def show_param if @show_params && @show_params.length > 0 "show=#{@show_params.join(",")}" else nil end end