Module: Moocher::GeoIp

Included in:
Resource
Defined in:
lib/moocher/geo_ip.rb

Instance Method Summary collapse

Instance Method Details

#geolocate_ip(ipaddress = nil) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/moocher/geo_ip.rb', line 7

def geolocate_ip(ipaddress=nil)
  base_url = 'https://api.moocher.io/ip/'
  if ipaddress
      return "IP Address doesn't seem to be valid." if !(IPAddress.valid? ipaddress)
      response = HTTParty.get(base_url + ipaddress)
      response.success? ? response.parsed_response : "Sorry No data was found."
    else
      response = HTTParty.get(base_url)
      response.success? ? response.parsed_response : "Sorry No data was found."
  end
end