Class: GunAccessorySupply::Catalog

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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

connect

Constructor Details

#initialize(options = {}) ⇒ Catalog

Returns a new instance of Catalog.



4
5
6
7
# File 'lib/gun_accessory_supply/catalog.rb', line 4

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

Class Method Details

.all(options = {}) ⇒ Object



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

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

Instance Method Details

#allObject



14
15
16
17
18
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
53
54
# File 'lib/gun_accessory_supply/catalog.rb', line 14

def all
  tempfile = get_most_recent_file(GunAccessorySupply.config.inventory_filename_prefix, 'out')
  items = []

  File.open(tempfile).each_with_index do |row, i|
    row = parse_row(row)

    if i==0
      @headers = row
      next
    end

    category = row[@headers.index('Sub-Category')].try(:strip)

    item = {
      mfg_number:      row[@headers.index('Item ID')].try(:strip),
      upc:             row[@headers.index('UPC')].try(:strip),
      name:            row[@headers.index('Item Description')].try(:strip),
      quantity:        row[@headers.index("Available Qty")].to_i,
      price:           row[@headers.index('Dealer Cost')].try(:strip),
      msrp:            row[@headers.index('MSRP')].try(:strip),
      map_price:       row[@headers.index('MAP Price')].try(:strip),
      brand:           row[@headers.index('Manufacturer')].try(:strip),
      item_identifier: row[@headers.index("Item ID")].try(:strip),
      category:        cleaned_category(category)[0],
      subcategory:     cleaned_category(category)[1],
      weight:          row[@headers.index('Shipping Weight')].try(:strip),
      features: {
        caliber:       row[@headers.index('Caliber')].try(:strip),
        image_name:    row[@headers.index("Image Link")].try(:strip),
      }
    }

    items << item
  end

  tempfile.close
  tempfile.unlink

  items
end