geo-lite-city 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md ADDED
@@ -0,0 +1,47 @@
1
+ # geo lite city
2
+
3
+ Because sometimes you need just SQL only.
4
+
5
+ ## Installation
6
+
7
+ gem install geo-lite-city
8
+
9
+ ## Usage
10
+
11
+ require "geo"
12
+ Geo.city("8.8.8.8")
13
+
14
+ returns
15
+
16
+ {
17
+ area_code: 650,
18
+ dma_code: 807,
19
+ city_name: "Mountain View",
20
+ continent_code: "NA",
21
+ country_code: "NA.US"
22
+ country_code2: "US",
23
+ country_code3: "USA",
24
+ country_name: "United States",
25
+ ip: "8.8.8.8",
26
+ latitude: 37.385999999999996,
27
+ longitude: -122.0838,
28
+ name: "Mountain View, CA, United States",
29
+ postal_code: "",
30
+ region_code: "NA.US.CA",
31
+ region_name: "CA",
32
+ request: "8.8.8.8",
33
+ timezone: "America/Los_Angeles",
34
+ }
35
+
36
+ ## License
37
+
38
+ The geo-lite-city gem is distributed under the terms of the Modified BSD License, see LICENSE.BSD for details.
39
+
40
+ The file GeoLiteCity.dat, which is distributed with this gem, is (c) MaxMind, distributed under the Creative Commons Attribution-ShareAlike 3.0 Unported License.
41
+
42
+ Using this gem might require you to attribute the usage of the MaxMind databases
43
+ by including the following in all advertising and documentation mentioning
44
+ features of or use of this database:
45
+
46
+ This product includes GeoLite data created by MaxMind, available from
47
+ <a href="http://www.maxmind.com">http://www.maxmind.com</a>.
data/lib/geo.rb ADDED
@@ -0,0 +1,83 @@
1
+ require "geoip"
2
+
3
+ module Geo
4
+ end
5
+
6
+ require "geo/instance"
7
+ require "expectation"
8
+
9
+ module Geo
10
+ extend Instance
11
+
12
+ # lookup the IP in the GeoLiteCity database on city level.
13
+ def self.city(ip)
14
+ # Use some global IP if we are running locally.
15
+ if !ip || ip == "127.0.0.1"
16
+ ip = "www.heise.de"
17
+ end
18
+
19
+ if city = instance.city(ip)
20
+ complete_city(city)
21
+ end
22
+ end
23
+
24
+ private
25
+
26
+ def self.complete_city(city)
27
+ city = city.to_hash
28
+
29
+ parts = city.values_at :city_name, :region_name, :country_name
30
+ city_name = parts.grep(/[a-zA-Z]/).join(", ")
31
+
32
+ parts = city.values_at :continent_code, :country_code2, :region_name
33
+ region_code = parts.grep(/[a-zA-Z]/).join(".")
34
+
35
+ parts = city.values_at :continent_code, :country_code2
36
+ country_code = parts.grep(/[a-zA-Z]/).join(".")
37
+
38
+ city.update name: city_name, region_code: region_code, country_code: country_code
39
+ end
40
+
41
+ public
42
+
43
+ def self.cities(s, &block)
44
+ expect! s => [ Regexp, /../ ]
45
+
46
+ s = Regexp.compile("^" + Regexp.escape(s) + "$") unless s.is_a?(Regexp)
47
+
48
+ quietly do
49
+ r = []
50
+
51
+ instance.each.select do |rec|
52
+ next if rec.city_name !~ s
53
+
54
+ rec = complete_city(rec)
55
+ yield rec if block
56
+ r.push rec
57
+ end
58
+
59
+ r
60
+ end
61
+ end
62
+
63
+ private
64
+
65
+ # quietly and silence_stream are taken from activesupport
66
+
67
+ def self.quietly
68
+ silence_stream(STDOUT) do
69
+ silence_stream(STDERR) do
70
+ yield
71
+ end
72
+ end
73
+ end
74
+
75
+ def self.silence_stream(stream)
76
+ old_stream = stream.dup
77
+ stream.reopen(RbConfig::CONFIG['host_os'] =~ /mswin|mingw/ ? 'NUL:' : '/dev/null')
78
+ stream.sync = true
79
+ yield
80
+ ensure
81
+ stream.reopen(old_stream)
82
+ end
83
+ end
Binary file
data/lib/geo/etest.rb ADDED
@@ -0,0 +1,68 @@
1
+ module Geo::Etest
2
+ def test_geoip_google
3
+ assert_equal Geo.city("8.8.8.8"),
4
+ :request => "8.8.8.8",
5
+ :ip => "8.8.8.8",
6
+ :country_code2 => "US",
7
+ :country_code3 => "USA",
8
+ :country_name => "United States",
9
+ :continent_code => "NA",
10
+ :region_name => "CA",
11
+ :city_name => "Mountain View",
12
+ :postal_code => "",
13
+ :latitude => 37.385999999999996,
14
+ :longitude => -122.0838,
15
+ :dma_code => 807,
16
+ :area_code => 650,
17
+ :timezone => "America/Los_Angeles",
18
+ :name => "Mountain View, CA, United States",
19
+ :region_code => "NA.US.CA",
20
+ :country_code => "NA.US"
21
+ end
22
+
23
+ def test_geoip_heise
24
+ assert_equal Geo.city("127.0.0.1"),
25
+ :request => "www.heise.de",
26
+ :ip => "193.99.144.85",
27
+ :country_code2 => "DE",
28
+ :country_code3 => "DEU",
29
+ :country_name => "Germany",
30
+ :continent_code => "EU",
31
+ :region_name => "06",
32
+ :city_name => "Hanover",
33
+ :postal_code => "",
34
+ :latitude => 52.36670000000001,
35
+ :longitude => 9.716700000000003,
36
+ :dma_code => nil,
37
+ :area_code => nil,
38
+ :timezone => "Europe/Berlin",
39
+ :name => "Hanover, Germany",
40
+ :region_code => "EU.DE",
41
+ :country_code => "EU.DE"
42
+ end
43
+
44
+ def test_geoip_cities
45
+ berlins = Geo.cities("Berlin")
46
+ assert berlins.length > 1
47
+ assert_equal berlins.first, {
48
+ :area_code => nil,
49
+ :city_name => "Berlin",
50
+ :dma_code => nil,
51
+ :country_code => "EU.DE",
52
+ :country_code2 => "DE",
53
+ :country_code3 => "DEU",
54
+ :country_name => "Germany",
55
+ :continent_code => "EU",
56
+ :ip => "",
57
+ :latitude => 52.516699999999986,
58
+ :longitude => 13.400000000000006,
59
+ :name => "Berlin, Germany",
60
+ :postal_code => "",
61
+ :request => "",
62
+ :region_code => "EU.DE",
63
+ :region_name => "16",
64
+ :timezone => "Europe/Berlin"
65
+ }
66
+ end
67
+ end
68
+
@@ -0,0 +1,7 @@
1
+ module Geo::Instance
2
+ private
3
+
4
+ def instance #:nodoc:
5
+ Thread.current[:"geo_instance_instance"] ||= ::GeoIP.new( "#{File.dirname(__FILE__)}/GeoLiteCity.dat")
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ module Geo
2
+ VERSION = "0.0.1"
3
+ end
data/test/geo_test.rb ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env ruby
2
+ require_relative "test_helper"
3
+
4
+ Dir.glob(File.dirname(__FILE__) + "/../lib/**/etest.rb").each do |file|
5
+ load file
6
+ end
7
+
8
+ class GeoTest < Test::Unit::TestCase
9
+ include Geo::Etest
10
+ end
@@ -0,0 +1,10 @@
1
+ $:.unshift File.expand_path("../../lib", __FILE__)
2
+
3
+ require "bundler"
4
+ Bundler.setup(:test)
5
+ require "simplecov"
6
+ SimpleCov.start
7
+
8
+ require "etest-unit"
9
+
10
+ require "geo"
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: geo-lite-city
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - radiospiel
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-07-21 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: geoip
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.2'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.2'
30
+ description: Sometimes you just need SQL.
31
+ email: eno@radiospiel.org
32
+ executables: []
33
+ extensions: []
34
+ extra_rdoc_files: []
35
+ files:
36
+ - lib/geo/etest.rb
37
+ - lib/geo/GeoLiteCity.dat
38
+ - lib/geo/instance.rb
39
+ - lib/geo/version.rb
40
+ - lib/geo.rb
41
+ - README.md
42
+ - test/geo_test.rb
43
+ - test/test_helper.rb
44
+ homepage: http://github.com/radiospiel/geo-lite-city
45
+ licenses: []
46
+ post_install_message:
47
+ rdoc_options: []
48
+ require_paths:
49
+ - lib
50
+ required_ruby_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ segments:
57
+ - 0
58
+ hash: 1806988417301298879
59
+ required_rubygems_version: !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ! '>='
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ requirements: []
66
+ rubyforge_project:
67
+ rubygems_version: 1.8.25
68
+ signing_key:
69
+ specification_version: 3
70
+ summary: Sometimes you just need SQL.
71
+ test_files: []