Class: Geolocater

Inherits:
Object
  • Object
show all
Defined in:
lib/geolocater.rb,
lib/geolocater/version.rb

Constant Summary

REQUEST_URI =
"http://freegeoip.net/json/"
VERSION =
"0.1.0"

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Geolocater) initialize(ip_address)

A new instance of Geolocater



11
12
13
14
# File 'lib/geolocater.rb', line 11

def initialize(ip_address)
  @ip_address = IPAddr.new ip_address
  raise "IPv6 NOT SUPPORTED" if @ip_address.ipv6?
end

Instance Attribute Details

- (Object) ip_address

Returns the value of attribute ip_address



9
10
11
# File 'lib/geolocater.rb', line 9

def ip_address
  @ip_address
end

Class Method Details

+ (Object) geolocate_ip(ip_address)



29
30
31
32
# File 'lib/geolocater.rb', line 29

def geolocate_ip(ip_address)
  geolocator = Geolocater.new(ip_address)
  geolocator.geolocate_ip
end

Instance Method Details

- (Object) geolocate_ip



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/geolocater.rb', line 16

def geolocate_ip
  http_response = Faraday.get REQUEST_URI + ip_address.to_s
  if http_response.success? == true && http_response.status == 200;
    geolocated_info = JSON.parse(http_response.body)
    if geolocated_info["city"].empty?
      raise "Incomplete record. Please try another IP address"
    else
      return geolocated_info
    end
  end
end