Class: Cmxl::Statement

Inherits:
Object
  • Object
show all
Defined in:
lib/cmxl/statement.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#collectionObject

Returns the value of attribute collection.



4
5
6
# File 'lib/cmxl/statement.rb', line 4

def collection
  @collection
end

#fieldsObject

Returns the value of attribute fields.



4
5
6
# File 'lib/cmxl/statement.rb', line 4

def fields
  @fields
end

#linesObject

Returns the value of attribute lines.



4
5
6
# File 'lib/cmxl/statement.rb', line 4

def lines
  @lines
end

#sourceObject

Returns the value of attribute source.



4
5
6
# File 'lib/cmxl/statement.rb', line 4

def source
  @source
end

Instance Method Details

#account_identificationObject



65
66
67
# File 'lib/cmxl/statement.rb', line 65

def 
  field(25)
end

#available_balanceObject



85
86
87
# File 'lib/cmxl/statement.rb', line 85

def available_balance
  field(64)
end

#closing_balanceObject



77
78
79
# File 'lib/cmxl/statement.rb', line 77

def closing_balance
  field(62, 'F')
end

#closing_or_intermediary_balanceObject



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_dateObject

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


89
90
91
# File 'lib/cmxl/statement.rb', line 89

def legal_sequence_number
  field(28).source
end

#mt940_hashObject



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' => .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

Returns:

  • (Boolean)


101
102
103
# File 'lib/cmxl/statement.rb', line 101

def mt942?
  fields.any? { |field| field.is_a? Fields::FloorLimitIndicator }
end

#mt942_hashObject



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' => .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_balanceObject



69
70
71
# File 'lib/cmxl/statement.rb', line 69

def opening_balance
  field(60, 'F')
end

#opening_or_intermediary_balanceObject



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

#referenceObject



56
57
58
# File 'lib/cmxl/statement.rb', line 56

def reference
  field(20).reference
end

#shaObject

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_hObject



105
106
107
# File 'lib/cmxl/statement.rb', line 105

def to_h
  mt942? ? mt942_hash : mt940_hash
end

#to_hashObject



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

#transactionsObject



20
21
22
# File 'lib/cmxl/statement.rb', line 20

def transactions
  fields.select { |field| field.is_a?(Fields::Transaction) }
end

#vmk_credit_summaryObject



93
94
95
# File 'lib/cmxl/statement.rb', line 93

def vmk_credit_summary
  field(90, 'C')
end

#vmk_debit_summaryObject



97
98
99
# File 'lib/cmxl/statement.rb', line 97

def vmk_debit_summary
  field(90, 'D')
end