where 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +4 -0
- data/LICENSE +20 -0
- data/README.rdoc +33 -0
- data/Rakefile +53 -0
- data/VERSION +1 -0
- data/Where.gemspec +57 -0
- data/lib/where/address.rb +58 -0
- data/lib/where/base.rb +70 -0
- data/lib/where/ip_address.rb +33 -0
- data/lib/where.rb +14 -0
- data/test/helper.rb +10 -0
- data/test/test_where.rb +86 -0
- metadata +90 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Matt Venables
|
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,33 @@
|
|
1
|
+
= Where
|
2
|
+
|
3
|
+
A basic geolocation library for Ruby.
|
4
|
+
|
5
|
+
== Installation
|
6
|
+
|
7
|
+
gem install 'where'
|
8
|
+
|
9
|
+
== Usage
|
10
|
+
|
11
|
+
Search by Address:
|
12
|
+
|
13
|
+
result = Where.is '4 yawkey way boston ma'
|
14
|
+
if result.success?
|
15
|
+
puts "Address: #{result.address}"
|
16
|
+
puts "Street Number: #{result.street_number}"
|
17
|
+
puts "Street: #{result.street}"
|
18
|
+
puts "City: #{result.city}"
|
19
|
+
puts "State: #{result.state}"
|
20
|
+
puts "Zip: #{result.postal_code}"
|
21
|
+
puts "Country: #{result.country}"
|
22
|
+
puts "Lat: #{result.lat}"
|
23
|
+
puts "Lng: #{result.lng}"
|
24
|
+
end
|
25
|
+
|
26
|
+
Or, by IP-Address:
|
27
|
+
|
28
|
+
result = Where.is "173.194.33.104"
|
29
|
+
...
|
30
|
+
|
31
|
+
== Copyright
|
32
|
+
|
33
|
+
Copyright (c) 2010 Matt Venables. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "where"
|
8
|
+
gem.summary = %Q{A simple geolocation library.}
|
9
|
+
gem.description = %Q{A very simple geolocation library for searching based on postal address or ip address.}
|
10
|
+
gem.email = "matt@veg.etabl.es"
|
11
|
+
gem.homepage = "http://github.com/vegetables/where"
|
12
|
+
gem.authors = ["Matt Venables"]
|
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
|
+
require 'rake/testtask'
|
22
|
+
Rake::TestTask.new(:test) do |test|
|
23
|
+
test.libs << 'lib' << 'test'
|
24
|
+
test.pattern = 'test/**/test_*.rb'
|
25
|
+
test.verbose = true
|
26
|
+
end
|
27
|
+
|
28
|
+
begin
|
29
|
+
require 'rcov/rcovtask'
|
30
|
+
Rcov::RcovTask.new do |test|
|
31
|
+
test.libs << 'test'
|
32
|
+
test.pattern = 'test/**/test_*.rb'
|
33
|
+
test.verbose = true
|
34
|
+
end
|
35
|
+
rescue LoadError
|
36
|
+
task :rcov do
|
37
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
task :test => :check_dependencies
|
42
|
+
|
43
|
+
task :default => :test
|
44
|
+
|
45
|
+
require 'rake/rdoctask'
|
46
|
+
Rake::RDocTask.new do |rdoc|
|
47
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
48
|
+
|
49
|
+
rdoc.rdoc_dir = 'rdoc'
|
50
|
+
rdoc.title = "where #{version}"
|
51
|
+
rdoc.rdoc_files.include('README*')
|
52
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
53
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1
|
data/Where.gemspec
ADDED
@@ -0,0 +1,57 @@
|
|
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{where}
|
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 = ["Matt Venables"]
|
12
|
+
s.date = %q{2010-10-03}
|
13
|
+
s.description = %q{A very simple geolocation library for searching based on postal address or ip address.}
|
14
|
+
s.email = %q{matt@veg.etabl.es}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".gitignore",
|
22
|
+
"LICENSE",
|
23
|
+
"README.rdoc",
|
24
|
+
"Rakefile",
|
25
|
+
"VERSION",
|
26
|
+
"Where.gemspec",
|
27
|
+
"lib/where.rb",
|
28
|
+
"lib/where/address.rb",
|
29
|
+
"lib/where/base.rb",
|
30
|
+
"lib/where/ip_address.rb",
|
31
|
+
"test/helper.rb",
|
32
|
+
"test/test_where.rb"
|
33
|
+
]
|
34
|
+
s.homepage = %q{http://github.com/vegetables/where}
|
35
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
36
|
+
s.require_paths = ["lib"]
|
37
|
+
s.rubygems_version = %q{1.3.7}
|
38
|
+
s.summary = %q{A simple geolocation library.}
|
39
|
+
s.test_files = [
|
40
|
+
"test/helper.rb",
|
41
|
+
"test/test_where.rb"
|
42
|
+
]
|
43
|
+
|
44
|
+
if s.respond_to? :specification_version then
|
45
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
46
|
+
s.specification_version = 3
|
47
|
+
|
48
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
49
|
+
s.add_development_dependency(%q<shoulda>, [">= 0"])
|
50
|
+
else
|
51
|
+
s.add_dependency(%q<shoulda>, [">= 0"])
|
52
|
+
end
|
53
|
+
else
|
54
|
+
s.add_dependency(%q<shoulda>, [">= 0"])
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
@@ -0,0 +1,58 @@
|
|
1
|
+
module Where
|
2
|
+
class Address < Where::Base
|
3
|
+
class << self
|
4
|
+
GEOCODER_URL = 'http://maps.googleapis.com/maps/api/geocode/json?sensor=false&address='
|
5
|
+
|
6
|
+
def geocode(address, api_url=nil)
|
7
|
+
super(address, api_url || GEOCODER_URL)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def results=(results_array = [])
|
12
|
+
results_array.each { |result| self.new_attributes = result }
|
13
|
+
end
|
14
|
+
|
15
|
+
def types=(val = [])
|
16
|
+
@accuracy = determine_type(val)
|
17
|
+
end
|
18
|
+
|
19
|
+
def formatted_address=(val)
|
20
|
+
@address = val
|
21
|
+
end
|
22
|
+
|
23
|
+
def address_components=(components = [])
|
24
|
+
components.each do |component|
|
25
|
+
type = determine_type component['types']
|
26
|
+
next if type.empty?
|
27
|
+
if %w(region country).include? type
|
28
|
+
self.send "#{type}_code=", component['short_name']
|
29
|
+
self.send "#{type}_name=", component['long_name']
|
30
|
+
else
|
31
|
+
self.send "#{type}=", component['long_name']
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def geometry=(val = {})
|
37
|
+
self.new_attributes = val['location']
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
def determine_type(val = [])
|
43
|
+
{ 'street_address' => 'street_address',
|
44
|
+
'street_number' => 'street_number',
|
45
|
+
'route' => 'street',
|
46
|
+
'neighborhood' => 'neighborhood',
|
47
|
+
'locality' => 'city',
|
48
|
+
'administrative_area_level_1' => 'region',
|
49
|
+
'postal_code' => 'postal_code',
|
50
|
+
'country' => 'country'
|
51
|
+
}.each do |k, v|
|
52
|
+
return v if val.include? k
|
53
|
+
end
|
54
|
+
""
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
end
|
data/lib/where/base.rb
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
require 'uri'
|
2
|
+
require 'net/http'
|
3
|
+
require 'active_support'
|
4
|
+
|
5
|
+
module Where
|
6
|
+
class Base
|
7
|
+
attr_accessor :street_number, :street, :city, :neighborhood, :postal_code,
|
8
|
+
:region_code, :region_name, :accuracy,
|
9
|
+
:country_code, :country_name, :lat, :lng
|
10
|
+
|
11
|
+
class << self
|
12
|
+
def geocode(address, api_url = '')
|
13
|
+
begin
|
14
|
+
url = api_url + URI.escape(address)
|
15
|
+
response = Net::HTTP.get_response(URI.parse(url))
|
16
|
+
self.new response.body
|
17
|
+
rescue
|
18
|
+
self.new
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def initialize(data = "")
|
24
|
+
opts = ActiveSupport::JSON.decode(data)
|
25
|
+
return if opts.empty?
|
26
|
+
self.new_attributes = opts
|
27
|
+
self
|
28
|
+
end
|
29
|
+
|
30
|
+
def new_attributes=(opts = {})
|
31
|
+
opts.each do |k,v|
|
32
|
+
var_name = ActiveSupport::Inflector.underscore(k)
|
33
|
+
if self.respond_to?("#{var_name}=")
|
34
|
+
self.send "#{var_name}=", v
|
35
|
+
else
|
36
|
+
self.instance_variable_set("@#{var_name}", v)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
# Did the request return at least one result?
|
42
|
+
def success?
|
43
|
+
@status == 'OK' || !@region_code.nil?
|
44
|
+
end
|
45
|
+
|
46
|
+
def street_address
|
47
|
+
arr = [street_number, street].compact
|
48
|
+
arr.empty? ? nil : arr.join(" ")
|
49
|
+
end
|
50
|
+
|
51
|
+
def address
|
52
|
+
arr = [street_address, city, region(true), postal_code, country(true)].compact
|
53
|
+
arr.empty? ? nil : arr.join(", ")
|
54
|
+
end
|
55
|
+
|
56
|
+
def city(use_neighborhood = false)
|
57
|
+
@neighborhood && use_neighborhood ? @neighborhood : @city
|
58
|
+
end
|
59
|
+
|
60
|
+
def country(short_version = false)
|
61
|
+
short_version ? @country_code : @country_name
|
62
|
+
end
|
63
|
+
|
64
|
+
def region(short_version = false)
|
65
|
+
short_version ? @region_code : @region_name
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module Where
|
2
|
+
class IpAddress < Where::Base
|
3
|
+
class << self
|
4
|
+
GEOCODER_URL = 'http://www.geoplugin.net/json.gp?ip='
|
5
|
+
|
6
|
+
def geocode(address, api_url=nil)
|
7
|
+
super(address, api_url || GEOCODER_URL)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def initialize(body="")
|
12
|
+
data = body[10..-2] || ""
|
13
|
+
super(data.empty? ? data : data.gsub('geoplugin_', ''))
|
14
|
+
end
|
15
|
+
|
16
|
+
def latitude=(val)
|
17
|
+
@lat = val
|
18
|
+
end
|
19
|
+
|
20
|
+
def longitude=(val)
|
21
|
+
@lng = val
|
22
|
+
end
|
23
|
+
|
24
|
+
def accuracy
|
25
|
+
return 'street_address' unless street.nil?
|
26
|
+
return 'city' unless city.nil?
|
27
|
+
return 'region' unless region.nil?
|
28
|
+
return 'country' unless country.nil?
|
29
|
+
""
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
end
|
data/lib/where.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'where/base'
|
2
|
+
require 'where/address'
|
3
|
+
require 'where/ip_address'
|
4
|
+
|
5
|
+
module Where
|
6
|
+
def self.is(address = "")
|
7
|
+
address ||= ""
|
8
|
+
if address.match(/^(\d{1,3}\.){3}\d{1,3}$/)
|
9
|
+
Where::IpAddress.geocode address
|
10
|
+
else
|
11
|
+
Where::Address.geocode address
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
data/test/helper.rb
ADDED
data/test/test_where.rb
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class TestWhere < Test::Unit::TestCase
|
4
|
+
|
5
|
+
should "build an empty object when given a nil address" do
|
6
|
+
result = Where.is(nil)
|
7
|
+
assert result.accuracy.nil?
|
8
|
+
assert result.address.nil?
|
9
|
+
assert result.street_number.nil?
|
10
|
+
assert result.street.nil?
|
11
|
+
assert result.city.nil?
|
12
|
+
assert result.region.nil?
|
13
|
+
assert result.postal_code.nil?
|
14
|
+
assert result.country.nil?
|
15
|
+
assert result.lat.nil?
|
16
|
+
assert result.lng.nil?
|
17
|
+
end
|
18
|
+
|
19
|
+
should "build an empty object when given an empty address" do
|
20
|
+
result = Where.is("")
|
21
|
+
assert result.accuracy.nil?
|
22
|
+
assert result.address.nil?
|
23
|
+
assert result.street_number.nil?
|
24
|
+
assert result.street.nil?
|
25
|
+
assert result.city.nil?
|
26
|
+
assert result.region.nil?
|
27
|
+
assert result.postal_code.nil?
|
28
|
+
assert result.country.nil?
|
29
|
+
assert result.lat.nil?
|
30
|
+
assert result.lng.nil?
|
31
|
+
end
|
32
|
+
|
33
|
+
should "build a valid geolocation object when given a street address" do
|
34
|
+
result = Where.is("4 yawkey way boston ma")
|
35
|
+
assert result.accuracy == 'street_address'
|
36
|
+
assert !result.address.nil?
|
37
|
+
assert result.street_number == '4'
|
38
|
+
assert result.street == 'Yawkey Way'
|
39
|
+
assert result.city == 'Boston'
|
40
|
+
assert result.city(true) == 'Boston'
|
41
|
+
assert result.region == 'Massachusetts'
|
42
|
+
assert result.region(true) == 'MA'
|
43
|
+
assert result.postal_code == '02215'
|
44
|
+
assert result.country == 'United States'
|
45
|
+
assert result.country(true) == 'US'
|
46
|
+
assert !result.lat.nil?
|
47
|
+
assert !result.lng.nil?
|
48
|
+
end
|
49
|
+
|
50
|
+
should "build a valid geolocation object when given an address in a neighborhood" do
|
51
|
+
result = Where.is("brighton ma")
|
52
|
+
assert result.accuracy == 'neighborhood'
|
53
|
+
assert !result.address.nil?
|
54
|
+
assert result.street_number.nil?
|
55
|
+
assert result.street.nil?
|
56
|
+
assert result.city == 'Boston'
|
57
|
+
assert result.city(true) == 'Brighton'
|
58
|
+
assert result.region == 'Massachusetts'
|
59
|
+
assert result.region(true) == 'MA'
|
60
|
+
assert result.postal_code.nil?
|
61
|
+
assert result.country == 'United States'
|
62
|
+
assert result.country(true) == 'US'
|
63
|
+
assert !result.lat.nil?
|
64
|
+
assert !result.lng.nil?
|
65
|
+
end
|
66
|
+
|
67
|
+
|
68
|
+
should "build a valid geolocation object when given an ip address" do
|
69
|
+
result = Where.is("173.194.33.104")
|
70
|
+
assert result.accuracy == 'city'
|
71
|
+
assert !result.address.nil?
|
72
|
+
assert result.street_number.nil?
|
73
|
+
assert result.street.nil?
|
74
|
+
assert result.city == 'Mountain View'
|
75
|
+
assert result.city(true) == 'Mountain View'
|
76
|
+
assert result.region == 'California'
|
77
|
+
assert result.region(true) == 'CA'
|
78
|
+
assert result.postal_code.nil?
|
79
|
+
assert result.country == 'United States'
|
80
|
+
assert result.country(true) == 'US'
|
81
|
+
assert !result.lat.nil?
|
82
|
+
assert !result.lng.nil?
|
83
|
+
end
|
84
|
+
|
85
|
+
|
86
|
+
end
|
metadata
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: where
|
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
|
+
- Matt Venables
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-10-03 00:00:00 -04:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: shoulda
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
segments:
|
29
|
+
- 0
|
30
|
+
version: "0"
|
31
|
+
type: :development
|
32
|
+
version_requirements: *id001
|
33
|
+
description: A very simple geolocation library for searching based on postal address or ip address.
|
34
|
+
email: matt@veg.etabl.es
|
35
|
+
executables: []
|
36
|
+
|
37
|
+
extensions: []
|
38
|
+
|
39
|
+
extra_rdoc_files:
|
40
|
+
- LICENSE
|
41
|
+
- README.rdoc
|
42
|
+
files:
|
43
|
+
- .document
|
44
|
+
- .gitignore
|
45
|
+
- LICENSE
|
46
|
+
- README.rdoc
|
47
|
+
- Rakefile
|
48
|
+
- VERSION
|
49
|
+
- Where.gemspec
|
50
|
+
- lib/where.rb
|
51
|
+
- lib/where/address.rb
|
52
|
+
- lib/where/base.rb
|
53
|
+
- lib/where/ip_address.rb
|
54
|
+
- test/helper.rb
|
55
|
+
- test/test_where.rb
|
56
|
+
has_rdoc: true
|
57
|
+
homepage: http://github.com/vegetables/where
|
58
|
+
licenses: []
|
59
|
+
|
60
|
+
post_install_message:
|
61
|
+
rdoc_options:
|
62
|
+
- --charset=UTF-8
|
63
|
+
require_paths:
|
64
|
+
- lib
|
65
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
66
|
+
none: false
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
segments:
|
71
|
+
- 0
|
72
|
+
version: "0"
|
73
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
74
|
+
none: false
|
75
|
+
requirements:
|
76
|
+
- - ">="
|
77
|
+
- !ruby/object:Gem::Version
|
78
|
+
segments:
|
79
|
+
- 0
|
80
|
+
version: "0"
|
81
|
+
requirements: []
|
82
|
+
|
83
|
+
rubyforge_project:
|
84
|
+
rubygems_version: 1.3.7
|
85
|
+
signing_key:
|
86
|
+
specification_version: 3
|
87
|
+
summary: A simple geolocation library.
|
88
|
+
test_files:
|
89
|
+
- test/helper.rb
|
90
|
+
- test/test_where.rb
|