Class: ISO::SWIFT

Inherits:
Object
  • Object
show all
Defined in:
lib/iso/swift.rb

Overview

SWIFT

Usage

require 'iso/swift'
==== new instance - valid SWIFT code
swift = ISO::SWIFT.new('PSST FR PP SCE') # => #<ISO::SWIFT:0x007fb4a393e220 @data={"formatted"=>"PSSTFRPPSCE", "bank_code"=>"PSST", "country_code"=>"FR", "country_name"=>"France", "location_code"=>"PP", "branch_code"=>"SCE", "bank_name"=>"LA BANQUE POSTALE", "location_name"=>"ORLEANS", "branch_name"=>"CENTRE FINANCIER DORLEANS LA SOURCE"}, @errors=[]>
==== validation
swift.valid? # => true
swift.errors # => []
==== attributes
swift.original # => "PSST FR PP SCE"
swift.formatted # => "PSSTFRPPSCE"
swift.bank_code # => "PSST"
swift.bank_name # => "LA BANQUE POSTALE"
swift.country_code # => "FR"
swift.country_name # => "France"
swift.location_code # => "PP"
swift.location_name # => "ORLEANS"
swift.branch_code # => "SCE"
swift.branch_name # => "CENTRE FINANCIER DORLEANS LA SOURCE"

==== new instance - invalid SWIFT code
swift = ISO::SWIFT.new('PSSTFRCEE') # #<ISO::SWIFT:0x007f8abe708820 @data={}, @errors=[:bad_format]>
==== validation
swift.valid? # => false
swift.errors # => [:bad_format]

Constant Summary collapse

Regex =

Swift regular expression

/^[A-Z]{4}[A-Z]{2}[A-Z0-9]{2}([A-Z0-9]{3})?$/
AttrReaders =

Attributes

[
  :original,
  :formatted,
  :bank_code,
  :bank_name,
  :country_code,
  :country_name,
  :location_code,
  :location_name,
  :branch_code,
  :branch_name
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(swift) ⇒ ISO::SWIFT

Returns A new instance of ISO::SWIFT.

Parameters:

  • swift (String)

    The SWIFT in either compact or human readable form.



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/iso/swift.rb', line 68

def initialize(swift)
  @data = {}
  @errors = []
  @data["original"] = swift
  swift = parse(swift)
  validate(swift)
  if @errors.empty?
    feed_codes(swift)
    feed_lookup_info(swift)
  end
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



60
61
62
# File 'lib/iso/swift.rb', line 60

def data
  @data
end

#errorsArray<Sym> (readonly)

Returns Retuns an array of errors in symbol format from validation step, if any.

Returns:

  • (Array<Sym>)

    Retuns an array of errors in symbol format from validation step, if any



184
185
186
# File 'lib/iso/swift.rb', line 184

def errors
  @errors
end

Instance Method Details

#bank_codeString

Returns Retuns the bank code from an ISO::SWIFT instance.

Returns:

  • (String)

    Retuns the bank code from an ISO::SWIFT instance



135
136
137
# File 'lib/iso/swift.rb', line 135

def bank_code
  @data["bank_code"]
end

#bank_nameString

Returns Retuns the bank name from an ISO::SWIFT instance.

Returns:

  • (String)

    Retuns the bank name from an ISO::SWIFT instance



141
142
143
# File 'lib/iso/swift.rb', line 141

def bank_name
  @data["bank_name"]
end

#branch_codeString

Returns Retuns the branch code from an ISO::SWIFT instance.

Returns:

  • (String)

    Retuns the branch code from an ISO::SWIFT instance



172
173
174
# File 'lib/iso/swift.rb', line 172

def branch_code
  @data["branch_code"]
end

#branch_nameString

Returns Retuns the branch name from an ISO::SWIFT instance.

Returns:

  • (String)

    Retuns the branch name from an ISO::SWIFT instance



178
179
180
# File 'lib/iso/swift.rb', line 178

def branch_name
  @data["branch_name"]
end

#country_codeString

Returns Retuns the country code from an ISO::SWIFT instance.

Returns:

  • (String)

    Retuns the country code from an ISO::SWIFT instance



147
148
149
# File 'lib/iso/swift.rb', line 147

def country_code
  @data["country_code"]
end

#country_nameString

Returns Retuns the country name from an ISO::SWIFT instance The country name was fetched using github.com/hexorx/countries.

Returns:



154
155
156
# File 'lib/iso/swift.rb', line 154

def country_name
  @data["country_name"]
end

#feed_codes(swift) ⇒ Object

Extracts bank, country, location and branch codes from the parameter

Parameters:

  • swift (String)

    The SWIFT in either compact or human readable form.



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/iso/swift.rb', line 84

def feed_codes(swift)
  if @errors.empty?
    @data["formatted"] = swift
    @data["bank_code"] = swift[0..3]
    @data["country_code"] = swift[4..5]
    country = ::Country.new(country_code)
    if country
      @data["country_name"] = country.name
    else
      @errors << :bad_country_code
    end
    @data["location_code"] = swift[6..7]
    @data["branch_code"] = swift[8..10]
  end
end

#feed_lookup_info(swift) ⇒ Object

Lookup for the formatted swift in data/country_code.yml If found, extract the bank, location and branch names

Parameters:

  • swift (String)

    The SWIFT in either compact or human readable form.



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/iso/swift.rb', line 105

def feed_lookup_info(swift)
  cc = country_code.downcase
  path = File.expand_path("../../data/#{cc}.yml", __FILE__)
  if File.file?(path)
    db = YAML.load_file(path) || nil
    if db
      lk = db[formatted]
      if lk
        @data["bank_name"] = lk["institution"]
        @data["location_name"] = lk["city"]
        @data["branch_name"] = lk["branch"]
      end
    end
  end
end

#formattedString

Returns Retuns the formatted swift from an ISO::SWIFT instance.

Returns:

  • (String)

    Retuns the formatted swift from an ISO::SWIFT instance



129
130
131
# File 'lib/iso/swift.rb', line 129

def formatted
  @data["formatted"]
end

#location_codeString

Returns Retuns the location code from an ISO::SWIFT instance.

Returns:

  • (String)

    Retuns the location code from an ISO::SWIFT instance



160
161
162
# File 'lib/iso/swift.rb', line 160

def location_code
  @data["location_code"]
end

#location_nameString

Returns Retuns the location name from an ISO::SWIFT instance.

Returns:

  • (String)

    Retuns the location name from an ISO::SWIFT instance



166
167
168
# File 'lib/iso/swift.rb', line 166

def location_name
  @data["location_name"]
end

#originalString

Returns Retuns the original swift from an ISO::SWIFT instance.

Returns:

  • (String)

    Retuns the original swift from an ISO::SWIFT instance



123
124
125
# File 'lib/iso/swift.rb', line 123

def original
  @data["original"]
end

#valid?Boolean

Returns if the current ISO::SWIFT instance if valid

Returns:

  • (Boolean)

    Returns if the current ISO::SWIFT instance if valid



190
191
192
# File 'lib/iso/swift.rb', line 190

def valid?
  @errors.empty?
end