Class: Camt::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/camt/parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#fileObject

Returns the value of attribute file.



4
5
6
# File 'lib/camt/parser.rb', line 4

def file
  @file
end

Instance Method Details

#parse(file) ⇒ Object



44
45
46
47
48
# File 'lib/camt/parser.rb', line 44

def parse file
  self.file = file
  file.doc.remove_namespaces!
  file.doc.xpath('//BkToCstmrStmt').map{|node| parse_message node }
end

#parse_message(node) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/camt/parser.rb', line 15

def parse_message node
  group_header_node = node.at('./GrpHdr')

  group_header = GroupHeader.new
  group_header.message_id       = group_header_node.at('./MsgId').text
  group_header.created_at       = Time.parse(group_header_node.at('./CreDtTm').text)
  group_header.additional_info  = group_header_node.at('./AddtlInf').try(:text)

  if recipient_node = group_header_node.at('./MsgRcpt')
    group_header.recipient = {
      name: recipient_node.at('./Nm').try(:text),
      postal_address: recipient_node.at('./PstlAdr').try(:text),
      identification: recipient_node.at('./Id').try(:text),
      country_of_residence: recipient_node.at('./CtryOfRes').try(:text),
      contact_details: recipient_node.at('./CtctDtls').try(:text)
    }
  end

  if pagination_node = group_header_node.at('./MsgPgntn')
    group_header.pagination = { page: pagination_node.at('./PgNb').text, last_page: (pagination_node.at('./LastPgInd').text == 'true') }
  end

  message = Message.new
  message.group_header = group_header
  message.statements = node.xpath('./Stmt').map{|node| parse_Stmt node }

  message
end

#parse_Stmt(node) ⇒ Object



6
7
8
9
10
11
12
13
# File 'lib/camt/parser.rb', line 6

def parse_Stmt(node)
  # Parse a single Stmt node.
  #
  # Be sure to craft a unique, but short enough statement identifier,
  # as it is used as the basis of the generated move lines' names
  # which overflow when using the full IBAN and CAMT statement id.
  Statement.new(node, file.country_code)
end