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

Returns a new instance of IbanChecker.



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

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)



150
151
152
# File 'lib/iban-lookup.rb', line 150

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.



98
99
100
# File 'lib/iban-lookup.rb', line 98

def params
  @params
end

Instance Method Details

#cacheObject



144
145
146
# File 'lib/iban-lookup.rb', line 144

def cache
  IbanLookup.configuration.cache
end

#jsonObject



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

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



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

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

Returns:

  • (Boolean)


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

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