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(options = {}, &block) ⇒ Object



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

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

Instance Method Details

#all(&block) ⇒ Object



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

def all(&block)
  connect(@options) do |ftp|
    begin
      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

      CSV.foreach(csv_tempfile, { col_sep: ';', quote_char: "\x00" }).each do |row|
        yield(process_row(row))
      end
    end

    csv_tempfile.unlink
    ftp.close
  end
end