Class: RsrGroup::Inventory

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

Constant Summary collapse

DEFAULT_CATALOG_SMART_OPTS =

This corresponds with the ‘rsrinventory-new.txt’ and ‘rsrinventory-keydlr-new.txt’ files and the DEFAULT_CATALOG_FILENAME and KEYDEALER_CATALOG_FILENAME constants

{
  chunk_size: 500,
  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,
}
DEFAULT_QUANTITY_SMART_OPTS =

This corresponds with the ‘IM-QTY-CSV.csv’ file and the QTY_FILENAME constant

{
  chunk_size: 2000,
  convert_values_to_numeric: false,
  col_sep: ',',
  headers_in_file: false,
  user_provided_headers: [
    :item_identifier,
    :quantity,
  ]
}

Constants inherited from Base

Base::DEFAULT_CATALOG_FILENAME, Base::DEFAULT_DIR, Base::KEYDEALER_CATALOG_FILENAME, Base::KEYDEALER_DIR, Base::MAP_FILENAME, Base::QTY_FILENAME

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

connect

Constructor Details

#initialize(options = {}) ⇒ Inventory

Returns a new instance of Inventory.



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

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

Class Method Details

.all(options = {}) ⇒ Object



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

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

.quantity(options = {}) ⇒ Object



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

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

Instance Method Details

#allObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/rsr_group/inventory.rb', line 48

def all
  items = []

  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 to a tempfile
      ftp.getbinaryfile(KEYDEALER_CATALOG_FILENAME, tempfile.path)
    else
      ftp.chdir(DEFAULT_DIR)
      # Pull from the FTP and save to a tempfile
      ftp.getbinaryfile(DEFAULT_CATALOG_FILENAME, tempfile.path)
    end
    ftp.close

    SmarterCSV.process(tempfile, DEFAULT_CATALOG_SMART_OPTS) do |chunk|
      chunk.each do |item|
        if !item[:allocated_closeout_deleted].nil? && item[:allocated_closeout_deleted].to_sym.eql?(:Allocated)
          item[:quantity] = 0
        else
          item[:quantity] = item[:quantity].to_i
        end

        items << item
      end
    end

    tempfile.unlink
  end

  items
end

#quantityObject

Parse through the ‘IM-QTY-CSV.csv’ file



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/rsr_group/inventory.rb', line 85

def quantity
  items = []

  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(QTY_FILENAME, tempfile.path)
    else
      ftp.chdir(DEFAULT_DIR)
      # Pull from the FTP and save as a temp file
      ftp.getbinaryfile(QTY_FILENAME, tempfile.path)
    end
    ftp.close

    SmarterCSV.process(tempfile, DEFAULT_QUANTITY_SMART_OPTS) do |chunk|
      chunk.each do |item|
        item[:quantity] = item[:quantity].to_i

        items << item
      end
    end

    tempfile.unlink
  end

  items
end