Skip to content

Commit

Permalink
+ support for 1.8.7, proxy headers
Browse files Browse the repository at this point in the history
  • Loading branch information
niko committed Jun 6, 2011
1 parent cefe46c commit 656015a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/mm_geoip.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class NoIpGiven < StandardError; end

def initialize(env)
@env = env # may be a Rack @env or any hash containing initial data
@env[:ip] ||= @env["REMOTE_ADDR"] # :ip or "REMOTE_ADDR" should be present
@env[:ip] ||= @env["HTTP_X_REAL_IP"] || @env["HTTP_X_FORWARDED_FOR"] || @env["REMOTE_ADDR"]

raise NoIpGiven.new unless @env[:ip]

Expand Down Expand Up @@ -60,7 +60,7 @@ def lookup

return @lookup = {} unless looked_up_fields

@lookup = Hash[FIELDS.zip looked_up_fields]
@lookup = Hash[FIELDS.zip looked_up_fields.to_a]
@lookup[:region_name] = region_name
@lookup
end
Expand Down
6 changes: 6 additions & 0 deletions spec/mm_geoip_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@
it "works with the IP as 'REMOTE_ADDR' field" do
MMGeoip.new 'REMOTE_ADDR' => '134.34.3.2'
end
it "works with the IP as 'HTTP_X_REAL_IP' field" do
MMGeoip.new 'HTTP_X_REAL_IP' => '134.34.3.2'
end
it "works with the IP as 'HTTP_X_FORWARDED_FOR' field" do
MMGeoip.new 'HTTP_X_FORWARDED_FOR' => '134.34.3.2'
end
it "raises, if not :ip or 'REMOTE_ADDR' is given" do
lambda{ MMGeoip.new :whatelse => 'something' }.should raise_error(MMGeoip::NoIpGiven)
end
Expand Down

0 comments on commit 656015a

Please sign in to comment.