ivanvc-geolocation_city 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.document +5 -0
- data/.gitignore +9 -0
- data/LICENSE +20 -0
- data/README.rdoc +23 -0
- data/Rakefile +46 -0
- data/VERSION +1 -0
- data/ext/extconf.rb +5 -0
- data/ext/geolocation_city.c +66 -0
- data/ext/geolocation_city.h +15 -0
- data/ivanvc-geolocation_city.gemspec +62 -0
- data/lib/geolocation_city/rake_task.rb +38 -0
- data/spec/geolocation_city_spec.rb +40 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +9 -0
- metadata +104 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Iván Valdés (@ivanvc)
|
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.rdoc
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
= geolocation_city
|
2
|
+
|
3
|
+
== Requirements
|
4
|
+
|
5
|
+
Download the {GeoIP C API from MaxMind}[http://geolite.maxmind.com/download/geoip/api/c/], or install it via homebrew.
|
6
|
+
|
7
|
+
brew install geoip
|
8
|
+
|
9
|
+
==== Updating the .dat file
|
10
|
+
|
11
|
+
Don't forget to include 'geolocation_city/rake_task' to your Rakefile.
|
12
|
+
|
13
|
+
rake geolocation:update target=Test.dat
|
14
|
+
|
15
|
+
==== Getting localization from an IP
|
16
|
+
|
17
|
+
require 'geolocation_city'
|
18
|
+
geo = Geolocation::City.new('Test.dat')
|
19
|
+
geo.from_ip('24.24.24.24')
|
20
|
+
|
21
|
+
== Copyright
|
22
|
+
|
23
|
+
Copyright (c) 2010 Iván Valdés (@ivanvc). See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "ivanvc-geolocation_city"
|
8
|
+
gem.summary = %Q{GeoLocation by City from Maxmind}
|
9
|
+
gem.description = %Q{Ruby C wrapper for location by city.}
|
10
|
+
gem.email = "iv@nvald.es"
|
11
|
+
gem.homepage = "http://github.com/ivanvc/geolocation_city"
|
12
|
+
gem.authors = ["Iván Valdés (@ivanvc)"]
|
13
|
+
gem.add_development_dependency "rspec", ">= 1.2.9"
|
14
|
+
gem.add_dependency "rake", ">= 0.8.7"
|
15
|
+
# gem is a Gem::Specification... see for additional settings
|
16
|
+
end
|
17
|
+
Jeweler::GemcutterTasks.new
|
18
|
+
rescue LoadError
|
19
|
+
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
20
|
+
end
|
21
|
+
|
22
|
+
require 'spec/rake/spectask'
|
23
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
24
|
+
spec.libs << 'lib' << 'spec'
|
25
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
26
|
+
end
|
27
|
+
|
28
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
29
|
+
spec.libs << 'lib' << 'spec'
|
30
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
31
|
+
spec.rcov = true
|
32
|
+
end
|
33
|
+
|
34
|
+
task :spec => :check_dependencies
|
35
|
+
|
36
|
+
task :default => :spec
|
37
|
+
|
38
|
+
require 'rake/rdoctask'
|
39
|
+
Rake::RDocTask.new do |rdoc|
|
40
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
41
|
+
|
42
|
+
rdoc.rdoc_dir = 'rdoc'
|
43
|
+
rdoc.title = "geolocation_city #{version}"
|
44
|
+
rdoc.rdoc_files.include('README*')
|
45
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
46
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1
|
data/ext/extconf.rb
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
#import "geolocation_city.h"
|
2
|
+
|
3
|
+
static const char *_value_or_unknown(const char *ptr)
|
4
|
+
{
|
5
|
+
return ptr ? ptr : "Unknown";
|
6
|
+
}
|
7
|
+
|
8
|
+
static VALUE rb_city_initialize(VALUE rb_module, VALUE location)
|
9
|
+
{
|
10
|
+
file_location = STR2CSTR(location);
|
11
|
+
}
|
12
|
+
|
13
|
+
static VALUE rb_city_set_file_location(VALUE rb_module, VALUE location)
|
14
|
+
{
|
15
|
+
file_location = STR2CSTR(location);
|
16
|
+
}
|
17
|
+
|
18
|
+
static VALUE rb_city_get_file_location(VALUE rb_module)
|
19
|
+
{
|
20
|
+
return rb_str_new2(file_location);
|
21
|
+
}
|
22
|
+
|
23
|
+
static VALUE rb_city_from_ip(VALUE rb_module, VALUE host)
|
24
|
+
{
|
25
|
+
GeoIP *gi;
|
26
|
+
GeoIPRecord *gir;
|
27
|
+
VALUE city;
|
28
|
+
|
29
|
+
gi = GeoIP_open(file_location, GEOIP_INDEX_CACHE);
|
30
|
+
if (gi == NULL)
|
31
|
+
{
|
32
|
+
rb_raise(rb_eRuntimeError, "Unable to open geolocation file.");
|
33
|
+
}
|
34
|
+
|
35
|
+
gir = GeoIP_record_by_name(gi, STR2CSTR(host));
|
36
|
+
if (gir != NULL)
|
37
|
+
{
|
38
|
+
city = rb_str_new2(_value_or_unknown(gir->city));
|
39
|
+
GeoIPRecord_delete(gir);
|
40
|
+
} else
|
41
|
+
{
|
42
|
+
city = rb_str_new2("Unknown");
|
43
|
+
}
|
44
|
+
GeoIP_delete(gi);
|
45
|
+
|
46
|
+
return city;
|
47
|
+
}
|
48
|
+
|
49
|
+
static VALUE rb_city_to_s(VALUE rb_module)
|
50
|
+
{
|
51
|
+
char buf[256];
|
52
|
+
sprintf(buf, "#<%s:%p file_location=\"%s\">", rb_obj_classname(rb_module), (void*)rb_module, file_location);
|
53
|
+
return rb_str_new(buf, strlen(buf));
|
54
|
+
}
|
55
|
+
|
56
|
+
void Init_geolocation_city()
|
57
|
+
{
|
58
|
+
rb_mGeolocation = rb_define_module("Geolocation");
|
59
|
+
rb_cCity = rb_define_class_under(rb_mGeolocation, "City", rb_cObject);
|
60
|
+
|
61
|
+
rb_define_method(rb_cCity, "initialize", rb_city_initialize, 1);
|
62
|
+
rb_define_method(rb_cCity, "file_location=", rb_city_set_file_location, 1);
|
63
|
+
rb_define_method(rb_cCity, "file_location", rb_city_get_file_location, 0);
|
64
|
+
rb_define_method(rb_cCity, "from_ip", rb_city_from_ip, 1);
|
65
|
+
rb_define_method(rb_cCity, "to_s", rb_city_to_s, 0);
|
66
|
+
}
|
@@ -0,0 +1,15 @@
|
|
1
|
+
#include "ruby.h"
|
2
|
+
#include "version.h"
|
3
|
+
#include "GeoIP.h"
|
4
|
+
#include "GeoIPCity.h"
|
5
|
+
|
6
|
+
static char *file_location;
|
7
|
+
static const char *_value_or_unknown(const char *p);
|
8
|
+
|
9
|
+
static VALUE rb_mGeolocation;
|
10
|
+
static VALUE rb_cCity;
|
11
|
+
static VALUE rb_city_initialize(VALUE rb_module, VALUE location);
|
12
|
+
static VALUE rb_city_from_ip(VALUE rb_module, VALUE host);
|
13
|
+
static VALUE rb_city_set_file_location(VALUE rb_module, VALUE location);
|
14
|
+
static VALUE rb_city_get_file_location(VALUE rb_module);
|
15
|
+
static VALUE rb_city_to_s(VALUE rb_module);
|
@@ -0,0 +1,62 @@
|
|
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{ivanvc-geolocation_city}
|
8
|
+
s.version = "0.0.1"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Iv\303\241n Vald\303\251s (@ivanvc)"]
|
12
|
+
s.date = %q{2010-07-07}
|
13
|
+
s.description = %q{Ruby C wrapper for location by city.}
|
14
|
+
s.email = %q{iv@nvald.es}
|
15
|
+
s.extensions = ["ext/extconf.rb"]
|
16
|
+
s.extra_rdoc_files = [
|
17
|
+
"LICENSE",
|
18
|
+
"README.rdoc"
|
19
|
+
]
|
20
|
+
s.files = [
|
21
|
+
".document",
|
22
|
+
".gitignore",
|
23
|
+
"LICENSE",
|
24
|
+
"README.rdoc",
|
25
|
+
"Rakefile",
|
26
|
+
"VERSION",
|
27
|
+
"ext/extconf.rb",
|
28
|
+
"ext/geolocation_city.c",
|
29
|
+
"ext/geolocation_city.h",
|
30
|
+
"ivanvc-geolocation_city.gemspec",
|
31
|
+
"lib/geolocation_city/rake_task.rb",
|
32
|
+
"spec/geolocation_city_spec.rb",
|
33
|
+
"spec/spec.opts",
|
34
|
+
"spec/spec_helper.rb"
|
35
|
+
]
|
36
|
+
s.homepage = %q{http://github.com/ivanvc/geolocation_city}
|
37
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
38
|
+
s.require_paths = ["lib"]
|
39
|
+
s.rubygems_version = %q{1.3.6}
|
40
|
+
s.summary = %q{GeoLocation by City from Maxmind}
|
41
|
+
s.test_files = [
|
42
|
+
"spec/geolocation_city_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::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
51
|
+
s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
|
52
|
+
s.add_runtime_dependency(%q<rake>, [">= 0.8.7"])
|
53
|
+
else
|
54
|
+
s.add_dependency(%q<rspec>, [">= 1.2.9"])
|
55
|
+
s.add_dependency(%q<rake>, [">= 0.8.7"])
|
56
|
+
end
|
57
|
+
else
|
58
|
+
s.add_dependency(%q<rspec>, [">= 1.2.9"])
|
59
|
+
s.add_dependency(%q<rake>, [">= 0.8.7"])
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'rake'
|
2
|
+
|
3
|
+
namespace :geolocation do
|
4
|
+
desc 'Update the Geolocation database. Specify target= to set the target.'
|
5
|
+
task :update do
|
6
|
+
require 'open-uri'
|
7
|
+
require 'zlib'
|
8
|
+
require 'stringio'
|
9
|
+
|
10
|
+
raise 'Please specify the target' unless ENV['target']
|
11
|
+
file = ENV['target']
|
12
|
+
tmp_file = file + '.tmp'
|
13
|
+
|
14
|
+
url = ENV['url'] || "http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz"
|
15
|
+
content = nil
|
16
|
+
open(url) do |io|
|
17
|
+
compr = StringIO.new(raw = io.read)
|
18
|
+
if compr.read(2) == "\x1f\x8b"
|
19
|
+
compr.rewind
|
20
|
+
gz = Zlib::GzipReader.new(compr)
|
21
|
+
content = gz.read
|
22
|
+
gz.close
|
23
|
+
else
|
24
|
+
content = raw
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
begin
|
29
|
+
File.open(tmp_file, 'w') do |f|
|
30
|
+
f.write(content)
|
31
|
+
end
|
32
|
+
File.unlink(file) if FileTest.exists?(file)
|
33
|
+
File.rename(tmp_file, file)
|
34
|
+
rescue
|
35
|
+
raise "Error: Problem updating file! #{$!} (#{$!.class})"
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe GeoLocation::City do
|
4
|
+
before(:each) do
|
5
|
+
@city = GeoLocation::City.new(File.expand_path '../data/GeoLiteCity.dat')
|
6
|
+
end
|
7
|
+
|
8
|
+
describe "initialization" do
|
9
|
+
it "should set the file location" do
|
10
|
+
@city.file_location.should == File.expand_path('../data/GeoLiteCity.dat')
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe ".file_location" do
|
15
|
+
it "should be able to change it" do
|
16
|
+
@city.file_location = ''
|
17
|
+
|
18
|
+
@city.file_location.should == ''
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe ".from_ip" do
|
23
|
+
it "should resolve to Guadalajara" do
|
24
|
+
@city.from_ip('200.92.113.112').should == 'Guadalajara'
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should resolve to Syracuse" do
|
28
|
+
@city.from_ip('24.24.24.24').should == 'Syracuse'
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should not resolve an unknown IP" do
|
32
|
+
@city.from_ip('0.0.0.0').should == 'Unknown'
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should raise an exception if file not found" do
|
36
|
+
@city.file_location = ''
|
37
|
+
lambda { @city.from_ip('0.0.0.0') }.should raise_error
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
data/spec/spec.opts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
2
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
3
|
+
require 'spec'
|
4
|
+
require 'spec/autorun'
|
5
|
+
require "#{File.expand_path(File.dirname(__FILE__))}/../ext/geo_location_city.o"
|
6
|
+
|
7
|
+
Spec::Runner.configure do |config|
|
8
|
+
|
9
|
+
end
|
metadata
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ivanvc-geolocation_city
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
version: 0.0.1
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- "Iv\xC3\xA1n Vald\xC3\xA9s (@ivanvc)"
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-07-07 00:00:00 -05:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: rspec
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 1
|
29
|
+
- 2
|
30
|
+
- 9
|
31
|
+
version: 1.2.9
|
32
|
+
type: :development
|
33
|
+
version_requirements: *id001
|
34
|
+
- !ruby/object:Gem::Dependency
|
35
|
+
name: rake
|
36
|
+
prerelease: false
|
37
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
segments:
|
42
|
+
- 0
|
43
|
+
- 8
|
44
|
+
- 7
|
45
|
+
version: 0.8.7
|
46
|
+
type: :runtime
|
47
|
+
version_requirements: *id002
|
48
|
+
description: Ruby C wrapper for location by city.
|
49
|
+
email: iv@nvald.es
|
50
|
+
executables: []
|
51
|
+
|
52
|
+
extensions:
|
53
|
+
- ext/extconf.rb
|
54
|
+
extra_rdoc_files:
|
55
|
+
- LICENSE
|
56
|
+
- README.rdoc
|
57
|
+
files:
|
58
|
+
- .document
|
59
|
+
- .gitignore
|
60
|
+
- LICENSE
|
61
|
+
- README.rdoc
|
62
|
+
- Rakefile
|
63
|
+
- VERSION
|
64
|
+
- ext/extconf.rb
|
65
|
+
- ext/geolocation_city.c
|
66
|
+
- ext/geolocation_city.h
|
67
|
+
- ivanvc-geolocation_city.gemspec
|
68
|
+
- lib/geolocation_city/rake_task.rb
|
69
|
+
- spec/geolocation_city_spec.rb
|
70
|
+
- spec/spec.opts
|
71
|
+
- spec/spec_helper.rb
|
72
|
+
has_rdoc: true
|
73
|
+
homepage: http://github.com/ivanvc/geolocation_city
|
74
|
+
licenses: []
|
75
|
+
|
76
|
+
post_install_message:
|
77
|
+
rdoc_options:
|
78
|
+
- --charset=UTF-8
|
79
|
+
require_paths:
|
80
|
+
- lib
|
81
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
82
|
+
requirements:
|
83
|
+
- - ">="
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
segments:
|
86
|
+
- 0
|
87
|
+
version: "0"
|
88
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
89
|
+
requirements:
|
90
|
+
- - ">="
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
segments:
|
93
|
+
- 0
|
94
|
+
version: "0"
|
95
|
+
requirements: []
|
96
|
+
|
97
|
+
rubyforge_project:
|
98
|
+
rubygems_version: 1.3.6
|
99
|
+
signing_key:
|
100
|
+
specification_version: 3
|
101
|
+
summary: GeoLocation by City from Maxmind
|
102
|
+
test_files:
|
103
|
+
- spec/geolocation_city_spec.rb
|
104
|
+
- spec/spec_helper.rb
|