Class: IbanLookup::IbanCom::IbanCalc

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ IbanCalc

Returns a new instance of IbanCalc.



46
47
48
49
50
51
# File 'lib/iban-lookup.rb', line 46

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



86
87
88
# File 'lib/iban-lookup.rb', line 86

def method_missing(name, *args)
  json.fetch(String(name), nil)
end

Instance Attribute Details

#paramsObject (readonly)

Returns the value of attribute params.



44
45
46
# File 'lib/iban-lookup.rb', line 44

def params
  @params
end

Instance Method Details

#cacheObject



80
81
82
# File 'lib/iban-lookup.rb', line 80

def cache
  IbanLookup.configuration.cache
end

#jsonObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/iban-lookup.rb', line 53

def json
  @json ||= begin
    logger.info "Looking up #{params.to_query}"
    body = if cache
             cache.get(params.to_query).presence ||
             response.body.presence.tap { |j| cache.set(params.to_query, 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



71
72
73
74
75
76
77
78
# File 'lib/iban-lookup.rb', line 71

def response
  url = "#{URL.chomp('/')}/clients/api/calc-api.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