Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion lib/remixr/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class Client
base_uri 'api.remix.bestbuy.com/v1'
format :json

attr_reader :api_key, :store_filters, :product_filters
attr_reader :api_key, :store_filters, :product_filters, :category_filters

# Get your api_key found here http://remix.bestbuy.com/apps/register
def initialize(api_key=nil)
Expand All @@ -14,6 +14,7 @@ def initialize(api_key=nil)
@api_path = ''
@store_filters = {}
@product_filters = {}
@category_filters = {}
end

# Example filters:
Expand Down Expand Up @@ -61,6 +62,19 @@ def products(filters={})
self
end

# Example filters:
# Products in Gift Center
# categories({:name => "Gift Center"})
# GET /v1/categorie(name=”Gift Center”)
def categories(filters={})
unless @api_path.include?('categories()')
@api_path += '+' unless @api_path.blank?
@api_path += "categories()"
end
@category_filters.merge!(filters)
self
end


# Executes the search
# Possible options:
Expand All @@ -73,6 +87,7 @@ def fetch(options={})

apply_store_filters
apply_product_filters
apply_category_filters
@api_path = URI.escape(@api_path)
response = self.class.get("/" + @api_path, :query => opts)
@api_path = ''
Expand All @@ -96,6 +111,14 @@ def apply_product_filters
end
end

def apply_category_filters
if @category_filters.keys.blank?
@api_path.gsub!("categories()", "categories")
else
@api_path.gsub!("categories()", "categories(#{filter_params_string(@category_filters)})")
end
end

def filter_params_string(filters)
return "" unless filters.is_a?(Hash)

Expand Down