Class: BankAccountTools::BIC

Inherits:
Object
  • Object
show all
Defined in:
lib/bank_account_tools/bic.rb

Constant Summary collapse

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code) ⇒ BIC

Returns a new instance of BIC.



11
12
13
# File 'lib/bank_account_tools/bic.rb', line 11

def initialize(code)
  @code = code.to_s.gsub(/\s+/, '').upcase
end

Class Method Details

.valid?(code) ⇒ Boolean

Returns:

  • (Boolean)


7
8
9
# File 'lib/bank_account_tools/bic.rb', line 7

def self.valid?(code)
  new(code).valid?
end

Instance Method Details

#bank_codeObject



15
16
17
# File 'lib/bank_account_tools/bic.rb', line 15

def bank_code
  @code[0..3]
end

#branch_codeObject



27
28
29
# File 'lib/bank_account_tools/bic.rb', line 27

def branch_code
  @code[8..10]
end

#country_codeObject



19
20
21
# File 'lib/bank_account_tools/bic.rb', line 19

def country_code
  @code[4..5]
end

#location_codeObject



23
24
25
# File 'lib/bank_account_tools/bic.rb', line 23

def location_code
  @code[6..7]
end

#passive?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/bank_account_tools/bic.rb', line 43

def passive?
  location_code[1] == '1'
end

#reverse_billing?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/bank_account_tools/bic.rb', line 47

def reverse_billing?
  location_code[1] == '2'
end

#test?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/bank_account_tools/bic.rb', line 39

def test?
  location_code[1] == '0'
end

#to_formatted_strObject



35
36
37
# File 'lib/bank_account_tools/bic.rb', line 35

def to_formatted_str
  "#{bank_code} #{country_code} #{location_code} #{branch_code}".strip
end

#to_s(formatted = false) ⇒ Object



31
32
33
# File 'lib/bank_account_tools/bic.rb', line 31

def to_s(formatted=false)
  formatted ? to_formatted_str : @code
end

#valid?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/bank_account_tools/bic.rb', line 51

def valid?
  valid_format? && valid_location_code? && valid_branch_code?
end

#valid_branch_code?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/bank_account_tools/bic.rb', line 63

def valid_branch_code?
  branch_code.empty? || branch_code == 'XXX' || !branch_code.start_with?('X')
end

#valid_format?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/bank_account_tools/bic.rb', line 55

def valid_format?
  !!@code[REGEX]
end

#valid_location_code?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/bank_account_tools/bic.rb', line 59

def valid_location_code?
  !location_code.start_with?('0', '1') && !location_code.end_with?('O')
end