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
# File 'lib/zanders/catalog.rb', line 6

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

Class Method Details

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



11
12
13
14
# File 'lib/zanders/catalog.rb', line 11

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



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/zanders/catalog.rb', line 16

def all(chunk_size, &block)
  chunker = Zanders::Chunker.new(chunk_size)

  connect(@options) do |ftp|
    begin
      csv_tempfile = Tempfile.new

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

      xml_doc = Nokogiri::XML(csv_tempfile)

      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

      csv_tempfile.unlink
    ensure
      ftp.close
    end
  end
end