Class: Sepa::Base

Inherits:
Object
  • Object
show all
Includes:
Aduki::Initializer
Defined in:
lib/sepa/base.rb

Direct Known Subclasses

PaymentsInitiation::AccountIdentificationChoice, PaymentsInitiation::AccountSchemeNameChoice, PaymentsInitiation::Authorisation, PaymentsInitiation::BranchAndFinancialInstitutionIdentification, PaymentsInitiation::CashAccount, PaymentsInitiation::CashAccountTypeChoice, PaymentsInitiation::CategoryPurposeChoice, PaymentsInitiation::ContactDetails, PaymentsInitiation::FinancialIdentificationSchemeName1Choice, PaymentsInitiation::FinancialInstitutionIdentification, PaymentsInitiation::GenericAccountIdentification, PaymentsInitiation::GenericFinancialIdentification1, PaymentsInitiation::GenericOrganisationIdentification1, PaymentsInitiation::GenericPersonIdentification1, PaymentsInitiation::LocalInstrumentChoice, PaymentsInitiation::OrganisationIdentification, PaymentsInitiation::OrganisationIdentificationSchemeName1Choice, PaymentsInitiation::Pain00800104::CustomerDirectDebitInitiation, PaymentsInitiation::Pain00800104::DirectDebitTransaction, PaymentsInitiation::Pain00800104::DirectDebitTransactionInformation, PaymentsInitiation::Pain00800104::GroupHeader, PaymentsInitiation::Pain00800104::MandateRelatedInformation, PaymentsInitiation::Pain00800104::PaymentIdentification, PaymentsInitiation::Pain00800104::PaymentInformation, PaymentsInitiation::Pain00800104::PaymentTypeInformation, PaymentsInitiation::Pain00800104::PurposeChoice, PaymentsInitiation::Pain00800104::RegulatoryAuthority, PaymentsInitiation::Pain00800104::RegulatoryReporting, PaymentsInitiation::Pain00800104::RemittanceInformationChoice, PaymentsInitiation::Pain00800104::ServiceLevelChoice, PaymentsInitiation::Pain00800104::StructuredRegulatoryReporting, PaymentsInitiation::Pain00800104::TaxAuthorisation, PaymentsInitiation::Pain00800104::TaxInformation, PaymentsInitiation::Pain00800104::TaxParty, PaymentsInitiation::PartyChoiceIdentification, PaymentsInitiation::PartyIdentification, PaymentsInitiation::PersonIdentificationSchemeName1Choice, PaymentsInitiation::PostalAddress, PaymentsInitiation::PrivateIdentification

Constant Summary collapse

@@time_format =
"%Y-%m-%dT%H:%M:%SZ"
@@attribute_defs =
Hash.new { |h,k| h[k] = [] }

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.array_attribute(name, tag, member_type = nil, options = { }) ⇒ Object



130
131
132
133
134
# File 'lib/sepa/base.rb', line 130

def self.array_attribute name, tag, member_type=nil, options={ }
  attribute_defs << [name, { :tag => tag, :type => :[], :member_type => member_type, :options => options }]
  attr_accessor name
  aduki(name => member_type) if member_type
end

.attribute(name, tag, type = :string, member_type = nil, options = { }) ⇒ Object



113
114
115
116
117
118
119
120
121
122
# File 'lib/sepa/base.rb', line 113

def self.attribute name, tag, type=:string, member_type=nil, options={ }
  if type == :[]
    array_attribute name, tag, member_type, options
  elsif type.is_a?(Class) && type != Time && type != Date
    typed_attribute name, tag, type, options
  else
    attr_accessor name
    attribute_defs << [name, { :tag => tag, :type => type, :options => options }]
  end
end

.attribute_defsObject



97
98
99
# File 'lib/sepa/base.rb', line 97

def self.attribute_defs
  @@attribute_defs[self]
end

.attribute_defs=(arg) ⇒ Object



101
102
103
# File 'lib/sepa/base.rb', line 101

