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

Public Instance Methods

call() click to toggle source

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
pluck(*requested_fields) click to toggle source

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
query_string() click to toggle source

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
to_curl() click to toggle source

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
to_s() click to toggle source

@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

affiliate_param() click to toggle source

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
api_key_param() click to toggle source

Controls the apiKey query string parameter

# File lib/bestbuy/request.rb, line 82
def api_key_param
  "apiKey=#{@api_key}"
end
format_param() click to toggle source

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
show_param() click to toggle source

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