Class: SEPA::Message
- Inherits:
-
Object
- Object
- SEPA::Message
- Includes:
- ActiveModel::Validations
- Defined in:
- lib/sepa_king/message.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#account ⇒ Object
readonly
Returns the value of attribute account.
-
#grouped_transactions ⇒ Object
readonly
Returns the value of attribute grouped_transactions.
-
#message_identification ⇒ Object
Unique identifer for the whole message.
Instance Method Summary collapse
- #add_transaction(options) ⇒ Object
- #amount_total(selected_transactions = transactions) ⇒ Object
-
#batch_id(transaction_reference) ⇒ Object
Returns the id of the batch to which the given transaction belongs Identified based upon the reference of the transaction.
- #batches ⇒ Object
-
#initialize(account_options = {}) ⇒ Message
constructor
A new instance of Message.
- #schema_compatible?(schema_name) ⇒ Boolean
-
#to_xml(schema_name = self.known_schemas.first) ⇒ String
Xml.
- #transactions ⇒ Object
Constructor Details
#initialize(account_options = {}) ⇒ Message
Returns a new instance of Message.
23 24 25 26 |
# File 'lib/sepa_king/message.rb', line 23 def initialize(={}) @grouped_transactions = {} @account = account_class.new() end |
Instance Attribute Details
#account ⇒ Object (readonly)
Returns the value of attribute account.
14 15 16 |
# File 'lib/sepa_king/message.rb', line 14 def account @account end |
#grouped_transactions ⇒ Object (readonly)
Returns the value of attribute grouped_transactions.
14 15 16 |
# File 'lib/sepa_king/message.rb', line 14 def grouped_transactions @grouped_transactions end |
#message_identification ⇒ Object
Unique identifer for the whole message
72 73 74 |
# File 'lib/sepa_king/message.rb', line 72 def @message_identification ||= "SEPA-KING/#{Time.now.to_i}" end |
Instance Method Details
#add_transaction(options) ⇒ Object
28 29 30 31 32 33 |
# File 'lib/sepa_king/message.rb', line 28 def add_transaction() transaction = transaction_class.new() raise ArgumentError.new(transaction.errors..join("\n")) unless transaction.valid? @grouped_transactions[transaction_group(transaction)] ||= [] @grouped_transactions[transaction_group(transaction)] << transaction end |
#amount_total(selected_transactions = transactions) ⇒ Object
54 55 56 |
# File 'lib/sepa_king/message.rb', line 54 def amount_total(selected_transactions=transactions) selected_transactions.inject(0) { |sum, t| sum + t.amount } end |
#batch_id(transaction_reference) ⇒ Object
Returns the id of the batch to which the given transaction belongs Identified based upon the reference of the transaction
78 79 80 81 82 83 84 |
# File 'lib/sepa_king/message.rb', line 78 def batch_id(transaction_reference) grouped_transactions.each do |group, transactions| if transactions.select { |transaction| transaction.reference == transaction_reference }.any? return payment_information_identification(group) end end end |
#batches ⇒ Object
86 87 88 |
# File 'lib/sepa_king/message.rb', line 86 def batches grouped_transactions.keys.collect { |group| payment_information_identification(group) } end |
#schema_compatible?(schema_name) ⇒ Boolean
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/sepa_king/message.rb', line 58 def schema_compatible?(schema_name) raise ArgumentError.new("Schema #{schema_name} is unknown!") unless self.known_schemas.include?(schema_name) case schema_name when PAIN_001_002_03, PAIN_008_002_02, PAIN_001_001_03 account.bic.present? && transactions.all? { |t| t.schema_compatible?(schema_name) } when PAIN_001_003_03, PAIN_008_003_02, PAIN_008_001_02 transactions.all? { |t| t.schema_compatible?(schema_name) } end end |
#to_xml(schema_name = self.known_schemas.first) ⇒ String
Returns xml.
40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/sepa_king/message.rb', line 40 def to_xml(schema_name=self.known_schemas.first) raise RuntimeError.new(errors..join("\n")) unless valid? raise RuntimeError.new("Incompatible with schema #{schema_name}!") unless schema_compatible?(schema_name) builder = Builder::XmlMarkup.new indent: 2 builder.instruct! builder.Document(xml_schema(schema_name)) do builder.__send__(xml_main_tag) do build_group_header(builder) build_payment_informations(builder) end end end |
#transactions ⇒ Object
35 36 37 |
# File 'lib/sepa_king/message.rb', line 35 def transactions grouped_transactions.values.flatten end |