Class: RsrGroup::Catalog

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

Constant Summary collapse

KEYDEALER_DIR =
'keydealer'.freeze
INVENTORY_DIR =
'ftpdownloads'.freeze
INVENTORY_FILENAME =
'rsrinventory-new.txt'.freeze
KEYDEALER_FILENAME =
'rsrinventory-keydlr-new.txt'.freeze

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

connect

Constructor Details

#initialize(options = {}) ⇒ Catalog

Returns a new instance of Catalog.



9
10
11
12
13
# File 'lib/rsr_group/catalog.rb', line 9

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

  @options = options
end

Class Method Details

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



15
16
17
18
# File 'lib/rsr_group/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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/rsr_group/catalog.rb', line 20

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

      if ftp.nlst.include?(KEYDEALER_DIR)
        ftp.chdir(KEYDEALER_DIR)
        ftp.getbinaryfile(KEYDEALER_FILENAME, csv_tempfile.path)
      else
        ftp.chdir(INVENTORY_DIR)
        ftp.getbinaryfile(INVENTORY_FILENAME, csv_tempfile.path)
      end

      chunker.total_count = File.readlines(csv_tempfile).size

      CSV.readlines(csv_tempfile, col_sep: ';', quote_char: "\x00").to_enum.with_index(1).each do |row, current_line|
        if chunker.is_full?
          yield(chunker.chunk)

          chunker.reset
        elsif chunker.is_complete?
          yield(chunker.chunk)

          break
        else
          chunker.add(process_row(row))
        end
      end
    end

    csv_tempfile.unlink
    ftp.close
  end
end