Sha256: f3613ad9cea65875ce9a4c96fb378bfb91b62369d7f82eaef2efb5f40a9a86ac

Contents?: true

Size: 1.07 KB

Versions: 5

Compression:

Stored size: 1.07 KB

Contents

module Zanders
  class Inventory < Base

    INVENTORY_FILENAME = "liveinv.csv"

    DEFAULT_SMART_OPTS = {
      convert_values_to_numeric: false,
      key_mapping: {
        available:  :quantity,
        itemnumber: :item_identifier,
        price1:     :price
      },
      remove_unmapped_keys: true
    }

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

      @options = options
    end

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

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

          ftp.chdir(Zanders.config.ftp_directory)
          ftp.getbinaryfile(INVENTORY_FILENAME, csv_tempfile.path)

          opts = DEFAULT_SMART_OPTS.merge(chunk_size: chunk_size)

          SmarterCSV.process(csv_tempfile, opts) do |chunk|
            yield(chunk)
          end

          csv_tempfile.unlink
        ensure
          ftp.close
        end
      end
    end

  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
zanders-2.1.2 lib/zanders/inventory.rb
zanders-2.1.1 lib/zanders/inventory.rb
zanders-2.1.0 lib/zanders/inventory.rb
zanders-2.0.6 lib/zanders/inventory.rb
zanders-2.0.5 lib/zanders/inventory.rb