Class: Davidsons::Inventory

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

Constant Summary collapse

WHOLE_CATALOG_FILENAME =
'davidsons_inventory.csv'.freeze
DEFAULT_WHOLE_CATALOG_SMART_OPTS =
{
  chunk_size: 500,
  convert_values_to_numeric: false,
  force_utf8: true,
  remove_unmapped_keys: true,
  key_mapping: {
    "item_#":     :item_identifier,
    upc_code:     :upc,
    msp:          :msp,
    dealer_price: :dealer_price,
    sale_price:   :sale_price,
    quantity:     :quantity,
  }
}
QUANTITY_FILENAME =
'davidsons_quantity.csv'.freeze
DEFAULT_QUANTITY_SMART_OPTS =
{
  chunk_size: 2000,
  convert_values_to_numeric: false,
  force_utf8: true,
  key_mapping: {
    item_number: :item_identifier,
    upc_code:    :upc
  }
}

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

connect

Constructor Details

#initialize(options = {}) ⇒ Inventory

Returns a new instance of Inventory.

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



37
38
39
40
41
# File 'lib/davidsons/inventory.rb', line 37

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

Class Method Details

.all(options = {}) ⇒ Object

Iterates over all items in the ‘davidsons_inventory.csv’ file yielding the item_identifier, quantity, and price. We have to use this file since the ‘davidsons_quantity.csv’ file does not included prices. The price can vary based on the pricing_tier

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



53
54
55
56
# File 'lib/davidsons/inventory.rb', line 53

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

.quantity(options = {}) ⇒ Object

Iterates over all items in the ‘davidsons_quantity.csv’ file yielding the item_identifier, upc, quantity and locations.

Parameters:

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

Options Hash (options):

  • :username (Symbol)

    required

  • :password (Symbol)

    required



63
64
65
66
# File 'lib/davidsons/inventory.rb', line 63

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

Instance Method Details

#allObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/davidsons/inventory.rb', line 68

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

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

      item[:price]    = lowest_price(item)
      item[:quantity] = item[:quantity].to_i

      items << item
    end
  end

  items
end

#quantityObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/davidsons/inventory.rb', line 86

def quantity
  items    = []
  tempfile = get_file(QUANTITY_FILENAME)

  SmarterCSV.process(tempfile, DEFAULT_QUANTITY_SMART_OPTS) do |chunk|
    chunk.each do |item|
      item[:quantity]  = item[:quantity_nc].to_i + item[:quantity_az].to_i
      item[:locations] = map_location_quantities(item)

      items << item
    end
  end

  items
end