Class: Geolocater
- Inherits:
-
Object
- Object
- Geolocater
- Defined in:
- lib/geolocater.rb,
lib/geolocater/version.rb
Constant Summary collapse
- REQUEST_URI =
"http://freegeoip.net/json/"- VERSION =
"0.1.0"
Instance Attribute Summary collapse
-
#ip_address ⇒ Object
Returns the value of attribute ip_address.
Class Method Summary collapse
Instance Method Summary collapse
- #geolocate_ip ⇒ Object
-
#initialize(ip_address) ⇒ Geolocater
constructor
A new instance of Geolocater.
Constructor Details
#initialize(ip_address) ⇒ Geolocater
Returns 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
#ip_address ⇒ Object
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
.geolocate_ip(ip_address) ⇒ Object
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
#geolocate_ip ⇒ Object
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 |