Class: Absa::H2h::Transmission::Set

Inherits:
Object
  • Object
show all
Defined in:
lib/absa-h2h/transmission/set.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSet

Returns a new instance of Set.



6
7
8
# File 'lib/absa-h2h/transmission/set.rb', line 6

def initialize
  self.records = []
end

Instance Attribute Details

#recordsObject

Returns the value of attribute records.



4
5
6
# File 'lib/absa-h2h/transmission/set.rb', line 4

def records
  @records
end

Class Method Details

.build(data) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/absa-h2h/transmission/set.rb', line 10

def self.build(data)
  set = self.new
  
  data.each do |hash|
    if hash[:data].is_a? Array
      klass = "Absa::H2h::Transmission::#{hash[:type].capitalize.camelize}".constantize
      set.records << klass.build(hash[:data])
    else
      klass = "Absa::H2h::Transmission::#{self.partial_class_name}::#{hash[:type].capitalize.camelize}".constantize
      set.records << klass.new(hash[:data])
    end
  end
  
  set.validate!
  set
end

.for_record(record, transmission_type) ⇒ Object

move this logic to yml file



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/absa-h2h/transmission/set.rb', line 49

def self.for_record(record, transmission_type) # move this logic to yml file
  record_id = record[0..2]
  
  case record_id
  when '000','999'
    return Absa::H2h::Transmission::Document
  when '030','031','039'
    return (transmission_type == "output") ? Absa::H2h::Transmission::AccountHolderVerificationOutput : Absa::H2h::Transmission::AccountHolderVerification
  when '001', '020'
    return Absa::H2h::Transmission::Eft
  when '010','019'
    return Absa::H2h::Transmission::EftOutput
  when '011','013','014'
    return Absa::H2h::Transmission::EftUnpaid
  when '016','017','018'
    return Absa::H2h::Transmission::EftRedirect
  when '900','901','902','903'
    return Absa::H2h::Transmission::Reply
  end
end

.hash_from_s(string, transmission_type) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/absa-h2h/transmission/set.rb', line 109

def self.hash_from_s(string, transmission_type)
  set_info = {type: self.partial_class_name.underscore, data: []}
  lines = string.split(/^/)

  # look for rec_ids, split into chunks, and pass each related class a piece of string
  
  buffer = []
  current_set = nil
  subset = nil
 
  lines.each do |line|
    if Set.for_record(line, transmission_type) == self
      if subset && (buffer.length > 0)
        set_info[:data] << subset.hash_from_s(buffer.join, transmission_type)
        buffer = []
        subset = nil
      end
      
      record = line[0, 198]
      set_info[:data] << self.process_record(record)
    else
      subset = Set.for_record(line, transmission_type) unless subset        
      buffer << line
      
      if self.is_trailer_record?(subset, line)
        set_info[:data] << subset.hash_from_s(buffer.join, transmission_type)
        buffer = []
        subset = nil
      end
    end
  end

  set_info
end

.is_trailer_record?(set, record) ⇒ Boolean

Returns:

  • (Boolean)


87
88
89
90
91
# File 'lib/absa-h2h/transmission/set.rb', line 87

def self.is_trailer_record?(set, record)
  record_id = record[0,3]
  return true if set == Absa::H2h::Transmission::Eft and (["001","020"].include? record_id) and record[4,2] == "92"
  self.trailer_id(set) == record_id
end

.layout_rulesObject



156
157
158
159
160
# File 'lib/absa-h2h/transmission/set.rb', line 156

def self.layout_rules
  file_name = "#{Absa::H2h::CONFIG_DIR}/#{self.partial_class_name.underscore}.yml"
  
  YAML.load(File.open(file_name))
end

.module_nameObject



148
149
150
# File 'lib/absa-h2h/transmission/set.rb', line 148

def self.module_name
  self.name.split("::")[0..-1].join("::")
end

.partial_class_nameObject



152
153
154
# File 'lib/absa-h2h/transmission/set.rb', line 152

def self.partial_class_name
  self.name.split("::")[-1]
end

.process_record(record) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/absa-h2h/transmission/set.rb', line 93

def self.process_record(record)
  record_info = {}
  
  self.record_types.each do |record_type|
    klass = "#{self.name}::#{record_type.camelize}".constantize

    if klass.matches_definition?(record)
      options = klass.string_to_hash(record)
      record_info = {type: record_type, data: options}
      break
    end
  end
  
  record_info
end

.record_type(record_type) ⇒ Object



162
163
164
# File 'lib/absa-h2h/transmission/set.rb', line 162

def self.record_type(record_type)
  "#{self.name}::#{record_type.camelize}".constantize
end

.record_typesObject



144
145
146
# File 'lib/absa-h2h/transmission/set.rb', line 144

def self.record_types
  self.layout_rules.map {|k,v| k}
end

.trailer_id(klass) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/absa-h2h/transmission/set.rb', line 70

def self.trailer_id(klass)
  case klass.name
  when 'Absa::H2h::Transmission::Document'
    return '999'
  when 'Absa::H2h::Transmission::AccountHolderVerification'
    return '039'
  when 'Absa::H2h::Transmission::AccountHolderVerificationOutput'
    return '039'
  when 'Absa::H2h::Transmission::EftOutput'
    return '019'
  when 'Absa::H2h::Transmission::EftUnpaid'
    return '014'
  when 'Absa::H2h::Transmission::EftRedirect'
    return '018'
  end
end

Instance Method Details

#headerObject



27
28
29
# File 'lib/absa-h2h/transmission/set.rb', line 27

def header
  records[0]
end

#to_sObject



43
44
45
46
47
# File 'lib/absa-h2h/transmission/set.rb', line 43

def to_s
  string = ""
  records.each {|record| string += record.to_s }
  string
end

#trailerObject



31
32
33
# File 'lib/absa-h2h/transmission/set.rb', line 31

def trailer
  records[-1]
end

#transactionsObject



35
36
37
# File 'lib/absa-h2h/transmission/set.rb', line 35

def transactions
  records[1..-2]
end

#validate!Object



39
40
41
# File 'lib/absa-h2h/transmission/set.rb', line 39

def validate!
  
end