Class: Zanders::Catalog

Inherits:
Base
  • Object
show all
Defined in:
lib/zanders/catalog.rb

Constant Summary collapse

CATALOG_FILENAME =
"zandersinv.xml"

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

connect, requires!

Constructor Details

#initialize(options = {}) ⇒ Catalog

Returns a new instance of Catalog.



6
7
8
9
10
11
12
13
# File 'lib/zanders/catalog.rb', line 6

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

  if options[:full_product].present?
    self.load_descriptions
  end
end

Class Method Details

.all(chunk_size = 15, options = {}, &block) ⇒ Object



15
16
17
18
# File 'lib/zanders/catalog.rb', line 15

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

Instance Method Details

#all(chunk_size, &block) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/zanders/catalog.rb', line 20

def all(chunk_size, &block)
  chunker   = Zanders::Chunker.new(chunk_size)
  tempfile  = get_file(CATALOG_FILENAME)
  xml_doc   = Nokogiri::XML(tempfile.open)

  xml_doc.xpath("//ZandersDataOut").each do |item|
    if chunker.is_full?
      yield(chunker.chunk)

      chunker.reset!
    else
      chunker.add(map_hash(item, @options[:full_product].present?))
    end
  end

  if chunker.chunk.count > 0
    yield(chunker.chunk)
  end

  tempfile.unlink
end