Class: OrionWholesale::Catalog

Inherits:
Base
  • Object
show all
Defined in:
lib/orion_wholesale/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/orion_wholesale/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/orion_wholesale/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
55
56
57
58
59
60
61
62
63
64
# File 'lib/orion_wholesale/catalog.rb', line 14

def all
  tempfile = get_most_recent_file(OrionWholesale.config.catalog_filename_prefix, OrionWholesale.config.top_level_dir)
  items = []

  File.open(tempfile).each_with_index do |row, i|
    row = row.split("\t")
    
    if i==0
      @headers = row
      next
    end

    if row[@headers.index('Category1')].try(:strip) == 'Guns'
      # Guns
      case row[@headers.index('Category2')].try(:strip)
      when 'Long Guns'
        @category    = row[@headers.index('Category3')].try(:strip)
        @subcategory = row[@headers.index("Category4")].try(:strip)
      when 'Handguns'
        @category    = row[@headers.index('Category2')].try(:strip)
        @subcategory = row[@headers.index("Category3")].try(:strip)
      end
    else
      # Everything else
      @category    = row[@headers.index('Category1')].try(:strip)
      @subcategory = row[@headers.index('Category2')].try(:strip)
    end

    item = {
      mfg_number:      row[@headers.index('Item ID')].try(:strip),
      upc:             row[@headers.index('Bar Code')].try(:strip),
      name:            row[@headers.index('Description')].try(:strip),
      quantity:        row[@headers.index('Qty available')].to_i,
      price:           row[@headers.index('Price')].try(:strip),
      brand:           row[@headers.index('Brand')].try(:strip),
      item_identifier: row[@headers.index("Item ID")].try(:strip),
      category:        @category,
      subcategory:     @subcategory,
      features:        {
                         image_name: row[@headers.index("ImageFileName\n")].try(:strip),
                       },
    }

    items << item
  end

  tempfile.close
  tempfile.unlink

  items
end