Class: Newegg::Api

Inherits:
Object
  • Object
show all
Defined in:
lib/newegg/api.rb

Direct Known Subclasses

Category, Store

Constant Summary collapse

UserAgents =
{
    iPhone: 'Newegg iPhone App / 4.3.1',
    Android: 'Newegg Android App / 3.3.3'
}
DefaultUserAgent =
:iPhone

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeApi

Returns a new instance of Api.



11
12
13
# File 'lib/newegg/api.rb', line 11

def initialize
  self._stores = []
end

Instance Attribute Details

#_categoriesObject

Returns the value of attribute _categories.



9
10
11
# File 'lib/newegg/api.rb', line 9

def _categories
  @_categories
end

#_idObject

Returns the value of attribute _id.



9
10
11
# File 'lib/newegg/api.rb', line 9

def _id
  @_id
end

#_storesObject

Returns the value of attribute _stores.



9
10
11
# File 'lib/newegg/api.rb', line 9

def _stores
  @_stores
end

#connObject

Returns the value of attribute conn.



9
10
11
# File 'lib/newegg/api.rb', line 9

def conn
  @conn
end

Instance Method Details

#bannersObject

retrieve banners



357
358
359
# File 'lib/newegg/api.rb', line 357

def banners
  JSON.parse(api_get('Promotions.egg', 'Banners').body)
end

#categories(store_id) ⇒ Object

retrieve and populate list of categories for a given store_id

Parameters:

  • store_id (Integer)

    of the store



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/newegg/api.rb', line 47

def categories(store_id)
  return [] if store_id.nil?

  response = api_get("Stores.egg", "Categories", store_id)
  categories = JSON.parse(response.body)
  categories = categories.collect do |category|
    Newegg::Category.new(category['Description'], category['CategoryType'], category['CategoryID'],
                         category['StoreID'], category['ShowSeeAllDeals'], category['NodeId'])
  end

  categories
end

#client_configObject

retrieve iphone client config



308
309
310
# File 'lib/newegg/api.rb', line 308

def client_config
  JSON.parse(api_get('Configurations.egg', 'IphoneClient').body)
end

#combo_deals(item_number, options = {}) ⇒ Object

retrieve product combo deals given an item number

Parameters:

  • item_number (String)

    of the product

  • sub_category (Integer)
  • sort_field (Integer)
  • page_number (Integer)


262
263
264
265
266
267
268
269
270
# File 'lib/newegg/api.rb', line 262

def combo_deals(item_number, options={})
  options = {sub_category: -1, sort_field: 0, page_number: 1}.merge(options)
  params = {
      'SubCategory' => options[:sub_category],
      'SortField'   => options[:sort_field],
      'PageNumber'  => options[:page_number]
  }
  JSON.parse(api_get('Products.egg', item_number, 'ComboDeals', params).body)
end

#connectionObject

retrieve an active connection or establish a new connection

@ returns [Faraday::Connection] conn to the web service



20
21
22
23
24
25
26
27
# File 'lib/newegg/api.rb', line 20

def connection
  self.conn ||= Faraday.new(:url => 'http://www.ows.newegg.com') do |faraday|
    faraday.request :url_encoded            # form-encode POST params
    faraday.response :logger                # log requests to STDOUT

    faraday.adapter Faraday.default_adapter # make requests with Net::HTTP
  end
end

#countryObject

retrieve tracking country



294
295
296
# File 'lib/newegg/api.rb', line 294

def country
  JSON.parse(api_get('Tracking.egg', 'Country').body)
end

#daily_dealObject

retrieve daily deal



322
323
324
# File 'lib/newegg/api.rb', line 322

def daily_deal
  JSON.parse(api_get('Promotions.egg', 'DailyDeal').body)
end

#details(item_number) ⇒ Object

retrieve product details given an item number

Parameters:

  • item_number (String)

    of the product



241
242
243
# File 'lib/newegg/api.rb', line 241

def details(item_number)
  JSON.parse(api_get('Products.egg', item_number, 'ProductDetails').body)
end

#exclusive_dealsObject

retrieve exclusive deal item



315
316
317
# File 'lib/newegg/api.rb', line 315

def exclusive_deals
  JSON.parse(api_get('Promotions.egg', 'ExclusiveDeals').body)
end

#keywords(keyword) ⇒ Object

retrieve related keywords given an keyword

Parameters:

  • item_number (String)

    of the product



232
233
234
# File 'lib/newegg/api.rb', line 232

def keywords(keyword)
  JSON.parse(api_get('AutoKeywords.egg', nil, nil, keyword: keyword).body)
end

retrieves information necessary to search for products, given the store_id, category_id, node_id

Parameters:

  • store_id (Integer)

    of the store

  • category_id (Integer)

    of the store

  • node_id (Integer)

    of the store



