Class: SPS::Message
- Inherits:
-
Object
- Object
- SPS::Message
- Includes:
- ActiveModel::Validations
- Defined in:
- lib/sps_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.
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
-
#creation_date_time ⇒ Object
Get creation date time for the message (with fallback to Time.now.iso8601).
-
#creation_date_time=(value) ⇒ Object
Set creation date time for the message p.s.
-
#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.known_schemas.first, encoding = 'UTF-8') ⇒ String
Xml.
- #transactions ⇒ Object
Constructor Details
#initialize(account_options = {}) ⇒ Message
Returns a new instance of Message.
22 23 24 25 |
# File 'lib/sps_king/message.rb', line 22 def initialize( = {}) @grouped_transactions = {} @account = account_class.new() end |
Instance Attribute Details
#account ⇒ Object (readonly)
Returns the value of attribute account.
13 14 15 |
# File 'lib/sps_king/message.rb', line 13 def account @account end |
#grouped_transactions ⇒ Object (readonly)
Returns the value of attribute grouped_transactions.
13 14 15 |
# File 'lib/sps_king/message.rb', line 13 def grouped_transactions @grouped_transactions end |
Instance Method Details
#add_transaction(options) ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/sps_king/message.rb', line 27 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
57 58 59 |
# File 'lib/sps_king/message.rb', line 57 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
107 108 109 110 111 112 113 |
# File 'lib/sps_king/message.rb', line 107 def batch_id(transaction_reference) grouped_transactions.each do |group, transactions| if transactions.any? { |transaction| transaction.reference == transaction_reference } return payment_information_identification(group) end end end |
#batches ⇒ Object
115 116 117 |
# File 'lib/sps_king/message.rb', line 115 def batches grouped_transactions.keys.collect { |group| payment_information_identification(group) } end |
#creation_date_time ⇒ Object
Get creation date time for the message (with fallback to Time.now.iso8601)
101 102 103 |
# File 'lib/sps_king/message.rb', line 101 def creation_date_time @creation_date_time ||= Time.now.iso8601 end |
#creation_date_time=(value) ⇒ Object
91 92 93 94 95 96 97 98 |
# File 'lib/sps_king/message.rb', line 91 def creation_date_time=(value) raise ArgumentError.new('creation_date_time must be a string!') unless value.is_a?(String) regex = /[0-9]{4}[-][0-9]{2,2}[-][0-9]{2,2}(?:\s|T)[0-9]{2,2}[:][0-9]{2,2}[:][0-9]{2,2}/ raise ArgumentError.new("creation_date_time does not match #{regex}!") unless value.match(regex) @creation_date_time = value end |
#message_identification ⇒ Object
Get unique identifer for the message (with fallback to a random string)
84 85 86 |
# File 'lib/sps_king/message.rb', line 84 def @message_identification ||= "SPS-KING/#{SecureRandom.hex(11)}" end |
#message_identification=(value) ⇒ Object
Set unique identifer for the message
74 75 76 77 78 79 80 81 |
# File 'lib/sps_king/message.rb', line 74 def (value) raise ArgumentError.new('message_identification must be a string!') unless value.is_a?(String) regex = /\A([A-Za-z0-9]|[\+|\?|\/|\-|\:|\(|\)|\.|\,|\'|\ ]){1,35}\z/ raise ArgumentError.new("message_identification does not match #{regex}!") unless value.match(regex) @message_identification = value end |
#schema_compatible?(schema_name) ⇒ Boolean
61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/sps_king/message.rb', line 61 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_001_03_CH_02, PAIN_001_001_09_CH_03 transactions.all? { |t| t.schema_compatible?(schema_name) } when PAIN_008_001_02_CH_03 transactions.all? { |t| t.schema_compatible?(schema_name) } && !account.iban.to_s.match(/^(CH|LI)/).nil? # Only allowed for switzerland or liechtenstein end end |
#to_xml(schema_name = self.known_schemas.first, encoding = 'UTF-8') ⇒ String
Returns xml.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/sps_king/message.rb', line 40 def to_xml(schema_name = self.known_schemas.first, encoding = 'UTF-8') raise SPS::Error.new(errors..join("\n")) unless valid? # raise SPS::Error.new("Incompatible with schema #{schema_name}!") unless schema_compatible?(schema_name) builder = Nokogiri::XML::Builder.new(encoding: encoding) do |builder| builder.Document(xml_schema(schema_name)) do builder.__send__(xml_main_tag) do build_group_header(builder) build_payment_informations(builder, schema_name) end end end validate_final_document!(builder.doc, schema_name) builder.to_xml end |
#transactions ⇒ Object
35 36 37 |
# File 'lib/sps_king/message.rb', line 35 def transactions grouped_transactions.values.flatten end |