Module: Geocoder::Request

Defined in:
lib/geocoder/request.rb

Instance Method Summary collapse

Instance Method Details

#locationObject


4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/geocoder/request.rb', line 4

def location
  @location ||= begin
    detected_ip = env['HTTP_X_REAL_IP'] || (
      env['HTTP_X_FORWARDED_FOR'] &&
      env['HTTP_X_FORWARDED_FOR'].split(",").first.strip
    )
    detected_ip = IpAddress.new(detected_ip.to_s)
    if detected_ip.valid? and !detected_ip.loopback?
      real_ip = detected_ip.to_s
    else
      real_ip = self.ip
    end
    Geocoder.search(real_ip).first
  end
  @location
end