Class: WireClient::Message

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Validations
Defined in:
lib/wire_client/messages/message.rb

Direct Known Subclasses

CreditTransfer, DirectDebit

Instance Attribute Summary collapse

Instance Method Summary collapse

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 = .new()
end

Instance Attribute Details

#accountObject (readonly)

Returns the value of attribute account.



5
6
7
# File 'lib/wire_client/messages/message.rb', line 5

def 
  @account
end

#grouped_transactionsObject (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(options)
  transaction = transaction_class.new(options)
  unless transaction.valid?
    raise ArgumentError, transaction.error_messages
  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

#batchesObject



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_messagesObject



106
107
108
# File 'lib/wire_client/messages/message.rb', line 106

def error_messages
  errors.full_messages.join("\n")
end

#message_identificationObject

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
  @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 message_identification=(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

Returns:

  • (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.

Returns:

  • (String)

    xml

Raises:

  • (RuntimeError)


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.full_messages.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

#transactionsObject



33
34
35
# File 'lib/wire_client/messages/message.rb', line 33

def transactions
  grouped_transactions.values.flatten
end