Class: Lipseys::Catalog

Inherits:
Base
  • Object
show all
Defined in:
lib/lipseys/catalog.rb

Constant Summary collapse

CHUNK_SIZE =
500
API_URL =
'https://www.lipseys.com/API/catalog.ashx'
ITEMTYPES =
%w(ACCESSORY FIREARM NFA OPTIC)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Catalog

Returns a new instance of Catalog.



8
9
10
11
12
# File 'lib/lipseys/catalog.rb', line 8

def initialize(options = {})
  requires!(options, :username, :password)

  @options = options
end

Class Method Details

.all(options = {}) ⇒ Object



14
15
16
17
# File 'lib/lipseys/catalog.rb', line 14

def self.all(options = {})
  requires!(options, :username, :password)
  new(options).all
end

Instance Method Details

#allObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/lipseys/catalog.rb', line 19

def all
  inventory_tempfile = stream_to_tempfile(Lipseys::Inventory::API_URL, @options)
  catalog_tempfile   = stream_to_tempfile(API_URL, @options)
  inventory          = []
  items              = []

  # Let's get the inventory and toss 'er into an array
  Lipseys::Parser.parse(inventory_tempfile, 'Item') do |node|
    inventory.push({
      item_identifier: content_for(node, 'ItemNo'),
      map_price: content_for(node, 'RetailMAP'),
      quantity: content_for(node, 'QtyOnHand'),
      price: content_for(node, 'Price')
    })
  end

  Lipseys::Parser.parse(catalog_tempfile, 'Item') do |node|
    item = map_hash(node)
    availability = inventory.select { |i| i[:item_identifier] == item[:item_identifier] }.first

    if availability
      item[:price]     = availability[:price]
      item[:quantity]  = availability[:quantity]
      item[:map_price] = availability[:map_price]
    end

    items << item
  end

  inventory_tempfile.unlink
  catalog_tempfile.unlink

  items
end