Sha256: 9d6259a1961a7900d700e2cbcdd71be08c54a75b5a64a714da9ae02fcb9d04d8

Contents?: true

Size: 986 Bytes

Versions: 7

Compression:

Stored size: 986 Bytes

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(options = {})
      requires!(options, :username, :password)
      new(options).all
    end

    # Returns an array of hashes with the inventory item details.
    def all
      inventory = []

      connect(@options) do |ftp|
        ftp.chdir(BillHicks.config.top_level_dir)

        lines = ftp.gettextfile(INVENTORY_FILENAME, nil)

        CSV.parse(lines, headers: :first_row) do |row|
          inventory << {
            product: row.fetch('Product'),
            upc: row.fetch('UPC'),
            quantity: (Integer(row.fetch('Qty Avail')) rescue 0)
          }
        end
      end

      inventory
    end

  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
bill_hicks-1.1.5 lib/bill_hicks/inventory.rb
bill_hicks-1.1.4 lib/bill_hicks/inventory.rb
bill_hicks-1.1.3 lib/bill_hicks/inventory.rb
bill_hicks-1.1.2 lib/bill_hicks/inventory.rb
bill_hicks-1.1.1 lib/bill_hicks/inventory.rb
bill_hicks-1.1.0 lib/bill_hicks/inventory.rb
bill_hicks-1.0.0 lib/bill_hicks/inventory.rb