def self.attribute_defs= arg
  @@attribute_defs[self]= arg
end

.code_or_proprietaryObject



108
109
110
111
# File 'lib/sepa/base.rb', line 108

def self.code_or_proprietary
  attribute :code       , "Cd"
  attribute :proprietary, "Prtry"
end

.definition(txt) ⇒ Object



105
106
# File 'lib/sepa/base.rb', line 105

def self.definition txt
end

.time_format=(new_format) ⇒ Object



136
137
138
# File 'lib/sepa/base.rb', line 136

def self.time_format= new_format
  @@time_format = new_format
end

.typed_attribute(name, tag, type, options) ⇒ Object



124
125
126
127
128
# File 'lib/sepa/base.rb', line 124

def self.typed_attribute name, tag, type, options
  attribute_defs << [name, { :tag => tag, :type => type, :options => options }]
  attr_accessor name
  aduki name => type
end

Instance Method Details

#build_xml_attributes(names) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/sepa/base.rb', line 31

def build_xml_attributes names
  result = { }
  (names || { }).each { |k,v|
    result[k] = self.send v
  }
  result
end

#empty?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/sepa/base.rb', line 39

def empty?
  false
end

#empty_item?(item) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/sepa/base.rb', line 65

def empty_item? item
  item == nil || (item.respond_to?(:empty?) && item.empty?)
end

#normalize(str) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/sepa/base.rb', line 43

def normalize str
  # Integer like Fixnum and Bignum can directly be returned
  return str if str.is_a? Integer
  # Check for Numeric to exclude String, but handle Float, BigDecimal, Rational, ... we don't mind.
  # Kernel#sprintf could handle all to_f implementers, if to_f does not throw an exception
  return ('%.2f' % str) if str.is_a?(Numeric) && str.respond_to?(:to_f)
  # For String we need to replace illegal characters by an appropriate alternative
  replacements = {
      'à' => 'a', 'é' => 'e', 'è' => 'e',
      'û' => 'u', 'î' => 'i', 'ô' => 'o', 'ü' => 'u', 'ï' => 'i', 'ö' => 'o',
      'ÿ' => 'y', 'ç' => 'c', 'Ç' => 'C', 'É' => 'E', 'È' => 'E', 'á' => 'a',
      'À' => 'A', 'Á' => 'A', 'Ü' => 'U', 'Ï' => 'I', 'Ö' => 'O', 'ß' => 'ss'
  }
  str = replacements.to_a.
    inject(str.to_s) { |s, kv| s.gsub(kv[0], kv[1]) }.
    gsub(/[^a-zA-Z0-9_@ \.,()'+\/\?-]/, '')
end

#string_type?(type_def) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/sepa/base.rb', line 61

def string_type? type_def
  (type_def == :string) || (type_def.is_a?(Class) && type_def < StringWithConstraint)
end

#to_xml(builder) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/sepa/base.rb', line 69

def to_xml builder
  self.class.attribute_defs.each do |name, meta|
    item = self.send(name)
    options = meta[:options] || { }
    attributes = build_xml_attributes options[:attributes]
    next if empty_item? item
    if string_type?(meta[:type])
      builder.__send__(meta[:tag], normalize(item), attributes)
    elsif meta[:type] == :[]
      if meta[:member_type] == nil || string_type?(meta[:member_type])
        item.each { |obj| builder.__send__(meta[:tag], normalize(obj), attributes) }
      else
        item.each do |obj|
          builder.__send__(meta[:tag], attributes) { obj.to_xml builder }
        end
      end
    elsif meta[:type] == Time
      v = item.is_a?(String) ? item : item.strftime(@@time_format)
      builder.__send__(meta[:tag], v, attributes)
    elsif meta[:type] == Date
      v = item.is_a?(String) ? item : item.strftime("%Y-%m-%d")
      builder.__send__(meta[:tag], v, attributes)
    elsif meta[:type].is_a? Class
      builder.__send__(meta[:tag], attributes) { item.to_xml builder }
    end
  end
end