Sha256: ec6cbdf0e6445e739a0929bce134507782ac1edcedad134afeb871301647bf0d

Contents?: true

Size: 1.39 KB

Versions: 2

Compression:

Stored size: 1.39 KB

Contents

module BillHicks
  # Inventory item response structure:
  #
  #   {
  #     product:  "...",
  #     upc:      "...",
  #     quantity: "..."
  #   }
  class Inventory < Base

    INVENTORY_FILENAME = 'billhicksinventory.csv'

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

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

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

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

          ftp.chdir(BillHicks.config.top_level_dir)
          ftp.getbinaryfile(INVENTORY_FILENAME, csv_tempfile.path)

          SmarterCSV.process(csv_tempfile, {
            :chunk_size => chunk_size,
            :force_utf8 => true,
            :convert_values_to_numeric => false,
            :key_mapping => {
              :qty_avail  => :quantity,
              :upc        => :item_identifier
            }
          }) do |chunk|
            chunk.each do |item|
              item.except!(:product)
            end

            yield(chunk)
          end
        ensure
          ftp.close
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
bill_hicks-2.0.1 lib/bill_hicks/inventory.rb
bill_hicks-2.0 lib/bill_hicks/inventory.rb