Class: SWIFTCodeTool::SWIFT
- Inherits:
-
Object
- Object
- SWIFTCodeTool::SWIFT
- Defined in:
- lib/swift-code-tool/lib/swift-code.rb
Constant Summary collapse
- REGEX =
/^[A-Z]{4}[A-Z]{2}[A-Z0-9]{2}([A-Z0-9]{3})?$/
Class Method Summary collapse
-
.canonicalize_code(code) ⇒ Object
escape whitespace and convert to uppercase.
- .valid?(code, country_code = nil) ⇒ Boolean
Instance Method Summary collapse
- #bank_code ⇒ Object
- #branch_code ⇒ Object
- #code ⇒ Object
- #country_code ⇒ Object
-
#initialize(code) ⇒ SWIFT
constructor
A new instance of SWIFT.
- #location_code ⇒ Object
- #validation_errors(expected_country_code) ⇒ Object
Constructor Details
#initialize(code) ⇒ SWIFT
Returns a new instance of SWIFT.
15 16 17 |
# File 'lib/swift-code-tool/lib/swift-code.rb', line 15 def initialize(code) @code = SWIFT.canonicalize_code(code) end |
Class Method Details
.canonicalize_code(code) ⇒ Object
escape whitespace and convert to uppercase
40 41 42 |
# File 'lib/swift-code-tool/lib/swift-code.rb', line 40 def self.canonicalize_code( code ) code.strip.gsub(/\s+/, '').upcase end |
.valid?(code, country_code = nil) ⇒ Boolean
44 45 46 |
# File 'lib/swift-code-tool/lib/swift-code.rb', line 44 def self.valid?(code, country_code = nil) new(code).validation_errors(country_code).empty? end |
Instance Method Details
#bank_code ⇒ Object
23 24 25 |
# File 'lib/swift-code-tool/lib/swift-code.rb', line 23 def bank_code @code[0..3] end |
#branch_code ⇒ Object
35 36 37 |
# File 'lib/swift-code-tool/lib/swift-code.rb', line 35 def branch_code @code[8..10] end |
#code ⇒ Object
19 20 21 |
# File 'lib/swift-code-tool/lib/swift-code.rb', line 19 def code @code end |
#country_code ⇒ Object
27 28 29 |
# File 'lib/swift-code-tool/lib/swift-code.rb', line 27 def country_code @code[4..5] end |
#location_code ⇒ Object
31 32 33 |
# File 'lib/swift-code-tool/lib/swift-code.rb', line 31 def location_code @code[6..7] end |
#validation_errors(expected_country_code) ⇒ Object
48 49 50 51 52 53 54 55 56 |
# File 'lib/swift-code-tool/lib/swift-code.rb', line 48 def validation_errors(expected_country_code) errors = [] return [:unexpected_country_code] if not expected_country_code.nil? and (country_code != expected_country_code.upcase) return [:too_short] if @code.size < 8 return [:too_long] if @code.size > 11 return [:bad_chars] unless @code =~ /^[A-Z0-9]+$/ errors << :bad_format unless @code =~ REGEX errors end |