Class: Remixr::Client

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/remixr/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key = nil) ⇒ Client

Get your api_key found here remix.bestbuy.com/apps/register



10
11
12
13
14
15
16
17
# File 'lib/remixr/client.rb', line 10

def initialize(api_key=nil)
  @api_key = api_key
  @api_key ||= Remixr.api_key
  
  @api_path = ''
  @store_filters = {}
  @product_filters = {}
end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



7
8
9
# File 'lib/remixr/client.rb', line 7

def api_key
  @api_key
end

#product_filtersObject (readonly)

Returns the value of attribute product_filters.



7
8
9
# File 'lib/remixr/client.rb', line 7

def product_filters
  @product_filters
end

#store_filtersObject (readonly)

Returns the value of attribute store_filters.



7
8
9
# File 'lib/remixr/client.rb', line 7

def store_filters
  @store_filters
end

Instance Method Details

#fetch(options = {}) ⇒ Object

Executes the search Possible options: :page - positive integer for page number :show - comma delimited string or array of field names to show :sort - hash or string of sort info => ‘asc|desc’



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/remixr/client.rb', line 70

def fetch(options={})
  opts = {:apiKey => @api_key, :format => 'json'}
  opts.merge!(scrub_options(options))
  
  apply_store_filters
  apply_product_filters
  @api_path = URI.escape(@api_path)
  response = self.class.get("/" + @api_path, :query => opts)
  @api_path = ''
  Mash.new response
end

#in_region(region) ⇒ Object

Convenience method for finding a store by region

stores.in_region('TX')
GET /v1/stores(region=TX)


47
48
49
# File 'lib/remixr/client.rb', line 47

def in_region(region)
  self.stores({'region' => region})
end

#in_zip(zip) ⇒ Object

Convenience method for finding a store by zip

stores.in_zip(76227)
GET /v1/stores(postalCode=76227)


40
41
42
# File 'lib/remixr/client.rb', line 40

def in_zip(zip)
  self.stores({'postalCode' => zip})
end

#products(filters = {}) ⇒ Object

Example filters: Products under 20 bucks

products({:salePrice => {'$lt' => 20.00}})
GET /v1/products(salePrice<20.00)


55
56
57
58
59
60
61
62
# File 'lib/remixr/client.rb', line 55

def products(filters={})
  unless @api_path.include?('products()')
    @api_path += '+' unless @api_path.blank?
    @api_path += "products()"
  end
  @product_filters.merge!(filters)
  self
end

#stores(filters = {}) ⇒ Object

Example filters: Stores within 50 miles of ZIP 76227

stores({:area => [76227,50]})
GET /v1/stores(area(76227,50))

Stores west of the Pecos

stores({:lng => {'$lt' => -104.304199}})
GET /v1/stores(lng>-104.304199)


28
29
30
31
32
33
34
35
# File 'lib/remixr/client.rb', line 28

def stores(filters={})
  unless @api_path.include?('stores()')
    @api_path += '+' unless @api_path.blank?
    @api_path += "stores()"
  end
  @store_filters.merge!(filters)
  self
end