Class: WireClient::Message
- Inherits:
-
Object
- Object
- WireClient::Message
- Includes:
- ActiveModel::Validations
- Defined in:
- lib/wire_client/messages/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.
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
- #error_messages ⇒ Object
-
#initialize(account_options = {}) ⇒ Message
constructor
A new instance of Message.
-
#message_identification ⇒ Object
Get unique identifer for the message (with fallback to a random string).
-
#message_identification=(value) ⇒ Object
Set unique identifer for the message.
- #schema_compatible?(schema_name) ⇒ Boolean
-
#to_xml(schema_name = self.class.known_schemas.first) ⇒ String
Xml.
- #transactions ⇒ Object
Constructor Details
#initialize(account_options = {}) ⇒ Message
Returns a new instance of Message.
19 20 21 22 |
# File 'lib/wire_client/messages/message.rb', line 19 def initialize(={}) @grouped_transactions = {} @account = account_class.new() end |
Instance Attribute Details
#account ⇒ Object (readonly)
Returns the value of attribute account.
5 6 7 |
# File 'lib/wire_client/messages/message.rb', line 5 def account @account end |
#grouped_transactions ⇒ Object (readonly)
Returns the value of attribute grouped_transactions.
5 6 7 |
# File 'lib/wire_client/messages/message.rb', line 5 def grouped_transactions @grouped_transactions end |
Instance Method Details
#add_transaction(options) ⇒ Object
24 25 26 27 28 29 30 31 |
# File 'lib/wire_client/messages/message.rb', line 24 def add_transaction() transaction = transaction_class.new() unless transaction.valid? raise ArgumentError, transaction. end @grouped_transactions[transaction_group(transaction)] ||= [] @grouped_transactions[transaction_group(transaction)] << transaction end |
#amount_total(selected_transactions = transactions) ⇒ Object
54 55 56 |
# File 'lib/wire_client/messages/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
87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/wire_client/messages/message.rb', line 87 def batch_id(transaction_reference) grouped_transactions.each do |group, transactions| selected_transactions = begin transactions.select do |transaction| transaction.reference == transaction_reference end end if selected_transactions.any? return payment_information_identification(group) end end end |
#batches ⇒ Object
100 101 102 103 104 |
# File 'lib/wire_client/messages/message.rb', line 100 def batches grouped_transactions.keys.collect do |group| payment_information_identification(group) end end |
#error_messages ⇒ Object
106 107 108 |
# File 'lib/wire_client/messages/message.rb', line 106 def errors..join("\n") end |
#message_identification ⇒ Object
Get unique identifer for the message (with fallback to a random string)
81 82 83 |
# File 'lib/wire_client/messages/message.rb', line 81 def @message_identification ||= "WIRE#{SecureRandom.hex(5)}" end |
#message_identification=(value) ⇒ Object
Set unique identifer for the message
67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/wire_client/messages/message.rb', line 67 def (value) unless value.is_a?(String) raise ArgumentError, 'mesage_identification must be a string!' end regex = /\A([A-Za-z0-9]|[\+|\?|\/|\-|\:|\(|\)|\.|\,|\'|\ ]){1,35}\z/ unless value.match(regex) raise ArgumentError, "mesage_identification does not match #{regex}!" end @message_identification = value end |
#schema_compatible?(schema_name) ⇒ Boolean
58 59 60 61 62 63 64 |
# File 'lib/wire_client/messages/message.rb', line 58 def schema_compatible?(schema_name) unless self.known_schemas.include?(schema_name) raise ArgumentError, "Schema #{schema_name} is unknown!" end transactions.all? { |t| t.schema_compatible?(schema_name) } end |
#to_xml(schema_name = self.class.known_schemas.first) ⇒ String
Returns xml.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/wire_client/messages/message.rb', line 38 def to_xml(schema_name=self.class.known_schemas.first) raise(RuntimeError, errors..join("\n")) unless valid? unless schema_compatible?(schema_name) raise RuntimeError, "Incompatible with schema #{schema_name}!" end builder = Builder::XmlMarkup.new indent: 2 builder.instruct! :xml builder.Document(xml_schema(schema_name)) do builder.__send__(self.class.xml_main_tag) do build_group_header(builder) build_payment_information(builder) end end end |
#transactions ⇒ Object
33 34 35 |
# File 'lib/wire_client/messages/message.rb', line 33 def transactions grouped_transactions.values.flatten end |