crushlovely-max_mind 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt ADDED
@@ -0,0 +1,4 @@
1
+ == 0.0.1 2008-12-05
2
+
3
+ * 1 major enhancement:
4
+ * Initial release
data/Manifest.txt ADDED
@@ -0,0 +1,13 @@
1
+ History.txt
2
+ Manifest.txt
3
+ PostInstall.txt
4
+ README.rdoc
5
+ Rakefile
6
+ lib/maxmind.rb
7
+ script/console
8
+ script/destroy
9
+ script/generate
10
+ spec/maxmind_spec.rb
11
+ spec/spec.opts
12
+ spec/spec_helper.rb
13
+ tasks/rspec.rake
data/PostInstall.txt ADDED
@@ -0,0 +1,7 @@
1
+
2
+ For more information on maxmind, see http://maxmind.rubyforge.org
3
+
4
+ NOTE: Change this information in PostInstall.txt
5
+ You can also delete it if you don't want it.
6
+
7
+
data/README.rdoc ADDED
@@ -0,0 +1,48 @@
1
+ = maxmind
2
+
3
+ * FIX (url)
4
+
5
+ == DESCRIPTION:
6
+
7
+ FIX (describe your package)
8
+
9
+ == FEATURES/PROBLEMS:
10
+
11
+ * FIX (list of features or problems)
12
+
13
+ == SYNOPSIS:
14
+
15
+ FIX (code sample of usage)
16
+
17
+ == REQUIREMENTS:
18
+
19
+ * FIX (list of requirements)
20
+
21
+ == INSTALL:
22
+
23
+ * FIX (sudo gem install, anything else)
24
+
25
+ == LICENSE:
26
+
27
+ (The MIT License)
28
+
29
+ Copyright (c) 2008 FIXME full name
30
+
31
+ Permission is hereby granted, free of charge, to any person obtaining
32
+ a copy of this software and associated documentation files (the
33
+ 'Software'), to deal in the Software without restriction, including
34
+ without limitation the rights to use, copy, modify, merge, publish,
35
+ distribute, sublicense, and/or sell copies of the Software, and to
36
+ permit persons to whom the Software is furnished to do so, subject to
37
+ the following conditions:
38
+
39
+ The above copyright notice and this permission notice shall be
40
+ included in all copies or substantial portions of the Software.
41
+
42
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
43
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
44
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
45
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
46
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
47
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
48
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,44 @@
1
+ %w[rubygems rake rake/clean fileutils newgem rubigen].each { |f| require f }
2
+ require File.dirname(__FILE__) + '/lib/max_mind'
3
+
4
+ # Jeweler 1.0.0
5
+ begin
6
+ require 'jeweler'
7
+ Jeweler::Tasks.new do |gemspec|
8
+ gemspec.name = "max_mind"
9
+ gemspec.summary = "Ruby library for interacting with the MaxMind GeoIP Web Services."
10
+ gemspec.email = ["pj@crushlovely.com", "nate@nateclark.com"]
11
+ gemspec.homepage = "http://github.com/heythisisnate/max_mind"
12
+ gemspec.description = "Ruby library for interacting with the MaxMind GeoIP Web Services."
13
+ gemspec.authors = ["PJ Kelly", "Nate Clark"]
14
+ end
15
+ rescue LoadError
16
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
17
+ end
18
+
19
+
20
+ # # Generate all the Rake tasks
21
+ # # Run 'rake -T' to see list of generated tasks (from gem root directory)
22
+ # $hoe = Hoe.new('max_mind', MaxMind::VERSION) do |p|
23
+ # p.developer('FIXME full name', 'FIXME email')
24
+ # p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
25
+ # p.post_install_message = 'PostInstall.txt' # TODO remove if post-install message not required
26
+ # p.rubyforge_name = p.name # TODO this is default value
27
+ # # p.extra_deps = [
28
+ # # ['activesupport','>= 2.0.2'],
29
+ # # ]
30
+ # p.extra_dev_deps = [
31
+ # ['newgem', ">= #{::Newgem::VERSION}"]
32
+ # ]
33
+ #
34
+ # p.clean_globs |= %w[**/.DS_Store tmp *.log]
35
+ # path = (p.rubyforge_name == p.name) ? p.rubyforge_name : "\#{p.rubyforge_name}/\#{p.name}"
36
+ # p.remote_rdoc_dir = File.join(path.gsub(/^#{p.rubyforge_name}\/?/,''), 'rdoc')
37
+ # p.rsync_args = '-av --delete --ignore-errors'
38
+ # end
39
+
40
+ # require 'newgem/tasks' # load /tasks/*.rake
41
+ Dir['tasks/**/*.rake'].each { |t| load t }
42
+
43
+ # TODO - want other tests/tasks run by default? Add them to the list
44
+ # task :default => [:spec, :features]
data/VERSION.yml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ :major: 1
3
+ :minor: 0
4
+ :patch: 0
@@ -0,0 +1,5 @@
1
+ $:.push(File.join(File.dirname(__FILE__), %w[.. .. rspec]))
2
+
3
+ Autotest.add_discovery do
4
+ "rspec"
5
+ end
data/lib/max_mind.rb ADDED
@@ -0,0 +1,32 @@
1
+ $:.unshift File.dirname(__FILE__)
2
+
3
+ require 'rubygems'
4
+ require 'active_support'
5
+
6
+ require 'uri'
7
+ require 'cgi'
8
+ require 'net/http'
9
+ require 'logger'
10
+ require 'yaml'
11
+ require 'csv'
12
+
13
+ class Hash
14
+ def to_query_string
15
+ self.to_a.collect { |p| "#{p.first.to_s}=#{p.last}" }.join("&")
16
+ end
17
+ end
18
+
19
+ require 'max_mind/service'
20
+ require 'max_mind/service/country'
21
+ require 'max_mind/service/city'
22
+ require 'max_mind/service/city_isp_org'
23
+
24
+ module MaxMind
25
+ class LicenseError < StandardError; end
26
+ class ConfigurationError < StandardError; end
27
+ class ConnectionError < StandardError; end
28
+ class RequestError < StandardError; end
29
+ class ResponseError < StandardError; end
30
+
31
+ VERSION = '0.1.1'
32
+ end
@@ -0,0 +1,55 @@
1
+ module MaxMind
2
+ class Service
3
+ cattr_accessor :license_key
4
+
5
+ cattr_accessor :base_url
6
+ self.base_url = 'http://geoip1.maxmind.com'
7
+
8
+ class_inheritable_accessor :base_path
9
+
10
+ attr_accessor :response
11
+
12
+ def self.fetch_for_ip(ip)
13
+ raise RequestError.new("Cannot make a request without an IP address") if ip.nil?
14
+ service = self.new
15
+ service.make_request(ip)
16
+ service.parsed_response
17
+ end
18
+
19
+ # This method should be implemented in subclasses
20
+ # Here, we're only returning the raw data returned by the server
21
+ def parsed_response
22
+ self.response
23
+ end
24
+
25
+ def make_request(ip)
26
+ if license_key.nil?
27
+ raise LicenseError.new("License Key is missing")
28
+ end
29
+
30
+ uri = URI.parse(base_url)
31
+
32
+ begin
33
+ self.response = Net::HTTP.get(uri.host, build_path(:l => license_key, :i => ip), uri.port)
34
+ rescue EOFError => e
35
+ raise ConnectionError, "The remote server dropped the connection"
36
+ rescue Errno::ECONNRESET => e
37
+ raise ConnectionError, "The remote server reset the connection"
38
+ rescue Errno::ECONNREFUSED => e
39
+ raise ConnectionError, "The remote server refused the connection"
40
+ rescue Timeout::Error, Errno::ETIMEDOUT => e
41
+ raise ConnectionError, "The connection to the remote server timed out"
42
+ end
43
+
44
+ end
45
+
46
+ def build_path(params = {})
47
+ raise RequestError.new("Cannot build a valid request path!") unless !base_path.nil? && params.is_a?(Hash)
48
+ base_path + '?' + params.to_query_string
49
+ end
50
+
51
+ def valid_response?
52
+ !self.response.blank?
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,29 @@
1
+ module MaxMind
2
+ class CityService < Service
3
+
4
+ self.base_path = '/b'
5
+
6
+ def parsed_response
7
+ if self.valid_response?
8
+ parsed_response = CSV.parse_line(self.response)
9
+ {
10
+ :country => parsed_response[0],
11
+ :state => parsed_response[1],
12
+ :city => parsed_response[2],
13
+ :latitude => parsed_response[3],
14
+ :longitude => parsed_response[4],
15
+ :api_response => self.response
16
+ }
17
+ else
18
+ {
19
+ :api_response => self.response
20
+ }
21
+ end
22
+ end
23
+
24
+ def valid_response?
25
+ super && !(self.response =~ /,,,,,(.+?)/)
26
+ end
27
+
28
+ end
29
+ end
@@ -0,0 +1,30 @@
1
+ module MaxMind
2
+ class CityIspOrgService < Service
3
+
4
+ self.base_path = '/f'
5
+
6
+ def parsed_response
7
+ if self.valid_response?
8
+ parsed_response = CSV.parse_line(self.response)
9
+ {
10
+ :country => parsed_response[0],
11
+ :state => parsed_response[1],
12
+ :city => parsed_response[2],
13
+ :postal_code => parsed_response[3],
14
+ :latitude => parsed_response[4],
15
+ :longitude => parsed_response[5],
16
+ :api_response => self.response
17
+ }
18
+ else
19
+ {
20
+ :api_response => self.response
21
+ }
22
+ end
23
+ end
24
+
25
+ def valid_response?
26
+ super && !(self.response =~ /,,,,,,,,,,(.+?)/)
27
+ end
28
+
29
+ end
30
+ end
@@ -0,0 +1,25 @@
1
+ module MaxMind
2
+ class CountryService < Service
3
+
4
+ self.base_path = '/a'
5
+
6
+ def parsed_response
7
+ if self.valid_response?
8
+ parsed_response = CSV.parse_line(self.response)
9
+ {
10
+ :country => parsed_response[0],
11
+ :api_response => self.response
12
+ }
13
+ else
14
+ {
15
+ :api_response => self.response
16
+ }
17
+ end
18
+ end
19
+
20
+ def valid_response?
21
+ super && !(self.response =~ /\(NULL\),(.+?)/)
22
+ end
23
+
24
+ end
25
+ end
@@ -0,0 +1,34 @@
1
+ require File.dirname(__FILE__) + '/spec_helper.rb'
2
+
3
+ describe MaxMind::CityIspOrgService do
4
+
5
+ it "base_path should be /f by default" do
6
+ MaxMind::CityIspOrgService.base_path.should == '/f'
7
+ end
8
+
9
+ describe "response parsing" do
10
+
11
+ before(:each) do
12
+ @ip = '12.12.12.12'
13
+ MaxMind::CityIspOrgService.license_key = '1234'
14
+ end
15
+
16
+ it "should return a hash of values if response is valid" do
17
+ @valid_response = 'US,NY,Brooklyn,11222,40.728001,-73.945297,501,718,"Road Runner","Road Runner"'
18
+ Net::HTTP.stub!(:get).and_return(@valid_response)
19
+ @response = MaxMind::CityIspOrgService.fetch_for_ip(@ip)
20
+ @response.should == {:latitude=>"40.728001", :longitude=>"-73.945297", :country=>"US", :city=>"Brooklyn", :postal_code=>"11222", :state=>"NY", :api_response => @valid_response}
21
+ end
22
+
23
+ ['WHAteVEr', 'g@rbag3', '432153'].each do |r|
24
+ it "should return nil if response is invalid" do
25
+ @invalid_response = ",,,,,,,,,,#{r}"
26
+ Net::HTTP.stub!(:get).and_return(@invalid_response)
27
+ @response = MaxMind::CityIspOrgService.fetch_for_ip(@ip)
28
+ @response.should == {:api_response => @invalid_response}
29
+ end
30
+ end
31
+
32
+ end
33
+
34
+ end
data/spec/city_spec.rb ADDED
@@ -0,0 +1,34 @@
1
+ require File.dirname(__FILE__) + '/spec_helper.rb'
2
+
3
+ describe MaxMind::CityService do
4
+
5
+ it "base_path should be /b by default" do
6
+ MaxMind::CityService.base_path.should == '/b'
7
+ end
8
+
9
+ describe "response parsing" do
10
+
11
+ before(:each) do
12
+ @ip = '12.12.12.12'
13
+ MaxMind::CityService.license_key = '1234'
14
+ end
15
+
16
+ it "should return a hash of values if response is valid" do
17
+ @valid_response = 'US,NY,Brooklyn,40.728001,-73.945297'
18
+ Net::HTTP.stub!(:get).and_return(@valid_response)
19
+ @response = MaxMind::CityService.fetch_for_ip(@ip)
20
+ @response.should == {:latitude=>"40.728001", :longitude=>"-73.945297", :country=>"US", :city=>"Brooklyn", :state=>"NY", :api_response => @valid_response}
21
+ end
22
+
23
+ ['WHAteVEr', 'g@rbag3', '432153'].each do |r|
24
+ it "should return nil if response is invalid" do
25
+ @invalid_response = ",,,,,#{r}"
26
+ Net::HTTP.stub!(:get).and_return(@invalid_response)
27
+ @response = MaxMind::CityService.fetch_for_ip(@ip)
28
+ @response.should == {:api_response => @invalid_response}
29
+ end
30
+ end
31
+
32
+ end
33
+
34
+ end
@@ -0,0 +1,34 @@
1
+ require File.dirname(__FILE__) + '/spec_helper.rb'
2
+
3
+ describe MaxMind::CountryService do
4
+
5
+ it "base_path should be /a by default" do
6
+ MaxMind::CountryService.base_path.should == '/a'
7
+ end
8
+
9
+ describe "response parsing" do
10
+
11
+ before(:each) do
12
+ @ip = '12.12.12.12'
13
+ MaxMind::CountryService.license_key = '1234'
14
+ end
15
+
16
+ it "should return a hash of values if response is valid" do
17
+ @valid_response = 'US'
18
+ Net::HTTP.stub!(:get).and_return(@valid_response)
19
+ @response = MaxMind::CountryService.fetch_for_ip(@ip)
20
+ @response.should == {:country=>"US", :api_response => @valid_response}
21
+ end
22
+
23
+ ['WHAteVEr', 'g@rbag3', '432153'].each do |r|
24
+ it "should return nil if response is invalid" do
25
+ @invalid_response = "(NULL),#{r}"
26
+ Net::HTTP.stub!(:get).and_return(@invalid_response)
27
+ @response = MaxMind::CountryService.fetch_for_ip(@ip)
28
+ @response.should == {:api_response => @invalid_response}
29
+ end
30
+ end
31
+
32
+ end
33
+
34
+ end
@@ -0,0 +1,9 @@
1
+ require File.dirname(__FILE__) + '/spec_helper.rb'
2
+
3
+ describe MaxMind do
4
+
5
+ it "should specify version as a constant" do
6
+ MaxMind::VERSION.should_not be_nil
7
+ end
8
+
9
+ end
@@ -0,0 +1,103 @@
1
+ require File.dirname(__FILE__) + '/spec_helper.rb'
2
+
3
+ describe MaxMind::Service do
4
+
5
+ before(:each) do
6
+ MaxMind::Service.license_key = nil
7
+ end
8
+
9
+ describe "class attributes" do
10
+
11
+ it "should have a @@base_url defined" do
12
+ MaxMind::Service.base_url.should_not be_nil
13
+ end
14
+
15
+ it "@@base_url should be http://geoip1.maxmind.com by default" do
16
+ MaxMind::Service.base_url.should == 'http://geoip1.maxmind.com'
17
+ end
18
+
19
+ it "should have no license_key initially" do
20
+ MaxMind::Service.license_key.should == nil
21
+ end
22
+
23
+ it "should have no base_path initially" do
24
+ MaxMind::Service.base_path.should == nil
25
+ end
26
+
27
+ end
28
+
29
+ describe "class and instance methods" do
30
+
31
+ before(:each) do
32
+ @ip = '12.12.12.12'
33
+ @sample_response = 'US,NY,Brooklyn,11222,40.728001,-73.945297,501,718,"Road Runner","Road Runner"'
34
+ @base_path = '/z'
35
+ Net::HTTP.stub!(:get).and_return(@sample_response)
36
+ MaxMind::Service.stub!(:base_path).and_return(@base_path)
37
+ end
38
+
39
+ describe ".fetch_for_ip" do
40
+
41
+ it "should raise a license error if api key is not set" do
42
+ lambda {
43
+ MaxMind::Service.fetch_for_ip(@ip)
44
+ }.should raise_error(MaxMind::LicenseError)
45
+ end
46
+
47
+ describe "with license key" do
48
+
49
+ it "should return response" do
50
+ MaxMind::Service.license_key = '1234'
51
+ MaxMind::Service.fetch_for_ip(@ip).should == @sample_response
52
+ end
53
+
54
+ end
55
+
56
+ end
57
+
58
+ describe ".license_key=" do
59
+
60
+ it "allows the service subclass to inherit the license key" do
61
+ MaxMind::Service.license_key = 'abcdefg'
62
+ MaxMind::CityService.license_key.should == 'abcdefg'
63
+ end
64
+
65
+ end
66
+
67
+ describe ".base_url=" do
68
+ it "allows the service subclass to inherit the license key" do
69
+ MaxMind::Service.base_url = 'http://geoip3.maxmind.com'
70
+ MaxMind::CityService.base_url.should == 'http://geoip3.maxmind.com'
71
+ end
72
+ end
73
+
74
+
75
+
76
+ describe "instance methods" do
77
+
78
+ before(:each) do
79
+ MaxMind::Service.license_key = '1234'
80
+ @service = MaxMind::Service.new
81
+ end
82
+
83
+ it "should make request" do
84
+ @service.make_request(@ip).should == @sample_response
85
+ end
86
+
87
+ it "should build a proper path" do
88
+ query_params = {:meow => '1', :woof => '2'}
89
+ @service.build_path(query_params).should == "#{@base_path}?#{query_params.to_query_string}"
90
+ end
91
+
92
+ it "valid_response? should ensure response is not blank" do
93
+ @service = MaxMind::Service.new
94
+ @service.valid_response?.should be_false
95
+ @service.response = @sample_response
96
+ @service.valid_response?.should be_true
97
+ end
98
+
99
+ end
100
+
101
+ end
102
+
103
+ end
data/spec/spec.opts ADDED
@@ -0,0 +1 @@
1
+ --colour
@@ -0,0 +1,10 @@
1
+ require File.dirname(__FILE__) + '/../lib/max_mind'
2
+
3
+ begin
4
+ require 'spec'
5
+ rescue LoadError
6
+ require 'rubygems'
7
+ gem 'rspec'
8
+ require 'spec'
9
+ end
10
+
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: crushlovely-max_mind
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - PJ Kelly
8
+ - Nate Clark
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2009-06-11 00:00:00 -07:00
14
+ default_executable:
15
+ dependencies: []
16
+
17
+ description: Ruby library for interacting with the MaxMind GeoIP Web Services.
18
+ email:
19
+ - pj@crushlovely.com
20
+ - nate@nateclark.com
21
+ executables: []
22
+
23
+ extensions: []
24
+
25
+ extra_rdoc_files:
26
+ - README.rdoc
27
+ files:
28
+ - History.txt
29
+ - Manifest.txt
30
+ - PostInstall.txt
31
+ - README.rdoc
32
+ - Rakefile
33
+ - VERSION.yml
34
+ - lib/autotest/discover.rb
35
+ - lib/max_mind.rb
36
+ - lib/max_mind/service.rb
37
+ - lib/max_mind/service/city.rb
38
+ - lib/max_mind/service/city_isp_org.rb
39
+ - lib/max_mind/service/country.rb
40
+ - spec/city_isp_org_spec.rb
41
+ - spec/city_spec.rb
42
+ - spec/country_spec.rb
43
+ - spec/max_mind_spec.rb
44
+ - spec/service_spec.rb
45
+ - spec/spec.opts
46
+ - spec/spec_helper.rb
47
+ has_rdoc: true
48
+ homepage: http://github.com/heythisisnate/max_mind
49
+ post_install_message:
50
+ rdoc_options:
51
+ - --charset=UTF-8
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: "0"
59
+ version:
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: "0"
65
+ version:
66
+ requirements: []
67
+
68
+ rubyforge_project:
69
+ rubygems_version: 1.2.0
70
+ signing_key:
71
+ specification_version: 2
72
+ summary: Ruby library for interacting with the MaxMind GeoIP Web Services.
73
+ test_files:
74
+ - spec/city_isp_org_spec.rb
75
+ - spec/city_spec.rb
76
+ - spec/country_spec.rb
77
+ - spec/max_mind_spec.rb
78
+ - spec/service_spec.rb
79
+ - spec/spec_helper.rb