heythisisnate-max_mind 0.1.0
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/History.txt +4 -0
- data/Manifest.txt +13 -0
- data/PostInstall.txt +7 -0
- data/README.rdoc +48 -0
- data/Rakefile +44 -0
- data/VERSION +1 -0
- data/lib/autotest/discover.rb +5 -0
- data/lib/max_mind/service/city.rb +29 -0
- data/lib/max_mind/service/city_isp_org.rb +30 -0
- data/lib/max_mind/service/country.rb +25 -0
- data/lib/max_mind/service.rb +55 -0
- data/lib/max_mind.rb +32 -0
- data/max_mind.gemspec +64 -0
- data/script/console +10 -0
- data/script/destroy +14 -0
- data/script/generate +14 -0
- data/spec/city_isp_org_spec.rb +34 -0
- data/spec/city_spec.rb +34 -0
- data/spec/country_spec.rb +34 -0
- data/spec/max_mind_spec.rb +9 -0
- data/spec/service_spec.rb +81 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +10 -0
- data/tasks/rspec.rake +21 -0
- metadata +82 -0
data/History.txt
ADDED
data/Manifest.txt
ADDED
data/PostInstall.txt
ADDED
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 = "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 = ["P J 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
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
@@ -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,55 @@
|
|
1
|
+
module MaxMind
|
2
|
+
class Service
|
3
|
+
class_inheritable_accessor :license_key
|
4
|
+
|
5
|
+
class_inheritable_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
|
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.0.1'
|
32
|
+
end
|
data/max_mind.gemspec
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{max_mind}
|
5
|
+
s.version = "0.1.0"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["P J Kelly", "Nate Clark"]
|
9
|
+
s.date = %q{2009-05-11}
|
10
|
+
s.description = %q{Ruby library for interacting with the MaxMind GeoIP Web Services}
|
11
|
+
s.email = %q{nate@nateclark.com}
|
12
|
+
s.extra_rdoc_files = [
|
13
|
+
"README.rdoc"
|
14
|
+
]
|
15
|
+
s.files = [
|
16
|
+
"History.txt",
|
17
|
+
"Manifest.txt",
|
18
|
+
"PostInstall.txt",
|
19
|
+
"README.rdoc",
|
20
|
+
"Rakefile",
|
21
|
+
"VERSION",
|
22
|
+
"lib/autotest/discover.rb",
|
23
|
+
"lib/max_mind.rb",
|
24
|
+
"lib/max_mind/service.rb",
|
25
|
+
"lib/max_mind/service/city.rb",
|
26
|
+
"lib/max_mind/service/city_isp_org.rb",
|
27
|
+
"lib/max_mind/service/country.rb",
|
28
|
+
"max_mind.gemspec",
|
29
|
+
"script/console",
|
30
|
+
"script/destroy",
|
31
|
+
"script/generate",
|
32
|
+
"spec/city_isp_org_spec.rb",
|
33
|
+
"spec/city_spec.rb",
|
34
|
+
"spec/country_spec.rb",
|
35
|
+
"spec/max_mind_spec.rb",
|
36
|
+
"spec/service_spec.rb",
|
37
|
+
"spec/spec.opts",
|
38
|
+
"spec/spec_helper.rb",
|
39
|
+
"tasks/rspec.rake"
|
40
|
+
]
|
41
|
+
s.homepage = %q{http://github.com/heythisisnate/max_mind}
|
42
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
43
|
+
s.require_paths = ["lib"]
|
44
|
+
s.rubygems_version = %q{1.3.3}
|
45
|
+
s.summary = %q{Ruby library for interacting with the MaxMind GeoIP Web Services}
|
46
|
+
s.test_files = [
|
47
|
+
"spec/city_isp_org_spec.rb",
|
48
|
+
"spec/city_spec.rb",
|
49
|
+
"spec/country_spec.rb",
|
50
|
+
"spec/max_mind_spec.rb",
|
51
|
+
"spec/service_spec.rb",
|
52
|
+
"spec/spec_helper.rb"
|
53
|
+
]
|
54
|
+
|
55
|
+
if s.respond_to? :specification_version then
|
56
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
57
|
+
s.specification_version = 3
|
58
|
+
|
59
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
60
|
+
else
|
61
|
+
end
|
62
|
+
else
|
63
|
+
end
|
64
|
+
end
|
data/script/console
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# File: script/console
|
3
|
+
irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
|
4
|
+
|
5
|
+
libs = " -r irb/completion"
|
6
|
+
# Perhaps use a console_lib to store any extra methods I may want available in the cosole
|
7
|
+
# libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
|
8
|
+
libs << " -r #{File.dirname(__FILE__) + '/../lib/maxmind.rb'}"
|
9
|
+
puts "Loading maxmind gem"
|
10
|
+
exec "#{irb} #{libs} --simple-prompt"
|
data/script/destroy
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'rubigen'
|
6
|
+
rescue LoadError
|
7
|
+
require 'rubygems'
|
8
|
+
require 'rubigen'
|
9
|
+
end
|
10
|
+
require 'rubigen/scripts/destroy'
|
11
|
+
|
12
|
+
ARGV.shift if ['--help', '-h'].include?(ARGV[0])
|
13
|
+
RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
|
14
|
+
RubiGen::Scripts::Destroy.new.run(ARGV)
|
data/script/generate
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'rubigen'
|
6
|
+
rescue LoadError
|
7
|
+
require 'rubygems'
|
8
|
+
require 'rubigen'
|
9
|
+
end
|
10
|
+
require 'rubigen/scripts/generate'
|
11
|
+
|
12
|
+
ARGV.shift if ['--help', '-h'].include?(ARGV[0])
|
13
|
+
RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
|
14
|
+
RubiGen::Scripts::Generate.new.run(ARGV)
|
@@ -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,81 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper.rb'
|
2
|
+
|
3
|
+
describe MaxMind::Service do
|
4
|
+
|
5
|
+
describe "class attributes" do
|
6
|
+
|
7
|
+
it "should have a @@base_url defined" do
|
8
|
+
MaxMind::Service.base_url.should_not be_nil
|
9
|
+
end
|
10
|
+
|
11
|
+
it "@@base_url should be http://geoip1.maxmind.com by default" do
|
12
|
+
MaxMind::Service.base_url.should == 'http://geoip1.maxmind.com'
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should have no license_key initially" do
|
16
|
+
MaxMind::Service.license_key.should == nil
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should have no base_path initially" do
|
20
|
+
MaxMind::Service.base_path.should == nil
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
describe "class and instance methods" do
|
26
|
+
|
27
|
+
before(:each) do
|
28
|
+
@ip = '12.12.12.12'
|
29
|
+
@sample_response = 'US,NY,Brooklyn,11222,40.728001,-73.945297,501,718,"Road Runner","Road Runner"'
|
30
|
+
@base_path = '/z'
|
31
|
+
Net::HTTP.stub!(:get).and_return(@sample_response)
|
32
|
+
MaxMind::Service.stub!(:base_path).and_return(@base_path)
|
33
|
+
end
|
34
|
+
|
35
|
+
describe ".fetch_for_ip" do
|
36
|
+
|
37
|
+
it "should raise a license error if api key is not set" do
|
38
|
+
lambda {
|
39
|
+
MaxMind::Service.fetch_for_ip(@ip)
|
40
|
+
}.should raise_error(MaxMind::LicenseError)
|
41
|
+
end
|
42
|
+
|
43
|
+
describe "with license key" do
|
44
|
+
|
45
|
+
it "should return response" do
|
46
|
+
MaxMind::Service.license_key = '1234'
|
47
|
+
MaxMind::Service.fetch_for_ip(@ip).should == @sample_response
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
53
|
+
|
54
|
+
describe "instance methods" do
|
55
|
+
|
56
|
+
before(:each) do
|
57
|
+
MaxMind::Service.license_key = '1234'
|
58
|
+
@service = MaxMind::Service.new
|
59
|
+
end
|
60
|
+
|
61
|
+
it "should make request" do
|
62
|
+
@service.make_request(@ip).should == @sample_response
|
63
|
+
end
|
64
|
+
|
65
|
+
it "should build a proper path" do
|
66
|
+
query_params = {:meow => '1', :woof => '2'}
|
67
|
+
@service.build_path(query_params).should == "#{@base_path}?#{query_params.to_query_string}"
|
68
|
+
end
|
69
|
+
|
70
|
+
it "valid_response? should ensure response is not blank" do
|
71
|
+
@service = MaxMind::Service.new
|
72
|
+
@service.valid_response?.should be_false
|
73
|
+
@service.response = @sample_response
|
74
|
+
@service.valid_response?.should be_true
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
78
|
+
|
79
|
+
end
|
80
|
+
|
81
|
+
end
|
data/spec/spec.opts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--colour
|
data/spec/spec_helper.rb
ADDED
data/tasks/rspec.rake
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
begin
|
2
|
+
require 'spec'
|
3
|
+
rescue LoadError
|
4
|
+
require 'rubygems'
|
5
|
+
require 'spec'
|
6
|
+
end
|
7
|
+
begin
|
8
|
+
require 'spec/rake/spectask'
|
9
|
+
rescue LoadError
|
10
|
+
puts <<-EOS
|
11
|
+
To use rspec for testing you must install rspec gem:
|
12
|
+
gem install rspec
|
13
|
+
EOS
|
14
|
+
exit(0)
|
15
|
+
end
|
16
|
+
|
17
|
+
desc "Run the specs under spec/models"
|
18
|
+
Spec::Rake::SpecTask.new do |t|
|
19
|
+
t.spec_opts = ['--options', "spec/spec.opts"]
|
20
|
+
t.spec_files = FileList['spec/**/*_spec.rb']
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: heythisisnate-max_mind
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- P J Kelly
|
8
|
+
- Nate Clark
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2009-05-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: nate@nateclark.com
|
19
|
+
executables: []
|
20
|
+
|
21
|
+
extensions: []
|
22
|
+
|
23
|
+
extra_rdoc_files:
|
24
|
+
- README.rdoc
|
25
|
+
files:
|
26
|
+
- History.txt
|
27
|
+
- Manifest.txt
|
28
|
+
- PostInstall.txt
|
29
|
+
- README.rdoc
|
30
|
+
- Rakefile
|
31
|
+
- VERSION
|
32
|
+
- lib/autotest/discover.rb
|
33
|
+
- lib/max_mind.rb
|
34
|
+
- lib/max_mind/service.rb
|
35
|
+
- lib/max_mind/service/city.rb
|
36
|
+
- lib/max_mind/service/city_isp_org.rb
|
37
|
+
- lib/max_mind/service/country.rb
|
38
|
+
- max_mind.gemspec
|
39
|
+
- script/console
|
40
|
+
- script/destroy
|
41
|
+
- script/generate
|
42
|
+
- spec/city_isp_org_spec.rb
|
43
|
+
- spec/city_spec.rb
|
44
|
+
- spec/country_spec.rb
|
45
|
+
- spec/max_mind_spec.rb
|
46
|
+
- spec/service_spec.rb
|
47
|
+
- spec/spec.opts
|
48
|
+
- spec/spec_helper.rb
|
49
|
+
- tasks/rspec.rake
|
50
|
+
has_rdoc: false
|
51
|
+
homepage: http://github.com/heythisisnate/max_mind
|
52
|
+
post_install_message:
|
53
|
+
rdoc_options:
|
54
|
+
- --charset=UTF-8
|
55
|
+
require_paths:
|
56
|
+
- lib
|
57
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: "0"
|
62
|
+
version:
|
63
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: "0"
|
68
|
+
version:
|
69
|
+
requirements: []
|
70
|
+
|
71
|
+
rubyforge_project:
|
72
|
+
rubygems_version: 1.2.0
|
73
|
+
signing_key:
|
74
|
+
specification_version: 3
|
75
|
+
summary: Ruby library for interacting with the MaxMind GeoIP Web Services
|
76
|
+
test_files:
|
77
|
+
- spec/city_isp_org_spec.rb
|
78
|
+
- spec/city_spec.rb
|
79
|
+
- spec/country_spec.rb
|
80
|
+
- spec/max_mind_spec.rb
|
81
|
+
- spec/service_spec.rb
|
82
|
+
- spec/spec_helper.rb
|