Class: SWIFTCodeTool::SWIFT

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

Returns:

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



23
24
25
# File 'lib/swift-code-tool/lib/swift-code.rb', line 23

def bank_code
  @code[0..3]
end

#branch_codeObject



35
36
37
# File 'lib/swift-code-tool/lib/swift-code.rb', line 35

def branch_code
  @code[8..10]
end

#codeObject



19
20
21
# File 'lib/swift-code-tool/lib/swift-code.rb', line 19

def code
  @code
end

#country_codeObject



27
28
29
# File 'lib/swift-code-tool/lib/swift-code.rb', line 27

def country_code
  @code[4..5]
end

#location_codeObject



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