5

I am using the Geocoder gem but lately it does not seem to work.

I get this error:

Geocoding API not responding fast enough (use Geocoder.configure(:timeout => ...) to set limit).

My application_controller.rb is:

before_filter :lookup_ip_location

  private

    def lookup_ip_location
      if Rails.env.development?
        prr = Geocoder.search(request.remote_ip).first
        p "this is #{prr.inspect}"
      else
        request.location
      end
    end

This is development.rb:

# test geocoder gem locally
  class ActionDispatch::Request
    def remote_ip
      "71.212.123.5" # ipd home (Denver,CO or Renton,WA)
      # "208.87.35.103" # websiteuk.com -- Nassau, Bahamas
      # "50.78.167.161" # HOL Seattle, WA
    end
  end

I am loading an IP addresses from development.rb to check if geocoder works locally, but it does not. I am getting the above error.

Also, when printing prr I get nil.

I also added a geocoder.rb initializer to raise the timeout to 15 seconds but even after 15 seconds of the browser loading the page I'm still getting the same message.

Is it broken? Should I use another gem? If so, do you have any suggestions?

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Petros Kyriakou
  • 5,214
  • 4
  • 43
  • 82

1 Answers1

6

Interesting. I tried your exact methods, and was running into the same problems. I also tried bumping the timeout up to 60 seconds, and same error.

Then I noticed Geocoder uses freegeoip. So I went to see what that was all about. Lo and behold, freegeoip.net is down. Suspicious.

So I checked the Geocoder documentation for any different ip address lookup services they offer. Sure enough, under "Ip Address Services", there are multiple offers. I tried the first one that does not require an API key, which was :ipinfo_io.

[18] pry(main)> Geocoder.configure(ip_lookup: :ipinfo_io)
=> {:timeout=>30,
 :lookup=>:google,
 :ip_lookup=>:ipinfo_io,
 :language=>:en,
 :http_headers=>{},
 :use_https=>false,
 :http_proxy=>nil,
 :https_proxy=>nil,
 :api_key=>nil,
 :cache=>nil,
 :cache_prefix=>"geocoder:",
 :basic_auth=>{},
 :logger=>:kernel,
 :kernel_logger_level=>2,
 :always_raise=>[],
 :units=>:mi,
 :distances=>:linear}
[19] pry(main)> Geocoder.search("144.138.175.101")
=> [#<Geocoder::Result::IpinfoIo:0x007fce5da5fe28 @cache_hit=nil, @data={"ip"=>"144.138.175.101", "city"=>"", "region"=>"", "country"=>"AU", "loc"=>"-27.0000,133.0000"}>]

And it works! But the response doesn't have much info. I would recommend looking at the other ip lookup services that Geocoder uses. Find one that is reliable and has enough response info for your needs. Seems that freegeoip is free, but can also be unreliable. Cheers.

EDIT: Found some related information about freegeoip.net here. If you really wish to use freegeoip, looks like you can run your own instance. Hope this helps!

Community
  • 1
  • 1
Anthony To
  • 2,193
  • 2
  • 21
  • 29
  • thanks for checking it out. However i get this error `Please specify a valid lookup for Geocoder (:ipinfo_io is not one of: ` thats weird. unless i have to update my gem? EDIT: yes i had to update gem to use ipinfo. works great thank you very much!Seems though that i does not support https, does this mean i cannot use it on an https production app? – Petros Kyriakou Apr 09 '16 at 23:14
  • I believe your app can still use https, but the calls to `ipinfo.io` from your server will not use https. Depending on your use case, this may be acceptable for you. Optionally, `ipinfo.io` allows https with an api key and cost. I encourage you to also compare `ipinfo.io` with the other services! – Anthony To Apr 09 '16 at 23:34
  • i have just now setup google and provided api_key and when i try `request.location` and print it, it says i am in iraq, which i am not, i am located in Cyprus, on my production app, it says i am in philippines. Why is that?would not expect that from google... – Petros Kyriakou Apr 09 '16 at 23:35
  • I'm not sure why that would happen. Perhaps you can create a new SO question with more details. – Anthony To Apr 10 '16 at 01:03