geoip_server 1.2.0 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -1,14 +1,3 @@
1
- source :rubygems
1
+ source 'https://rubygems.org'
2
2
 
3
- gem 'sinatra', '1.1.2'
4
- gem 'activesupport'
5
- gem 'geoip'
6
- gem 'i18n'
7
- gem 'newrelic_rpm'
8
-
9
- group :development, :test do
10
- gem 'jeweler'
11
- gem 'rake'
12
- gem 'shoulda'
13
- gem 'rack-test'
14
- end
3
+ gemspec
@@ -1,39 +1,45 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ geoip_server (1.3.0)
5
+ geoip (~> 1.1)
6
+ multi_json (~> 1.3)
7
+ newrelic_rpm (~> 3.4)
8
+ sinatra (~> 1.1)
9
+
1
10
  GEM
2
- remote: http://rubygems.org/
11
+ remote: https://rubygems.org/
3
12
  specs:
4
- activesupport (3.0.3)
5
- geoip (0.8.8)
6
- git (1.2.5)
7
- i18n (0.5.0)
8
- jeweler (1.8.4)
9
- bundler (~> 1.0)
10
- git (>= 1.2.5)
11
- rake
12
- rdoc
13
- json (1.7.5)
14
- newrelic_rpm (2.13.4)
15
- rack (1.2.1)
13
+ activesupport (3.2.8)
14
+ i18n (~> 0.6)
15
+ multi_json (~> 1.0)
16
+ geoip (1.1.2)
17
+ i18n (0.6.1)
18
+ multi_json (1.3.6)
19
+ newrelic_rpm (3.4.2.1)
20
+ rack (1.4.1)
21
+ rack-protection (1.2.0)
22
+ rack
16
23
  rack-test (0.6.1)
17
24
  rack (>= 1.0)
18
25
  rake (0.9.2.2)
19
- rdoc (3.12)
20
- json (~> 1.4)
21
- shoulda (2.0.6)
22
- sinatra (1.1.2)
23
- rack (~> 1.1)
24
- tilt (~> 1.2)
25
- tilt (1.2.1)
26
+ shoulda (3.1.1)
27
+ shoulda-context (~> 1.0)
28
+ shoulda-matchers (~> 1.2)
29
+ shoulda-context (1.0.0)
30
+ shoulda-matchers (1.3.0)
31
+ activesupport (>= 3.0.0)
32
+ sinatra (1.3.3)
33
+ rack (~> 1.3, >= 1.3.6)
34
+ rack-protection (~> 1.2)
35
+ tilt (~> 1.3, >= 1.3.3)
36
+ tilt (1.3.3)
26
37
 
27
38
  PLATFORMS
28
39
  ruby
29
40
 
30
41
  DEPENDENCIES
31
- activesupport
32
- geoip
33
- i18n
34
- jeweler
35
- newrelic_rpm
42
+ geoip_server!
36
43
  rack-test
37
44
  rake
38
45
  shoulda
39
- sinatra (= 1.1.2)
@@ -10,21 +10,28 @@ This simple Rack server is useful as a self-hosted service for making lookups to
10
10
  * Commit that data file to your clone: `git add vendor && git commit -m "adding data file"`