67
68
69
70
# File 'lib/newegg/api.rb', line 67

def navigate(store_id, category_id, node_id)
  response = api_get("Stores.egg", "Navigation", "#{store_id}/#{category_id}/#{node_id}")
  categories = JSON.parse(response.body)
end

#power_search(search_properties, options = {}) ⇒ Object

retrieves a single page of products given a query specified by an options hash. See options below.

Parameters:

  • search_properties (Array)
  • page_number (Integer)

    of the paginated power search results

  • product_type (Array)
  • min_price (String)
  • nvalues (String)
  • srch_in_desc (String)
  • brand_list (Array)
  • max_price (String)


210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/newegg/api.rb', line 210

def power_search(search_properties, options={})
  options = {page_number: 1, product_type: [], min_price: '', nvalues: '', srch_in_desc: '',
             brand_list: [], max_price: ''}.merge(options)
  request = {
      'PageNumber'       => options[:page_number],
      'ProductType'      => options[:product_type],
      'SearchProperties' => search_properties,
      'MinPrice'         => options[:min_price],
      'NValues'          => options[:nvalues],
      'SrchInDesc'       => options[:srch_in_desc],
      'BrandList'        => options[:brand_list],
      'MaxPrice'         => options[:max_price]
  }

  JSON.parse(api_post('PowerSearch.egg', nil, request).body, {quirks_mode: true})
end

#power_search_content(category_id) ⇒ Object

retrieve power search content given an category

Parameters:

  • category_id (Integer)


194
195
196
# File 'lib/newegg/api.rb', line 194

def power_search_content(category_id)
  JSON.parse(api_get('PowerSearchContent.egg', '0', "#{category_id}/-1").body)
end

#query(options = {}) ⇒ Object

retrieves a single page of products given a query specified by an options hash. See options below.

Parameters:

  • brand_list (Array)
  • search_properties (Array)
  • keyword (String)
  • max_price (String)
  • min_price (String)
  • nvalue (String)
  • product_type (Array)
  • page_number (Integer)
  • node_id (Integer)
  • category_id (Integer)
  • brand_id (Integer)
  • store_id (Integer)
  • store_type (Integer)
  • sub_category_id (Integer)


166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/newegg/api.rb', line 166

def query(options={})
  options = {brand_list: [], search_properties: [], keyword: '', max_price:'', min_price: '', nvalue: '', product_type: [],
             page_number: 1, node_id: -1, category_id: -1, brand_id: -1, store_id: -1, store_type: -1, sub_category_id: -1}.merge(options)
  request = {
      'BrandList'        => options[:brand_list],
      'SearchProperties' => options[:search_properties],
      'Keyword'          => options[:keyword],
      'MaxPrice'         => options[:max_price],
      'MinPrice'         => options[:min_price],
      'NValue'           => options[:nvalue],
      'ProductType'      => options[:product_type],
      'PageNumber'       => options[:page_number],
      'NodeId'           => options[:node_id],
      'CategoryId'       => options[:category_id],
      'BrandId'          => options[:brand_id],
      'StoreId'          => options[:store_id],
      'StoreType'        => options[:store_type],
      'SubCategoryId'    => options[:sub_category_id]
  }

  JSON.parse(api_post('Search.egg', 'Query', request).body, {quirks_mode: true})
end

#reviews(item_number, page_number = 1, options = {}) ⇒ Object

retrieve product reviews given an item number

Parameters:

  • item_number (String)

    of the product

  • page_number (Integer) (defaults to: 1)
  • time (String)
  • rating (String)

    default All (can also be 5, 4, 3, 2, 1)

  • sort (String)

    default ‘date posted’ (can also be ‘most helpful’, ‘highest rated’, ‘lowest rated’, ‘ownership’)



281
282
283
284
285
286
287
288
289
# File 'lib/newegg/api.rb', line 281

def reviews(item_number, page_number = 1, options={})
  options = {time: 'all', rating: 'All', sort: 'date posted'}.merge(options)
  params = {
      'filter.time'   => options[:time],
      'filter.rating' => options[:rating],
      'sort'          => options[:sort]
  }
  JSON.parse(api_get('Products.egg', item_number, "Reviewsinfo/#{page_number}", params).body)
end

#search(options = {}) ⇒ Object

retrieves a single page of products given a query specified by an options hash. See options below. node_id, page_number, and an optional sorting method

Parameters:

  • store_id, (Integer)

    from @api.navigation, returned as StoreID

  • category_id (Integer)

    from @api.navigation, returned as CategoryType

  • sub_category_id (Integer)

    from @api.navigation, returned as CategoryID

  • node_id (Integer)

    from @api.navigation, returned as NodeId

  • page_number (Integer)

    of the paginated search results, returned as PaginationInfo from search

  • sort (String)

    style of the returned search results, default is FEATURED (can also be RATING, PRICE, PRICED, REVIEWS)

  • keywords (String)


