rack-geoip 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
22
+ data
data/COPYING ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Chris Kraybill
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,78 @@
1
+ = Rack::Geoip
2
+
3
+ A rack middleware component that handles simple geoip lookups.
4
+
5
+ == Installation
6
+
7
+ First you must have a GeoIP database installed. Currently the only backend support is the free one from
8
+ MaxMind (http://www.maxmind.com). So you must first install the C api bindings.
9
+
10
+ $ wget http://geolite.maxmind.com/download/geoip/api/c/GeoIP.tar.gz
11
+ $ tar xzf GeoIP.tar.gz
12
+ $ cd GeoIP-1.4.6
13
+ $ ./configure && make && sudo make install
14
+
15
+ To download the actual GeoLiteCity (free) database you can currently find it here (I like to place it in
16
+ a data sub-directory).
17
+
18
+ $ cd data
19
+ $ wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
20
+ $ gunzip GeoLiteCity.dat.gz
21
+
22
+ And finally install the gem
23
+
24
+ $ gem install rack-geoip
25
+
26
+ == Usage
27
+
28
+ === Basic Usage
29
+
30
+ require 'rack/geoip'
31
+ use Rack::Geoip
32
+ run app
33
+
34
+ === Using with Rails
35
+
36
+ Add this to your config/environment.rb
37
+
38
+ config.gem 'rack-geoip'
39
+
40
+ And then in an initializer block
41
+
42
+ config.middleware.use "Rack::Geoip",
43
+ :db => 'data/GeoLiteCity.dat',
44
+ :path => '/geoip/info'
45
+
46
+ === Sample output
47
+
48
+ $ curl http://localhost:9292/lookup?ip=8.8.8.8
49
+
50
+ { "area_code":650,
51
+ "city":"Mountain View",
52
+ "postal_code":"94043",
53
+ "country_code":"US",
54
+ "latitude":37.4192008972168,
55
+ "country_code3":"USA",
56
+ "longitude":-122.057403564453,
57
+ "country_name":"United States",
58
+ "dma_code":807,
59
+ "region":"CA" }
60
+
61
+ == Note on Patches/Pull Requests
62
+
63
+ * Fork the project.
64
+ * Make your feature addition or bug fix.
65
+ * Add tests for it. This is important so I don't break it in a
66
+ future version unintentionally.
67
+ * Commit, do not mess with rakefile, version, or history.
68
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
69
+ * Send me a pull request. Bonus points for topic branches.
70
+
71
+ == Credits
72
+
73
+ I want to acknowledge the work done by Geoffrey Grosenbach (http://peepcode.com) on the sinatra-geoip gem which was the
74
+ inspiration for this.
75
+
76
+ == License
77
+
78
+ Rack::Geoip is Copyright (c) 2010 by Chris Kraybill and is provided under the MIT license. See COPYING for details.
data/Rakefile ADDED
@@ -0,0 +1,48 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "rack-geoip"
8
+ gem.summary = %Q{A rack middleware component that handles simple geoip lookups.}
9
+ gem.description = %Q{A rack middleware component that handles simple geoip lookups.}
10
+ gem.email = "ckraybill@gmail.com"
11
+ gem.homepage = "http://github.com/ckraybill/rack-geoip"
12
+ gem.authors = ["Chris Kraybill"]
13
+ gem.add_development_dependency "rspec", ">= 1.2.9"
14
+ gem.add_dependency "geoip_city"
15
+ gem.add_dependency "json"
16
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
17
+ end
18
+ Jeweler::GemcutterTasks.new
19
+ rescue LoadError
20
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
21
+ end
22
+
23
+ require 'spec/rake/spectask'
24
+ Spec::Rake::SpecTask.new(:spec) do |spec|
25
+ spec.libs << 'lib' << 'spec'
26
+ spec.spec_files = FileList['spec/**/*_spec.rb']
27
+ end
28
+
29
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
30
+ spec.libs << 'lib' << 'spec'
31
+ spec.pattern = 'spec/**/*_spec.rb'
32
+ spec.rcov = true
33
+ end
34
+
35
+ task :spec => :check_dependencies
36
+
37
+ task :default => :spec
38
+
39
+ require 'rake/rdoctask'
40
+ Rake::RDocTask.new do |rdoc|
41
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
42
+
43
+ rdoc.rdoc_dir = 'rdoc'
44
+ rdoc.title = "rack-geoip #{version}"
45
+ rdoc.rdoc_files.include('README*','COPYING')
46
+ rdoc.rdoc_files.include('lib/**/*.rb')
47
+ rdoc.rdoc_files.include('TODO')
48
+ end
data/TODO ADDED
@@ -0,0 +1,8 @@
1
+ == Short Term
2
+
3
+ * Document code better
4
+
5
+ == Version bumps
6
+
7
+ * Implement a backend interface to allow for different GeoIP backends
8
+ * Country code support
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.1
data/examples/basic.ru ADDED
@@ -0,0 +1,13 @@
1
+ require 'rubygems'
2
+ require 'rack'
3
+ require 'rack/geoip'
4
+
5
+ class BasicRackApp
6
+ def call(env)
7
+ [200, {'Content-Type' => 'text/plain'}, 'Your request did not hit the geoip lookup']
8
+ end
9
+ end
10
+
11
+ use Rack::Geoip, :path => '/lookup', :db => 'data/GeoLiteCity.dat'
12
+
13
+ run BasicRackApp.new
@@ -0,0 +1,32 @@
1
+ require 'geoip_city'
2
+ require 'json'
3
+
4
+ module Rack::Geoip
5
+ # Implements Rack's middleware interface and provides the geoip
6
+ # lookup service
7
+ class Lookup
8
+ DEFAULT = {:path => '/geoip/lookup', :db => 'GeoLiteCity.dat'}
9
+
10
+ def initialize(app, options={})
11
+ @app, @options = app, DEFAULT.merge(options)
12
+ @db = GeoIPCity::Database.new(@options[:db])
13
+ end
14
+
15
+ def call(env)
16
+ dup._call(env)
17
+ end
18
+
19
+ def _call(env)
20
+ if env['PATH_INFO'] == @options[:path]
21
+ request = Rack::Request.new(env)
22
+ if request.params['ip'] && result=@db.look_up(request.params['ip'])
23
+ [200, {'Content-Type' => 'application/json'}, result.to_json]
24
+ else
25
+ [404, {'Content-Type' => 'text/plain'}, "Example usage: #{env['rack.url_scheme']}://#{env['HTTP_HOST']}#{env['PATH_INFO']}?ip=8.8.8.8"]
26
+ end
27
+ else
28
+ @app.call(env)
29
+ end
30
+ end
31
+ end
32
+ end
data/lib/rack/geoip.rb ADDED
@@ -0,0 +1,37 @@
1
+ require 'rack'
2
+
3
+ module Rack #:nodoc:
4
+ # = Rack Middleware for GEOIP Lookups
5
+ #
6
+ # Rack::Geoip is intended to be a rack interface into a GeoIP database
7
+ # provided by MaxMind (http://www.maxmind.com). See the README for
8
+ # installation
9
+ #
10
+ # == Usage
11
+ #
12
+ # === Basic Usage
13
+ #
14
+ # require 'rack/geoip'
15
+ # use Rack::Geoip
16
+ # run app
17
+ #
18
+ # === Using with Rails
19
+ #
20
+ # Add this to your config/environment.rb
21
+ #
22
+ # config.gem 'rack-geoip'
23
+ #
24
+ # And then in an initializer block
25
+ #
26
+ # config.middleware.use "Rack::Geoip",
27
+ # :db => 'data/GeoLiteCity.dat',
28
+ # :path => '/geoip/info'
29
+ #
30
+ module Geoip
31
+ autoload :Lookup, 'rack/geoip/lookup'
32
+
33
+ def self.new(backend, options = {})
34
+ Lookup.new(backend,options)
35
+ end
36
+ end
37
+ end
data/lib/rack-geoip.rb ADDED
@@ -0,0 +1 @@
1
+ require 'rack/geoip'
@@ -0,0 +1,65 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{rack-geoip}
8
+ s.version = "0.1.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Chris Kraybill"]
12
+ s.date = %q{2010-08-25}
13
+ s.description = %q{A rack middleware component that handles simple geoip lookups.}
14
+ s.email = %q{ckraybill@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "README",
17
+ "TODO"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "COPYING",
23
+ "README",
24
+ "Rakefile",
25
+ "TODO",
26
+ "VERSION",
27
+ "examples/basic.ru",
28
+ "lib/rack-geoip.rb",
29
+ "lib/rack/geoip.rb",
30
+ "lib/rack/geoip/lookup.rb",
31
+ "rack-geoip.gemspec",
32
+ "spec/rack-geoip_spec.rb",
33
+ "spec/spec.opts",
34
+ "spec/spec_helper.rb"
35
+ ]
36
+ s.homepage = %q{http://github.com/ckraybill/rack-geoip}
37
+ s.rdoc_options = ["--charset=UTF-8"]
38
+ s.require_paths = ["lib"]
39
+ s.rubygems_version = %q{1.3.7}
40
+ s.summary = %q{A rack middleware component that handles simple geoip lookups.}
41
+ s.test_files = [
42
+ "spec/rack-geoip_spec.rb",
43
+ "spec/spec_helper.rb"
44
+ ]
45
+
46
+ if s.respond_to? :specification_version then
47
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
48
+ s.specification_version = 3
49
+
50
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
51
+ s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
52
+ s.add_runtime_dependency(%q<geoip_city>, [">= 0"])
53
+ s.add_runtime_dependency(%q<json>, [">= 0"])
54
+ else
55
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
56
+ s.add_dependency(%q<geoip_city>, [">= 0"])
57
+ s.add_dependency(%q<json>, [">= 0"])
58
+ end
59
+ else
60
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
61
+ s.add_dependency(%q<geoip_city>, [">= 0"])
62
+ s.add_dependency(%q<json>, [">= 0"])
63
+ end
64
+ end
65
+
@@ -0,0 +1,40 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe "RackGeoip" do
4
+ def app
5
+ app = Rack::Builder.new {
6
+ use Rack::Geoip, :path => '/geocode/lookup', :db => File.dirname(__FILE__)+'/../data/GeoLiteCity.dat'
7
+ run BasicRackApp.new
8
+ }
9
+ end
10
+
11
+ # This makes sure the middleware doesn't get in the way of normal requests
12
+ it "says hello" do
13
+ get '/'
14
+ last_response.should be_ok
15
+ last_response.body.should == 'Hello World'
16
+ end
17
+
18
+ # This makes sure that the middleware will return a 404 with some suggested usage
19
+ # when the request is malformed
20
+ it 'should return 404 when a geocode lookup request has malformed parameters' do
21
+ get '/geocode/lookup'
22
+ last_response.status.should == 404
23
+ last_response.body.should == 'Example usage: http:///geocode/lookup?ip=8.8.8.8'
24
+ end
25
+
26
+ # This makes sure that the response is JSON parse-able
27
+ it 'should return some json when a geocode lookup works' do
28
+ get '/geocode/lookup?ip=8.8.8.8'
29
+ last_response.should be_ok
30
+ json = JSON.parse(last_response.body)
31
+ json.class.should == Hash
32
+ end
33
+
34
+ # This tests the actual geocode lookup
35
+ it 'should return the city Mountain View when the provided IP lookup is for a googleplex' do
36
+ get '/geocode/lookup?ip=8.8.8.8'
37
+ json = JSON.parse(last_response.body)
38
+ json['city'].should == 'Mountain View'
39
+ end
40
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,18 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'rubygems'
4
+ require 'json'
5
+ require 'rack-geoip'
6
+ require 'spec'
7
+ require 'spec/autorun'
8
+ require 'rack/test'
9
+
10
+ Spec::Runner.configure do |config|
11
+ config.include Rack::Test::Methods
12
+ end
13
+
14
+ class BasicRackApp
15
+ def call(env)
16
+ [200, {'Content-Type' => 'text/plain'}, 'Hello World']
17
+ end
18
+ end
metadata ADDED
@@ -0,0 +1,126 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-geoip
3
+ version: !ruby/object:Gem::Version
4
+ hash: 25
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 1
10
+ version: 0.1.1
11
+ platform: ruby
12
+ authors:
13
+ - Chris Kraybill
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-08-25 00:00:00 -07:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rspec
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 13
30
+ segments:
31
+ - 1
32
+ - 2
33
+ - 9
34
+ version: 1.2.9
35
+ type: :development
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: geoip_city
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 3
46
+ segments:
47
+ - 0
48
+ version: "0"
49
+ type: :runtime
50
+ version_requirements: *id002
51
+ - !ruby/object:Gem::Dependency
52
+ name: json
53
+ prerelease: false
54
+ requirement: &id003 !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ hash: 3
60
+ segments:
61
+ - 0
62
+ version: "0"
63
+ type: :runtime
64
+ version_requirements: *id003
65
+ description: A rack middleware component that handles simple geoip lookups.
66
+ email: ckraybill@gmail.com
67
+ executables: []
68
+
69
+ extensions: []
70
+
71
+ extra_rdoc_files:
72
+ - README
73
+ - TODO
74
+ files:
75
+ - .document
76
+ - .gitignore
77
+ - COPYING
78
+ - README
79
+ - Rakefile
80
+ - TODO
81
+ - VERSION
82
+ - examples/basic.ru
83
+ - lib/rack-geoip.rb
84
+ - lib/rack/geoip.rb
85
+ - lib/rack/geoip/lookup.rb
86
+ - rack-geoip.gemspec
87
+ - spec/rack-geoip_spec.rb
88
+ - spec/spec.opts
89
+ - spec/spec_helper.rb
90
+ has_rdoc: true
91
+ homepage: http://github.com/ckraybill/rack-geoip
92
+ licenses: []
93
+
94
+ post_install_message:
95
+ rdoc_options:
96
+ - --charset=UTF-8
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ none: false
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ hash: 3
105
+ segments:
106
+ - 0
107
+ version: "0"
108
+ required_rubygems_version: !ruby/object:Gem::Requirement
109
+ none: false
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ hash: 3
114
+ segments:
115
+ - 0
116
+ version: "0"
117
+ requirements: []
118
+
119
+ rubyforge_project:
120
+ rubygems_version: 1.3.7
121
+ signing_key:
122
+ specification_version: 3
123
+ summary: A rack middleware component that handles simple geoip lookups.
124
+ test_files:
125
+ - spec/rack-geoip_spec.rb
126
+ - spec/spec_helper.rb