Class: Cmxl::Statement
- Inherits:
-
Object
- Object
- Cmxl::Statement
- Defined in:
- lib/cmxl/statement.rb
Instance Attribute Summary collapse
-
#collection ⇒ Object
Returns the value of attribute collection.
-
#fields ⇒ Object
Returns the value of attribute fields.
-
#lines ⇒ Object
Returns the value of attribute lines.
-
#source ⇒ Object
Returns the value of attribute source.
Instance Method Summary collapse
- #account_identification ⇒ Object
- #available_balance ⇒ Object
- #closing_balance ⇒ Object
- #closing_or_intermediary_balance ⇒ Object
-
#field(tag, modifier = nil) ⇒ Object
Internal: Field accessor returns a field object by a given tag.
-
#generation_date ⇒ Object
Get generation date from field 20.
-
#initialize(source) ⇒ Statement
constructor
Public: Initiate a new Statement and parse a provided single statement string It directly parses the source and initiates file and transaction objects.
- #legal_sequence_number ⇒ Object
- #mt940_hash ⇒ Object
- #mt942? ⇒ Boolean
- #mt942_hash ⇒ Object
- #opening_balance ⇒ Object
- #opening_or_intermediary_balance ⇒ Object
-
#parse! ⇒ Object
Internal: Parse a single MT940 statement and extract the line data.
- #reference ⇒ Object
-
#sha ⇒ Object
Public: SHA2 of the provided source This is an experiment of trying to identify statements.
- #strip_headers! ⇒ Object
- #to_h ⇒ Object
- #to_hash ⇒ Object
- #to_json(*args) ⇒ Object
- #transactions ⇒ Object
- #vmk_credit_summary ⇒ Object
- #vmk_debit_summary ⇒ Object
Constructor Details
#initialize(source) ⇒ Statement
Public: Initiate a new Statement and parse a provided single statement string It directly parses the source and initiates file and transaction objects.
Example:
Cmxl::Statement.new(single_statement_string)
12 13 14 15 16 17 18 |
# File 'lib/cmxl/statement.rb', line 12 def initialize(source) self.source = source self.fields = [] self.lines = [] strip_headers! if Cmxl.config[:strip_headers] parse! end |
Instance Attribute Details
#collection ⇒ Object
Returns the value of attribute collection.
4 5 6 |
# File 'lib/cmxl/statement.rb', line 4 def collection @collection end |
#fields ⇒ Object
Returns the value of attribute fields.
4 5 6 |
# File 'lib/cmxl/statement.rb', line 4 def fields @fields end |
#lines ⇒ Object
Returns the value of attribute lines.
4 5 6 |
# File 'lib/cmxl/statement.rb', line 4 def lines @lines end |
#source ⇒ Object
Returns the value of attribute source.
4 5 6 |
# File 'lib/cmxl/statement.rb', line 4 def source @source end |
Instance Method Details
#account_identification ⇒ Object
65 66 67 |
# File 'lib/cmxl/statement.rb', line 65 def account_identification field(25) end |
#available_balance ⇒ Object
85 86 87 |
# File 'lib/cmxl/statement.rb', line 85 def available_balance field(64) end |
#closing_balance ⇒ Object
77 78 79 |
# File 'lib/cmxl/statement.rb', line 77 def closing_balance field(62, 'F') end |
#closing_or_intermediary_balance ⇒ Object
81 82 83 |
# File 'lib/cmxl/statement.rb', line 81 def closing_or_intermediary_balance field(62) end |
#field(tag, modifier = nil) ⇒ Object
Internal: Field accessor returns a field object by a given tag
Example: field(20) field(61,‘F’)
150 151 152 |
# File 'lib/cmxl/statement.rb', line 150 def field(tag, modifier = nil) fields.detect { |field| field.tag == tag.to_s && (modifier.nil? || field.modifier == modifier) } end |
#generation_date ⇒ Object
Get generation date from field 20. If generation date is not provided in field 20, method will fall back to field 13 if present.
61 62 63 |
# File 'lib/cmxl/statement.rb', line 61 def generation_date field(20).date || (field(13).nil? ? nil : field(13).date) end |
#legal_sequence_number ⇒ Object
89 90 91 |
# File 'lib/cmxl/statement.rb', line 89 def legal_sequence_number field(28).source end |
#mt940_hash ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/cmxl/statement.rb', line 109 def mt940_hash { 'reference' => reference, 'sha' => sha, 'generation_date' => generation_date, 'account_identification' => account_identification.to_h, 'opening_balance' => opening_balance.to_h, 'closing_balance' => closing_balance.to_h, 'available_balance' => available_balance.to_h, 'transactions' => transactions.map(&:to_h), 'fields' => fields.map(&:to_h) } end |
#mt942? ⇒ Boolean
101 102 103 |
# File 'lib/cmxl/statement.rb', line 101 def mt942? fields.any? { |field| field.is_a? Fields::FloorLimitIndicator } end |
#mt942_hash ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/cmxl/statement.rb', line 123 def mt942_hash { 'reference' => reference, 'sha' => sha, 'generation_date' => generation_date, 'account_identification' => account_identification.to_h, 'debit_summary' => vmk_debit_summary.to_h, 'credit_summary' => vmk_credit_summary.to_h, 'transactions' => transactions.map(&:to_h), 'fields' => fields.map(&:to_h) } end |
#opening_balance ⇒ Object
69 70 71 |
# File 'lib/cmxl/statement.rb', line 69 def opening_balance field(60, 'F') end |
#opening_or_intermediary_balance ⇒ Object
73 74 75 |
# File 'lib/cmxl/statement.rb', line 73 def opening_or_intermediary_balance field(60) end |
#parse! ⇒ Object
Internal: Parse a single MT940 statement and extract the line data
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/cmxl/statement.rb', line 26 def parse! self.fields = [] lines = source.split(/(^:[0-9A-Z]{2,3}:)/m).reject(&:empty?).each_slice(2).map(&:join) lines.map do |line| if line =~ /\A:86:/ if field = fields.last field.(line) end else field = Field.parse(line) fields << field unless field.nil? end end end |
#reference ⇒ Object
56 57 58 |
# File 'lib/cmxl/statement.rb', line 56 def reference field(20).reference end |
#sha ⇒ Object
Public: SHA2 of the provided source This is an experiment of trying to identify statements. The MT940 itself might not provide a unique identifier
Returns the SHA2 of the source
52 53 54 |
# File 'lib/cmxl/statement.rb', line 52 def sha Digest::SHA2.new.update(source).to_s end |
#strip_headers! ⇒ Object
43 44 45 46 |
# File 'lib/cmxl/statement.rb', line 43 def strip_headers! source.gsub!(/\A.*?(?=^:)/m, '') # beginning: strip every line in the beginning that does not start with a : source.strip! end |
#to_h ⇒ Object
105 106 107 |
# File 'lib/cmxl/statement.rb', line 105 def to_h mt942? ? mt942_hash : mt940_hash end |
#to_hash ⇒ Object
136 137 138 |
# File 'lib/cmxl/statement.rb', line 136 def to_hash to_h end |
#to_json(*args) ⇒ Object
140 141 142 |
# File 'lib/cmxl/statement.rb', line 140 def to_json(*args) to_h.to_json(*args) end |
#transactions ⇒ Object
20 21 22 |
# File 'lib/cmxl/statement.rb', line 20 def transactions fields.select { |field| field.is_a?(Fields::Transaction) } end |
#vmk_credit_summary ⇒ Object
93 94 95 |
# File 'lib/cmxl/statement.rb', line 93 def vmk_credit_summary field(90, 'C') end |
#vmk_debit_summary ⇒ Object
97 98 99 |
# File 'lib/cmxl/statement.rb', line 97 def vmk_debit_summary field(90, 'D') end |