Class: Davidsons::Catalog

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

Constant Summary collapse

WHOLE_CATALOG_FILENAME =
'davidsons_inventory.csv'.freeze
DEFAULT_SMART_OPTS =
{
  chunk_size: 500,
  convert_values_to_numeric: false,
  force_utf8: true,
  key_mapping: {
    "item_#":         :item_identifier,
    item_description: :name,
    upc_code:         :upc,
    manufacturer:     :brand,
    retail_price:     :msrp,
    gun_type:         :category,
  }
}

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

connect

Constructor Details

#initialize(options = {}) ⇒ Catalog

Returns a new instance of Catalog.

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :username (Symbol)

    required

  • :password (Symbol)

    required

  • :pricing_tier (Symbol)

    optional

    • Denotes which price to use

    • Can be :regular or :minimum



25
26
27
28
29
# File 'lib/davidsons/catalog.rb', line 25

def initialize(options = {})
  requires!(options, :username, :password)
  @options = options
  @options[:pricing_tier] ||= :regular
end

Class Method Details

.all(options = {}) ⇒ Object



31
32
33
34
# File 'lib/davidsons/catalog.rb', line 31

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

Instance Method Details

#allObject



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
# File 'lib/davidsons/catalog.rb', line 36

def all
  tempfile = get_file(WHOLE_CATALOG_FILENAME)
  items    = []

  SmarterCSV.process(tempfile, DEFAULT_SMART_OPTS) do |chunk|
    chunk.each do |item|
      item[:msrp]&.tr!('$', '')
      item[:upc]&.tr!('#', '')

      item[:price]      = lowest_price(item)
      item[:mfg_number] = item[:item_identifier]
      item[:quantity]   = item[:quantity].to_i
      item[:features]   = map_features(item)

      item.delete(:sale_ends)
      item.delete(:sale_price)

      items << item
    end
  end

  tempfile.unlink

  items
end