Sha256: 0cad945c90712341f3a54af3a64899641db799112e882e07fdf4df1c10f99c03

Contents?: true

Size: 1.25 KB

Versions: 3

Compression:

Stored size: 1.25 KB

Contents

module Lipseys
  class Inventory < Base

    API_URL = 'https://www.lipseys.com/API/pricequantitycatalog.ashx'

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

      @options = options
    end

    def self.all(options = {}, &block)
      new(options).all &block
    end

    def self.quantity(options = {}, &block)
      new(options).all &block
    end

    def self.get_quantity_file(options = {})
      new(options).get_quantity_file
    end

    def all(&block)
      tempfile = stream_to_tempfile(API_URL, @options)

      Lipseys::Parser.parse(tempfile, 'Item') do |node|
        yield map_hash(node)
      end

      tempfile.unlink
    end

    def get_quantity_file
      quantity_tempfile = stream_to_tempfile(API_URL, @options)
      tempfile          = Tempfile.new

      Lipseys::Parser.parse(quantity_tempfile, 'Item') do |node|
        tempfile.puts("#{content_for(node, 'ItemNo')},#{content_for(node, 'QtyOnHand')}")
      end

      quantity_tempfile.unlink
      tempfile.close

      tempfile.path
    end

    private

    def map_hash(node)
      {
        item_identifier: content_for(node, 'ItemNo'),
        quantity: content_for(node, 'QtyOnHand'),
        price: content_for(node, 'Price')
      }
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
lipseys-4.0.0 lib/lipseys/inventory.rb
lipseys-3.0.1 lib/lipseys/inventory.rb
lipseys-3.0.0 lib/lipseys/inventory.rb