Class: RsrGroup::Inventory

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

Constant Summary collapse

KEYDEALER_DIR =
'keydealer'.freeze
INVENTORY_DIR =
'ftpdownloads'.freeze
QTY_FILENAME =
'IM-QTY-CSV.csv'.freeze
MAP_FILENAME =
'retail-map.csv'.freeze
INVENTORY_FILENAME =
'rsrinventory-new.txt'.freeze
KEYDEALER_FILENAME =
'rsrinventory-keydlr-new.txt'.freeze
DEFAULT_SMART_OPTS =
{
  chunk_size: 100,
  convert_values_to_numeric: false,
  col_sep: ";",
  quote_char: "|",
  headers_in_file: false,
  user_provided_headers: [
    :item_identifier, :upc, :short_description, :department_number, :manufacturer_id, :retail_price,
    :price, :weight, :quantity, :model, :manufacturer, :mfg_number, :allocated_closeout_deleted, :long_description,
    :image_name, 51.times.map { |i| "state_#{i}".to_sym }, :ships_ground_only, :signature_required, :blocked_from_drop_ship,
    :date_entered, :map_price, :image_disclaimer, :length, :width, :height, :null
  ].flatten,
  remove_unmapped_keys: true,
}

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

connect, requires!

Constructor Details

#initialize(options = {}) ⇒ Inventory

Returns a new instance of Inventory.



26
27
28
29
30
# File 'lib/rsr_group/inventory.rb', line 26

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

  @options = options
end

Class Method Details

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



42
43
44
45
# File 'lib/rsr_group/inventory.rb', line 42

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

.get_quantity_file(options = {}) ⇒ Object



32
33
34
35
# File 'lib/rsr_group/inventory.rb', line 32

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

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



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

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

Instance Method Details

#all(chunk_size, &block) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/rsr_group/inventory.rb', line 47

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

    # Is this a key dealer?
    if ftp.nlst.include?(KEYDEALER_DIR)
      ftp.chdir(KEYDEALER_DIR)
      # Pull from the FTP and save as a temp file
      ftp.getbinaryfile(KEYDEALER_FILENAME, tempfile.path)
    else
      ftp.chdir(INVENTORY_DIR)
      # Pull from the FTP and save as a temp file
      ftp.getbinaryfile(INVENTORY_FILENAME, tempfile.path)
    end

    SmarterCSV.process(tempfile, DEFAULT_SMART_OPTS.merge(chunk_size: chunk_size)) do |chunk|
      yield(chunk)
    end

    tempfile.unlink
    ftp.close
  end
end

#get_quantity_fileObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/rsr_group/inventory.rb', line 71

def get_quantity_file
  connect(@options) do |ftp|
    quantity_tempfile = Tempfile.new
    tempfile          = Tempfile.new

    # Is this a key dealer?
    if ftp.nlst.include?(KEYDEALER_DIR)
      ftp.chdir(KEYDEALER_DIR)
      # Pull from the FTP and save as a temp file
      ftp.getbinaryfile(QTY_FILENAME, quantity_tempfile.path)
    else
      ftp.chdir(INVENTORY_DIR)
      # Pull from the FTP and save as a temp file
      ftp.getbinaryfile(QTY_FILENAME, quantity_tempfile.path)
    end

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

    quantity_tempfile.unlink
    ftp.close

    tempfile.path
  end
end

#quantity(chunk_size, &block) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/rsr_group/inventory.rb', line 105

def quantity(chunk_size, &block)
  connect(@options) do |ftp|
    tempfile = Tempfile.new

    # Is this a key dealer?
    if ftp.nlst.include?(KEYDEALER_DIR)
      ftp.chdir(KEYDEALER_DIR)
      # Pull from the FTP and save as a temp file
      ftp.getbinaryfile(KEYDEALER_FILENAME, tempfile.path)
    else
      ftp.chdir(INVENTORY_DIR)
      # Pull from the FTP and save as a temp file
      ftp.getbinaryfile(INVENTORY_FILENAME, tempfile.path)
    end

    SmarterCSV.process(tempfile, DEFAULT_SMART_OPTS.merge(chunk_size: chunk_size)) do |chunk|
      chunk.each do |item|
        item = Hash[*item.select {|k,v| [:item_identifier, :quantity].include?(k)}.flatten]
      end

      yield(chunk)
    end

    tempfile.unlink
    ftp.close
  end
end