class BestbuyApi::Params

Public Class Methods

new(criteria, attributes) click to toggle source
# File lib/bestbuy_api/params.rb, line 3
def initialize(criteria, attributes)
  @criteria = criteria
  @attributes = attributes
end

Public Instance Methods

structure() click to toggle source
# File lib/bestbuy_api/params.rb, line 8
def structure
  { slug: build_filters, query: build_response_options }
end

Private Instance Methods

build_filters() click to toggle source
# File lib/bestbuy_api/params.rb, line 14
def build_filters
  collection = collect_search
  collection.empty? ? '' : query_string(collection)
end
build_response_options() click to toggle source
# File lib/bestbuy_api/params.rb, line 19
def build_response_options
  query = @criteria.slice(:limit, :page)
  query[:show] = collect_read
  query
end
collect_read() click to toggle source
# File lib/bestbuy_api/params.rb, line 25
def collect_read
  Array[@criteria[:attributes].map do |key|
    elements = @attributes[:read]
    if elements.key?(key)
      attribute = elements[key]
      attribute.key?(:field) ? attribute[:field] : key
    end
  end].join(',')
end
query_string(props) click to toggle source
# File lib/bestbuy_api/params.rb, line 45
def query_string(props)
  search = props.delete('search')
  uri = URI.encode_www_form(props)
  search_query = "(search=#{search})" if search
  uri = format("#{search_query}%<and>s", and: "&#{uri}") if search_query && uri
  "(#{uri})"
end