11
11
  * Signup for an account at Heroku ([better details here](http://github.com/sinatra/heroku-sinatra-app))
12
12
  * Create and name a new app where you want to host this
13
- * push it to Heroku.com: `git push heroku master`
13
+ * Push it to Heroku.com: `git push heroku master`
14
14
 
15
15
 
16
16
  ## HowTo
17
17
 
18
18
  Once the server is running you can make a GET request to the server and receive lookup results in JSON format.
19
19
 
20
- ip = request.remote_ip
21
- require 'open-uri'
22
- data = JSON.decode(open("http://my-geoip-service-app.heroku.com/#{ip}").read)
23
- render :text => "You're in: #{data[:city]}"
20
+ ```ruby
21
+ require 'json'
22
+ require 'open-uri'
23
+ # Get the requesting user's IP address
24
+ # In a Rails app, you can use: request.remote_ip
25
+ # In a Sinatra app, you can use: request.ip
26
+ # In a Rack app, you can use: @env['REMOTE_ADDR']
27
+ ip = "207.97.227.239"
28
+ data = JSON.load(open("http://my-geoip-service-app.herokuapp.com/#{ip}").read)
29
+ "You're in: #{data['city']}"
30
+ ```
24
31
 
25
32
  Or, straight from a terminal:
26
33
 
27
- curl -X POST http://my-geoip-service-app.heroku.com/207.97.227.239
34
+ curl http://my-geoip-service-app.herokuapp.com/207.97.227.239
28
35
 
29
36
  Patches welcome, forks celebrated.
30
37
 
data/Rakefile CHANGED
@@ -1,24 +1,5 @@
1
- begin
2
- require 'jeweler'
3
- Jeweler::Tasks.new do |gem|
4
- gem.name = "geoip_server"
5
- gem.summary = %Q{Query the MaxMind GeoIP data records via a web service}
6
- gem.description = %Q{Query the MaxMind GeoIP data records via a web service}
7
- gem.email = "gitcommit@6brand.com"
8
- gem.homepage = "http://github.com/JackDanger/geoip_server"
9
- gem.authors = ["Jack Danger Canty"]
10
- gem.add_dependency "sinatra", ">= 1.0.0"
11
- gem.add_dependency "active_support", ">= 0"
12
- gem.add_dependency "geoip", ">= 0"
13
- gem.add_development_dependency "shoulda", ">= 0"
14
- # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
- end
16
- Jeweler::GemcutterTasks.new
17
- rescue LoadError
18
- puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
19
- end
20
-
21
-
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
22
3
 
23
4
  task :default => :test
24
5
 
@@ -29,9 +10,6 @@ Rake::TestTask.new(:test) do |test|
29
10
  test.verbose = true
30
11
  end
31
12
 
32
-
33
-
34
-
35
13
  namespace :geoip do
36
14
  desc "Update GeoIP City data file"
37
15
  task :update_city_lite => :vendor do
@@ -46,4 +24,4 @@ namespace :geoip do
46
24
  task :vendor do
47
25
  %x{mkdir -p vendor}
48
26
  end
49
- end
27
+ end
data/config.ru CHANGED
@@ -1,8 +1,2 @@
1
- require 'rubygems'
2
- require 'bundler/setup'
3
- require File.expand_path(File.join(File.dirname(__FILE__), 'lib/geoip_server'))
4
-
5
- ## There is no need to set directories here anymore;
6
- ## Just run the application
7
-
1
+ require './lib/geoip_server'
8
2
  run Sinatra::Application
@@ -1,70 +1,20 @@
1
- # Generated by jeweler
2
- # DO NOT EDIT THIS FILE DIRECTLY
3
- # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
- # -*- encoding: utf-8 -*-
5
-
6
1
  Gem::Specification.new do |s|
7
- s.name = %q{geoip_server}
8
- s.version = File.read('VERSION').chomp
9
-
10
- s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
- s.authors = ["Jack Danger Canty", "Fabien Allanic"]
12
- s.date = %q{2011-04-10}
13
- s.description = %q{Query the MaxMind GeoIP data records via a web service}
14
- s.email = %q{gitcommit@6brand.com fabien@foodtree.com}
15
- s.extra_rdoc_files = [
16
- "README.markdown"
17
- ]
18
- s.files = [
19
- "Gemfile",
20
- "Gemfile.lock",
21
- "README.markdown",
22
- "Rakefile",
23
- "VERSION",
24
- "config.ru",
25
- "geoip_server.gemspec",
26
- "lib/geoip_server.rb",
27
- "test.rb"
28
- ]
29
- s.homepage = %q{http://github.com/JackDanger/geoip_server}
30
- s.require_paths = ["lib"]
31
- s.rubygems_version = %q{1.4.2}
32
- s.summary = %q{Query the MaxMind GeoIP data records via a web service}
33
-
34
- if s.respond_to? :specification_version then
35
- s.specification_version = 3
36
-
37
- if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
38
- s.add_runtime_dependency(%q<sinatra>, [">= 0"])
39
- s.add_runtime_dependency(%q<activesupport>, [">= 0"])
40
- s.add_runtime_dependency(%q<geoip>, [">= 0"])
41
- s.add_runtime_dependency(%q<i18n>, [">= 0"])
42
- s.add_runtime_dependency(%q<newrelic_rpm>, [">= 0"])
43
- s.add_runtime_dependency(%q<sinatra>, [">= 1.0.0"])
44
- s.add_runtime_dependency(%q<active_support>, [">= 0"])
45
- s.add_runtime_dependency(%q<geoip>, [">= 0"])
46
- s.add_development_dependency(%q<shoulda>, [">= 0"])
47
- else
48
- s.add_dependency(%q<sinatra>, [">= 0"])
49
- s.add_dependency(%q<activesupport>, [">= 0"])
50
- s.add_dependency(%q<geoip>, [">= 0"])
51
- s.add_dependency(%q<i18n>, [">= 0"])
52
- s.add_dependency(%q<newrelic_rpm>, [">= 0"])
53
- s.add_dependency(%q<sinatra>, [">= 1.0.0"])
54
- s.add_dependency(%q<active_support>, [">= 0"])
55
- s.add_dependency(%q<geoip>, [">= 0"])
56
- s.add_dependency(%q<shoulda>, [">= 0"])
57
- end
58
- else
59
- s.add_dependency(%q<sinatra>, [">= 0"])
60
- s.add_dependency(%q<activesupport>, [">= 0"])
61
- s.add_dependency(%q<geoip>, [">= 0"])
62
- s.add_dependency(%q<i18n>, [">= 0"])
63
- s.add_dependency(%q<newrelic_rpm>, [">= 0"])
64
- s.add_dependency(%q<sinatra>, [">= 1.0.0"])
65
- s.add_dependency(%q<active_support>, [">= 0"])
66
- s.add_dependency(%q<geoip>, [">= 0"])
67
- s.add_dependency(%q<shoulda>, [">= 0"])
68
- end
2
+ s.name = 'geoip_server'
3
+ s.version = '1.3.0'
4
+ s.authors = ["Jack Danger Canty", "Erik Michaels-Ober"]
5
+ s.description = 'Query the MaxMind GeoIP data records via a web service'
6
+ s.email = 'gitcommit@6brand.com'
7
+ s.files = %w(Gemfile Gemfile.lock README.markdown Rakefile config.ru geoip_server.gemspec test.rb)
8
+ s.files += Dir.glob('lib/**/*.rb')
9
+ s.test_files = %w(test.rb)
10
+ s.homepage = 'http://github.com/JackDanger/geoip_server'
11
+ s.require_paths = ['lib']
12
+ s.summary = s.description
13
+ s.add_dependency 'sinatra', '~> 1.1'
14
+ s.add_dependency 'geoip', '~> 1.1'
15
+ s.add_dependency 'multi_json', '~> 1.3'
16
+ s.add_dependency 'newrelic_rpm', '~> 3.4'
17
+ s.add_development_dependency 'rake'
18
+ s.add_development_dependency 'shoulda'
19
+ s.add_development_dependency 'rack-test'
69
20
  end
70
-
@@ -1,14 +1,9 @@
1
- ## Resources
2
- gem 'sinatra', :version => '1.0'
3
1
  require 'sinatra'
4
2
  require 'geoip'
5
- require 'i18n'
6
- require 'active_support'
3
+ require 'multi_json'
7
4
 
8
5
  data_file = File.expand_path(File.join(File.dirname(__FILE__), '..', 'vendor', 'GeoLiteCity.dat'))
9
6
 
10
- ## Application
11
-
12
7
  configure :production do
13
8
  ENV['APP_ROOT'] ||= File.dirname(__FILE__)
14
9
  begin
@@ -35,17 +30,12 @@ get '/' do
35
30
  }
36
31
  end
37
32
 
38
- get '/:ip' do
39
- pass unless params[:ip] =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/
40
-
41
- data = GeoIP.new(data_file).city(params[:ip])
42
-
33
+ get /\/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$/ do |ip|
34
+ data = GeoIP.new(data_file).city(ip)
43
35
  content_type 'application/json;charset=ascii-8bit'
44
- headers['Cache-Control'] = "public; max-age=#{365*24*60*60}"
45
-
36
+ headers['Cache-Control'] = "public; max-age=31536000" # = 365 (days) * 24 (hours) * 60 (minutes) * 60 (seconds)
46
37
  return "{}" unless data
47
-
48
- respond_with(ActiveSupport::JSON.encode(encode(data)))
38
+ respond_with(MultiJson.encode(encode(data)))
49
39
  end
50
40
 
51
41
  def respond_with json
@@ -64,34 +54,33 @@ end
64
54
 
65
55
  def encode data
66
56
  {
67
- # * The host or IP address string as requested
68
- :ip => data.shift,
69
- # * The IP address string after looking up the host
70
- :ip_lookup => data.shift,
71
- # * The GeoIP country-ID as an integer
72
- # :country_id => data.shift,
73
- # * The ISO3166-1 two-character country code
74
- :country_code => data.shift,
75
- # * The ISO3166-2 three-character country code
76
- :country_code_long => data.shift,
77
- # * The ISO3166 English-language name of the country
78
- :country => data.shift,
79
- # * The two-character continent code
80
- :continent => data.shift,
81
- # * The region name
82
- :region => data.shift,
83
- # * The city name
84
- :city => data.shift,
85
- # * The postal code
86
- :postal_code => data.shift,
87
- # * The latitude
88
- :lat => data.shift,
89
- # * The longitude
90
- :lng => data.shift,
91
- # * The USA dma_code and area_code, if available (REV1 City database)
92
- :dma_code => data.shift,
93
- :area_code => data.shift,
57
+ # The host or IP address string as requested
58
+ :ip => data.request,
59
+ # The IP address string after looking up the host
60
+ :ip_lookup => data.ip,
61
+ # The ISO3166-1 two-character country code
62
+ :country_code => data.country_code2,
63
+ # The ISO3166-2 three-character country code
64
+ :country_code_long => data.country_code3,
65
+ # The ISO3166 English-language name of the country
66
+ :country => data.country_name,
67
+ # The two-character continent code
68
+ :continent => data.continent_code,
69
+ # The region name
70
+ :region => data.region_name,
71
+ # The city name
72
+ :city => data.city_name,
73
+ # The postal code
74
+ :postal_code => data.postal_code,
75
+ # The latitude
76
+ :lat => data.latitude,
77
+ # The longitude
78
+ :lng => data.longitude,
79
+ # The USA DMA code, if available
80
+ :dma_code => data.dma_code,
81
+ # The area code, if available
82
+ :area_code => data.area_code,
94
83
  # Timezone, if available
95
- :timezone => data.shift
84
+ :timezone => data.timezone,
96
85
  }
97
86
  end
data/test.rb CHANGED
@@ -52,9 +52,26 @@ class GeoipServerTest < Test::Unit::TestCase
52
52
  end
53
53
  end
54
54
 
55
- context "converting array" do
55
+ context "converting struct" do
56
56
  setup {
57
- array = [
57
+ Struct.new(
58
+ "City",
59
+ :request,
60
+ :ip,
61
+ :country_code2,
62
+ :country_code3,
63
+ :country_name,
64
+ :continent_code,
65
+ :region_name,
66
+ :city_name,
67
+ :postal_code,
68
+ :latitude,
69
+ :longitude,
70
+ :dma_code,
71
+ :area_code,
72
+ :timezone
73
+ ) unless defined? Struct::City
74
+ city = Struct::City.new(
58
75
  "67.161.92.71",
59
76
  "67.161.92.71",
60
77
  "US",
@@ -69,8 +86,8 @@ class GeoipServerTest < Test::Unit::TestCase
69
86
  819,
70
87
  206,
71
88
  "America/Los_Angeles"
72
- ]
73
- @hash = encode(array)
89
+ )
90
+ @hash = encode(city)
74
91
  }
75
92
  should "find city" do
76
93
  assert_equal 'Seattle', @hash[:city]
metadata CHANGED
@@ -1,113 +1,85 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: geoip_server
3
3
  version: !ruby/object:Gem::Version
4
- hash: 31
4
+ hash: 27
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
- - 2
8
+ - 3
9
9
  - 0
10
- version: 1.2.0
10
+ version: 1.3.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Jack Danger Canty
14
- - Fabien Allanic
14
+ - Erik Michaels-Ober
15
15
  autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2011-04-10 00:00:00 Z
19
+ date: 2012-09-14 00:00:00 Z
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
22
- name: sinatra
23
- prerelease: false
24
- requirement: &id001 !ruby/object:Gem::Requirement
22
+ version_requirements: &id001 !ruby/object:Gem::Requirement
25
23
  none: false
26
24
  requirements:
27
- - - ">="
25
+ - - ~>
28
26
  - !ruby/object:Gem::Version
29
- hash: 3
27
+ hash: 13
30
28
  segments:
31
- - 0
32
- version: "0"
33
- type: :runtime
34
- version_requirements: *id001
35
- - !ruby/object:Gem::Dependency
36
- name: activesupport
29
+ - 1
30
+ - 1
31
+ version: "1.1"
37
32
  prerelease: false
38
- requirement: &id002 !ruby/object:Gem::Requirement
39
- none: false
40
- requirements:
41
- - - ">="
42
- - !ruby/object:Gem::Version
43
- hash: 3
44
- segments:
45
- - 0
46
- version: "0"
47
33
  type: :runtime
48
- version_requirements: *id002
34
+ name: sinatra
35
+ requirement: *id001
49
36
  - !ruby/object:Gem::Dependency
50
- name: geoip
51
- prerelease: false
52
- requirement: &id003 !ruby/object:Gem::Requirement
37
+ version_requirements: &id002 !ruby/object:Gem::Requirement
53
38
  none: false
54
39
  requirements:
55
- - - ">="
40
+ - - ~>
56
41
  - !ruby/object:Gem::Version
57
- hash: 3
42
+ hash: 13
58
43
  segments:
59
- - 0
60
- version: "0"
61
- type: :runtime
62
- version_requirements: *id003
63
- - !ruby/object:Gem::Dependency
64
- name: i18n
44
+ - 1
45
+ - 1
46
+ version: "1.1"
65
47
  prerelease: false
66
- requirement: &id004 !ruby/object:Gem::Requirement
67
- none: false
68
- requirements:
69
- - - ">="
70
- - !ruby/object:Gem::Version
71
- hash: 3
72
- segments:
73
- - 0
74
- version: "0"
75
48
  type: :runtime
76
- version_requirements: *id004
49
+ name: geoip
50
+ requirement: *id002
77
51
  - !ruby/object:Gem::Dependency
78
- name: newrelic_rpm
79
- prerelease: false
80
- requirement: &id005 !ruby/object:Gem::Requirement
52
+ version_requirements: &id003 !ruby/object:Gem::Requirement
81
53
  none: false
82
54
  requirements:
83
- - - ">="
55
+ - - ~>
84
56
  - !ruby/object:Gem::Version
85
- hash: 3
57
+ hash: 9
86
58
  segments:
87
- - 0
88
- version: "0"
59
+ - 1
60
+ - 3
61
+ version: "1.3"
62
+ prerelease: false
89
63
  type: :runtime
90
- version_requirements: *id005
64
+ name: multi_json
65
+ requirement: *id003
91
66
  - !ruby/object:Gem::Dependency
92
- name: sinatra
93
- prerelease: false
94
- requirement: &id006 !ruby/object:Gem::Requirement
67
+ version_requirements: &id004 !ruby/object:Gem::Requirement
95
68
  none: false
96
69
  requirements:
97
- - - ">="
70
+ - - ~>
98
71
  - !ruby/object:Gem::Version
99
- hash: 23
72
+ hash: 15
100
73
  segments:
101
- - 1
102
- - 0
103
- - 0
104
- version: 1.0.0
74
+ - 3
75
+ - 4
76
+ version: "3.4"
77
+ prerelease: false
105
78
  type: :runtime
106
- version_requirements: *id006
79
+ name: newrelic_rpm
80
+ requirement: *id004
107
81
  - !ruby/object:Gem::Dependency
108
- name: active_support
109
- prerelease: false
110
- requirement: &id007 !ruby/object:Gem::Requirement
82
+ version_requirements: &id005 !ruby/object:Gem::Requirement
111
83
  none: false
112
84
  requirements:
113
85
  - - ">="
@@ -116,12 +88,12 @@ dependencies:
116
88
  segments:
117
89
  - 0
118
90
  version: "0"
119
- type: :runtime
120
- version_requirements: *id007
121
- - !ruby/object:Gem::Dependency
122
- name: geoip
123
91
  prerelease: false
124
- requirement: &id008 !ruby/object:Gem::Requirement
92
+ type: :development
93
+ name: rake
94
+ requirement: *id005
95
+ - !ruby/object:Gem::Dependency
96
+ version_requirements: &id006 !ruby/object:Gem::Requirement
125
97
  none: false
126
98
  requirements:
127
99
  - - ">="
@@ -130,12 +102,12 @@ dependencies:
130
102
  segments:
131
103
  - 0
132
104
  version: "0"
133
- type: :runtime
134
- version_requirements: *id008
135
- - !ruby/object:Gem::Dependency
136
- name: shoulda
137
105
  prerelease: false
138
- requirement: &id009 !ruby/object:Gem::Requirement
106
+ type: :development
107
+ name: shoulda
108
+ requirement: *id006
109
+ - !ruby/object:Gem::Dependency
110
+ version_requirements: &id007 !ruby/object:Gem::Requirement
139
111
  none: false
140
112
  requirements:
141
113
  - - ">="
@@ -144,26 +116,27 @@ dependencies:
144
116
  segments:
145
117
  - 0
146
118
  version: "0"
119
+ prerelease: false
147
120
  type: :development
148
- version_requirements: *id009
121
+ name: rack-test
122
+ requirement: *id007
149
123
  description: Query the MaxMind GeoIP data records via a web service
150
- email: gitcommit@6brand.com fabien@foodtree.com
124
+ email: gitcommit@6brand.com
151
125
  executables: []
152
126
 
153
127
  extensions: []
154
128
 
155
- extra_rdoc_files:
156
- - README.markdown
129
+ extra_rdoc_files: []
130
+
157
131
  files:
158
132
  - Gemfile
159
133
  - Gemfile.lock
160
134
  - README.markdown
161
135
  - Rakefile
162
- - VERSION
163
136
  - config.ru
164
137
  - geoip_server.gemspec
165
- - lib/geoip_server.rb
166
138
  - test.rb
139
+ - lib/geoip_server.rb
167
140
  homepage: http://github.com/JackDanger/geoip_server
168
141
  licenses: []
169
142
 
@@ -193,9 +166,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
193
166
  requirements: []
194
167
 
195
168
  rubyforge_project:
196
- rubygems_version: 1.8.23
169
+ rubygems_version: 1.8.24
197
170
  signing_key:
198
171
  specification_version: 3
199
172
  summary: Query the MaxMind GeoIP data records via a web service
200
- test_files: []
201
-
173
+ test_files:
174
+ - test.rb
data/VERSION DELETED
@@ -1 +0,0 @@
1
- 1.2.0