Class: RsrGroup::ResponseFile

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

Constant Summary

Constants inherited from Base

Base::DEFAULT_CATALOG_FILENAME, Base::DEFAULT_DIR, Base::KEYDEALER_CATALOG_FILENAME, Base::KEYDEALER_DIR, Base::MAP_FILENAME, Base::QTY_FILENAME

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

connect

Constructor Details

#initialize(options = {}) ⇒ ResponseFile

Returns a new instance of ResponseFile.



7
8
9
10
11
12
13
# File 'lib/rsr_group/response_file.rb', line 7

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

  @credentials    = options.select { |k, v| [:username, :password].include?(k) }
  @filename       = File.basename(options[:filename])
  @account_number = @filename.split('-')[2]
end

Instance Attribute Details

#contentObject Also known as: get_content

Returns the value of attribute content.



5
6
7
# File 'lib/rsr_group/response_file.rb', line 5

def content
  @content
end

#filenameObject (readonly)

Returns the value of attribute filename.



4
5
6
# File 'lib/rsr_group/response_file.rb', line 4

def filename
  @filename
end

#mtimeObject

Returns the value of attribute mtime.



5
6
7
# File 'lib/rsr_group/response_file.rb', line 5

def mtime
  @mtime
end

Class Method Details

.all(options = {}) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rsr_group/response_file.rb', line 41

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

  Base.connect(options) do |ftp|
    ftp.chdir(RsrGroup.config.response_dir)
    @resp = ftp.nlst("*.txt")
    ftp.close
  end

  @resp
end

.get_each(options = {}, &block) ⇒ Object



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

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

  Base.connect(options) do |ftp|
    ftp.chdir(RsrGroup.config.response_dir)

    @list = ftp.nlst("*.txt")
    @list.each do |file|
      resource         = new(options.merge(filename: file))
      resource.content = ftp.gettextfile(file, nil)
      resource.mtime   = ftp.mtime(file)
      yield(resource)
    end

    ftp.close
  end

  @list  
end

Instance Method Details

#response_typeObject



64
65
66
# File 'lib/rsr_group/response_file.rb', line 64

def response_type
  FILE_TYPES[@filename.split("-").first]
end

#to_jsonObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/rsr_group/response_file.rb', line 68

def to_json
  get_content

  if @content.length == 0
    raise ZeroByteFile.new("File is empty (filename: #{@filename})")
  end

  @json = {
    response_type: response_type,
    filename: @filename,
    account_number: @account_number,
    rsr_order_number: nil,
    details: [],
    errors: [],
  }

  return parse_eerr  if error?
  return parse_econf if confirmation?
  return parse_eship if shipping?
end