class WireClient::Message

Attributes

account[R]
grouped_transactions[R]

Public Class Methods

new(account_options={}) click to toggle source
# File lib/wire_client/messages/message.rb, line 19
def initialize(account_options={})
  @grouped_transactions = {}
  @account = account_class.new(account_options)
end

Public Instance Methods

add_transaction(options) click to toggle source
# 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) click to toggle source
# 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) click to toggle source

Returns the id of the batch to which the given transaction belongs Identified based upon the reference of the transaction

# 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() click to toggle source
# 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() click to toggle source
# File lib/wire_client/messages/message.rb, line 106
def error_messages
  errors.full_messages.join("\n")
end
message_identification() click to toggle source

Get unique identifer for the message (with fallback to a random string)

# File lib/wire_client/messages/message.rb, line 81
def message_identification
  @message_identification ||= "WIRE#{SecureRandom.hex(5)}"
end
message_identification=(value) click to toggle source

Set unique identifer for the message

# 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) click to toggle source
# 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) click to toggle source

@return [String] xml

# 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
transactions() click to toggle source
# File lib/wire_client/messages/message.rb, line 33
def transactions
  grouped_transactions.values.flatten
end

Private Instance Methods

account_id(builder, account) click to toggle source
# File lib/wire_client/messages/message.rb, line 176
def account_id(builder, account)
  if account.iban
    builder.Id do
      builder.IBAN(account.iban)
    end
  else
    builder.Id do
      builder.Othr do
        builder.Id(account.account_number)
      end
    end
    builder.Tp do
      builder.Cd('CACC')
    end
    builder.Ccy(account.currency)
  end
end
build_group_header(builder) click to toggle source
# File lib/wire_client/messages/message.rb, line 122
def build_group_header(builder)
  builder.GrpHdr do
    builder.MsgId(message_identification)
    builder.CreDtTm(Time.now.iso8601)
    builder.NbOfTxs(transactions.length)
    builder.CtrlSum('%.2f' % amount_total)
    builder.InitgPty do
      builder.Nm(account.name)
      builder.Id do
        builder.OrgId do
          builder.Othr do
            builder.Id(account.identifier)
            builder.SchmeNm do
              builder.Cd(account.schema_code)
            end
          end
        end
      end
    end
  end
end
entity_address(builder, entity) click to toggle source
# File lib/wire_client/messages/message.rb, line 154
def entity_address(builder, entity)
  builder.StrtNm(entity.address_line)
  builder.PstCd(entity.postal_code)
  builder.TwnNm(entity.city)
  builder.CtrySubDvsn(entity.country_subdivision_abbr)
  builder.Ctry(entity.country)
  builder.AdrLine(entity.address_line)
end
entity_agent_id(builder, entity) click to toggle source
# File lib/wire_client/messages/message.rb, line 163
def entity_agent_id(builder, entity)
  if entity.bic
    builder.BIC(entity.bic)
  else
    builder.ClrSysMmbId do
      builder.ClrSysId do
        builder.Cd(entity.clear_system_code)
      end
      builder.MmbId(entity.wire_routing_number)
    end
  end
end
payment_information_identification(group) click to toggle source

Unique and consecutive identifier (used for the <PmntInf> blocks)

# File lib/wire_client/messages/message.rb, line 145
def payment_information_identification(group)
  "#{message_identification}#{grouped_transactions.keys.index(group) + 1}"
end
transaction_account_id(builder, transaction) click to toggle source
# File lib/wire_client/messages/message.rb, line 194
def transaction_account_id(builder, transaction)
  if transaction.iban
    builder.Id do
      builder.IBAN(transaction.iban)
    end
  else
    builder.Id do
      builder.Othr do
        builder.Id(transaction.account_number)
      end
    end
    builder.Tp do
      builder.Cd('CACC')
    end
  end
end
transaction_group(transaction) click to toggle source

Returns a key to determine the group to which the transaction belongs

# File lib/wire_client/messages/message.rb, line 150
def transaction_group(transaction)
  transaction
end
xml_schema(schema_name) click to toggle source

@return {Hash<Symbol=>String>} xml schema information used in output xml

# File lib/wire_client/messages/message.rb, line 113
def xml_schema(schema_name)
  urn = "urn:iso:std:iso:20022:tech:xsd:#{schema_name}"
  {
    :xmlns                => "#{urn}",
    :'xmlns:xsi'          => 'http://www.w3.org/2001/XMLSchema-instance',
    :'xsi:schemaLocation' => "#{urn} #{schema_name}.xsd"
  }
end