Class: IpToEarth::APIController

Inherits:
Object
  • Object
show all
Defined in:
lib/ip_to_earth/api_controller.rb

Constant Summary collapse

HEADERS =
{
  accept: 'application/json'
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(api_key: Configuration.api_key) ⇒ APIController

Returns a new instance of APIController.



7
8
9
10
11
# File 'lib/ip_to_earth/api_controller.rb', line 7

def initialize(api_key: Configuration.api_key)
  @lookup_url_template =
    Addressable::Template.new("#{Configuration.base_uri}/{?ip,api_key}")
                         .partial_expand(api_key: api_key)
end

Instance Method Details

#lookup(ip) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/ip_to_earth/api_controller.rb', line 13

def lookup(ip)
  query_url = lookup_url_template.expand(ip: ip)

  response = Http.headers(HEADERS).get(query_url)

  fail_on_bad_response(response)
  parse_response(response)
end