Class: IbanLookup::IbanCom::IbanChecker

Inherits:
Object
  • Object
show all
Defined in:
lib/iban-lookup.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(iban) ⇒ IbanChecker



98
99
100
101
102
103
104
# File 'lib/iban-lookup.rb', line 98

def initialize(iban)
  @params = {
    :api_key => IbanLookup.configuration.api_key,
    :format => :json,
    :iban => iban,
  }
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object (private)



148
149
150
# File 'lib/iban-lookup.rb', line 148

def method_missing(name, *args)
  json.fetch('bank_data', {}).fetch(String(name), nil)
end

Instance Attribute Details

#paramsObject (readonly)

Returns the value of attribute params.



96
97
98
# File 'lib/iban-lookup.rb', line 96

def params
  @params
end

Instance Method Details

#cacheObject



142
143
144
# File 'lib/iban-lookup.rb', line 142

def cache
  IbanLookup.configuration.cache
end

#jsonObject



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/iban-lookup.rb', line 115

def json
  @json ||= begin
    logger.info "Looking up #{params[:iban]}"
    body = if cache
             cache.get(params[:iban]).presence ||
             response.body.presence.tap { |j| cache.set(params[:iban], j) }
           else
             response.body.presence
           end

    JSON.parse(body.presence || '{}')
  end
rescue => e
  logger.error "Unable to extract JSON from IBAN response (#{e.class}): #{e.message}"
  logger.debug e.backtrace.join("\n")
  {}
end

#responseObject



133
134
135
136
137
138
139
140
# File 'lib/iban-lookup.rb', line 133

def response
  url = "#{URL.chomp('/')}/clients/api/ibanv2.php?#{params.to_query}"
  logger.debug "IBAN url: #{url}"
  Faraday.get(url).tap do |res|
    logger.debug "IBAN response status: #{res.status}"
    logger.debug "IBAN response body: #{res.body}"
  end
end

#valid?Boolean



106
107
108
109
110
111
112
113
# File 'lib/iban-lookup.rb', line 106

def valid?
  valid_codes = %w(001 002 003 004 005)
  json.fetch('validations', []).all? { |v| valid_codes.include? v['code'] }
rescue => e
  logger.error "Unable to validate IBAN from IBAN json response (#{e.class}): #{e.message}"
  logger.debug e.backtrace.join("\n")
  false
end