Class: BillHicks::Inventory

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

Overview

Inventory item response structure:

{
  product:  "...",
  upc:      "...",
  quantity: "..."
}

Constant Summary collapse

INVENTORY_FILENAME =
'billhicksinventory.csv'

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

connect

Constructor Details

#initialize(options = {}) ⇒ Inventory

Returns a new instance of Inventory.



13
14
15
16
# File 'lib/bill_hicks/inventory.rb', line 13

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

Class Method Details

.all(chunk_size = 15, options = {}, &block) ⇒ Object



18
19
20
21
# File 'lib/bill_hicks/inventory.rb', line 18

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

Instance Method Details

#all(chunk_size, &block) ⇒ Object



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
# File 'lib/bill_hicks/inventory.rb', line 23

def all(chunk_size, &block)
  connect(@options) do |ftp|
    begin
      csv_tempfile = Tempfile.new

      ftp.chdir(BillHicks.config.top_level_dir)
      ftp.getbinaryfile(INVENTORY_FILENAME, csv_tempfile.path)

      SmarterCSV.process(csv_tempfile, {
        chunk_size: chunk_size,
        force_utf8: true,
        convert_values_to_numeric: false,
        key_mapping: {
          qty_avail: :quantity,
          upc:       :item_identifier
        }
      }) do |chunk|
        chunk.each do |item|
          item.except!(:product)
        end

        yield(chunk)
      end
    ensure
      ftp.close
    end
  end
end