127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/newegg/api.rb', line 127

def search(options={})
  options = {store_id: -1, category_id: -1, sub_category_id: -1, node_id: -1, page_number: 1, sort: "FEATURED",
             keywords: ""}.merge(options)
  request = {
      'IsUPCCodeSearch'      => false,
      'IsSubCategorySearch'  => options[:sub_category_id] > 0,
      'isGuideAdvanceSearch' => false,
      'StoreDepaId'          => options[:store_id],
      'CategoryId'           => options[:category_id],
      'SubCategoryId'        => options[:sub_category_id],
      'NodeId'               => options[:node_id],
      'BrandId'              => -1,
      'NValue'               => "",
      'Keyword'              => options[:keywords],
      'Sort'                 => options[:sort],
      'PageNumber'           => options[:page_number]
  }

  JSON.parse(api_post("Search.egg", "Advanced", request).body, {quirks_mode: true})
end

#shell_shockersObject

retrieve shell shocker items



343
344
345
# File 'lib/newegg/api.rb', line 343

def shell_shockers
  JSON.parse(api_get('Promotions.egg', 'ShellShockers').body)
end

#shell_shockers_newObject

retrieve new shell shocker items



350
351
352
# File 'lib/newegg/api.rb', line 350

def shell_shockers_new
  JSON.parse(api_get('Promotions.egg', 'NewShellShocker').body)
end

#shop_all_navigationObject

retrieves stores



89
90
91
# File 'lib/newegg/api.rb', line 89

def shop_all_navigation
  JSON.parse(api_get('Store.egg', 'ShopAllNavigation').body)
end

#shop_more_navigationObject



93
94
95
# File 'lib/newegg/api.rb', line 93

def shop_more_navigation
  JSON.parse(api_get('Store.egg', 'MoreNavigation').body)
end

#shopping_guideObject

retrieve shopping guide config



301
302
303
# File 'lib/newegg/api.rb', line 301

def shopping_guide
  JSON.parse(api_get('Configurations.egg', 'ShoppingGuideConfig').body)
end

#specifications(item_number) ⇒ Object

retrieve product information given an item number

Parameters:

  • item_number (String)

    of the product



250
251
252
# File 'lib/newegg/api.rb', line 250

def specifications(item_number)
  JSON.parse(api_get("Products.egg", item_number, "Specification").body)
end

#spotlightObject

retrieve spotlight item



329
330
331
# File 'lib/newegg/api.rb', line 329

def spotlight
  JSON.parse(api_get('Promotions.egg', 'Spotlight').body)
end

#spotlightsObject

retrieve spotlights



336
337
338
# File 'lib/newegg/api.rb', line 336

def spotlights
  JSON.parse(api_get('Promotions.egg', 'Spotlights').body)
end

#store_content(store_id, category_id = -1,, node_id = -1,, store_type = 4, page_number = 1) ⇒ Object

retrieves store content



75
76
77
78
79
80
81
82
83
84
# File 'lib/newegg/api.rb', line 75

def store_content(store_id, category_id = -1, node_id = -1, store_type = 4, page_number = 1)
  params = {
      'storeId'    => store_id,
      'categoryId' => category_id,
      'nodeId'     => node_id,
      'storeType'  => store_type,
      'pageNumber' => page_number
  }
  JSON.parse(api_get('Stores.egg', 'Content', nil, params).body)
end

#store_navigate(store_id, category_id = -1,, node_id = -1,, store_type = 4) ⇒ Object

retrieves information necessary to search for products, given the store_id, category_id, node_id

Parameters:

  • store_id (Integer)

    of the store

  • category_id (Integer) (defaults to: -1,)

    of the store

  • node_id (Integer) (defaults to: -1,)

    of the store

  • store_type (Integer) (defaults to: 4)

    of the store



105
106
107
108
109
110
111
112
113
# File 'lib/newegg/api.rb', line 105

def store_navigate(store_id, category_id = -1, node_id = -1, store_type = 4)
  params = {
      'storeId'    => store_id,
      'categoryId' => category_id,
      'nodeId'     => node_id,
      'storeType'  => store_type
  }
  JSON.parse(api_get('Store.egg', 'storeNavigation', nil, params).body)
end

#storesObject

retrieve and populate a list of available stores



32
33
34
35
36
37
38
39
40
# File 'lib/newegg/api.rb', line 32

def stores
  return self._stores if not self._stores.empty?
  response = api_get("Stores.egg")
  stores = JSON.parse(response.body)
  stores.each do |store|
    self._stores <<  Newegg::Store.new(store['Title'], store['StoreDepa'], store['StoreID'], store['ShowSeeAllDeals'])
  end
  self._stores
end