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

CHUNK_SIZE =
2000
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.



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

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

Class Method Details

.all(options = {}) ⇒ Object



29
30
31
32
# File 'lib/bill_hicks/inventory.rb', line 29

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

.get_quantity_file(options = {}) ⇒ Object



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

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

.quantity(options = {}) ⇒ Object



24
25
26
27
# File 'lib/bill_hicks/inventory.rb', line 24

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

Instance Method Details

#allObject Also known as: quantity



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/bill_hicks/inventory.rb', line 34

def all
  items             = []
  quantity_tempfile = get_file(INVENTORY_FILENAME)

  SmarterCSV.process(quantity_tempfile, {
    chunk_size: CHUNK_SIZE,
    force_utf8: true,
    convert_values_to_numeric: false,
    key_mapping: {
      product: :item_identifier,
      qty_avail: :quantity,
    }
  }) do |chunk|
    chunk.each do |item|
      items << item
    end
  end

  quantity_tempfile.close
  quantity_tempfile.unlink

  items
end

#get_quantity_fileObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/bill_hicks/inventory.rb', line 58

def get_quantity_file
  quantity_tempfile = get_file(INVENTORY_FILENAME)
  tempfile          = Tempfile.new

  SmarterCSV.process(quantity_tempfile, {
    chunk_size: CHUNK_SIZE,
    force_utf8: true,
    convert_values_to_numeric: false,
    key_mapping: {
      product: :item_identifier,
      qty_avail: :quantity,
    }
  }) do |chunk|
    chunk.each do |item|
      tempfile.puts("#{item[:item_identifier]},#{item[:quantity]}")
    end
  end

  quantity_tempfile.close
  quantity_tempfile.unlink
  tempfile.close
  tempfile.path
end