class RsrGroup::Inventory

Constants

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

DEFAULT_QUANTITY_SMART_OPTS

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

Public Class Methods

all(options = {}) click to toggle source
# File lib/rsr_group/inventory.rb, line 43
def self.all(options = {})
  requires!(options, :username, :password)
  new(options).all
end
new(options = {}) click to toggle source
# File lib/rsr_group/inventory.rb, line 33
def initialize(options = {})
  requires!(options, :username, :password)
  @options = options
end
quantity(options = {}) click to toggle source
# File lib/rsr_group/inventory.rb, line 38
def self.quantity(options = {})
  requires!(options, :username, :password)
  new(options).quantity
end

Public Instance Methods

all() click to toggle source
# 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
quantity() click to toggle source

Parse through the 'IM-QTY-CSV.csv' file

# 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