rails-geocoder 0.8.0 → 0.8.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/CHANGELOG.rdoc +12 -0
- data/README.rdoc +7 -6
- data/Rakefile +1 -1
- data/VERSION +1 -1
- data/lib/geocoder.rb +15 -9
- data/rails-geocoder.gemspec +5 -3
- data/test/fixtures/madison_square_garden.xml +16 -0
- data/test/geocoder_test.rb +10 -3
- data/test/test_helper.rb +59 -0
- metadata +5 -3
data/CHANGELOG.rdoc
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
= Changelog
|
2
|
+
|
3
|
+
Per-release changes to Geocoder.
|
4
|
+
|
5
|
+
== 0.8.1 (2009 Oct 8)
|
6
|
+
|
7
|
+
* Extract XML-fetching code from Geocoder.search and place in Geocoder._fetch_xml (for ease of mocking).
|
8
|
+
* Add tests for coordinate-fetching instance methods.
|
9
|
+
|
10
|
+
== 0.8.0 (2009 Oct 1)
|
11
|
+
|
12
|
+
First release.
|
data/README.rdoc
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
= Geocoder
|
2
2
|
|
3
|
-
Geocoder adds database-agnostic
|
3
|
+
Geocoder adds object geocoding and database-agnostic distance calculations to Ruby on Rails. It does not rely on proprietary database functions so finding geocoded objects in a given area is easily done using out-of-the-box MySQL or even SQLite.
|
4
4
|
|
5
|
-
==
|
5
|
+
== Install
|
6
6
|
|
7
|
-
Install either
|
7
|
+
Install either as a plugin:
|
8
8
|
|
9
9
|
script/plugin install git://github.com/alexreisner/geocoder.git
|
10
10
|
|
11
|
-
or
|
11
|
+
or as a gem:
|
12
12
|
|
13
13
|
# add to config/environment.rb:
|
14
14
|
config.gem "rails-geocoder", :lib => "geocoder", :source => "http://gemcutter.org/"
|
@@ -16,6 +16,8 @@ or *as a gem*:
|
|
16
16
|
# at command prompt:
|
17
17
|
sudo rake gems:install
|
18
18
|
|
19
|
+
== Configure
|
20
|
+
|
19
21
|
To add geocoding features to a class:
|
20
22
|
|
21
23
|
geocoded_by :location
|
@@ -36,8 +38,7 @@ If your model has +address+, +city+, +state+, and +country+ attributes your +loc
|
|
36
38
|
[address, city, state, country].compact.join(', ')
|
37
39
|
end
|
38
40
|
|
39
|
-
|
40
|
-
== Features
|
41
|
+
== Use
|
41
42
|
|
42
43
|
Assuming +Venue+ is a geocoded model:
|
43
44
|
|
data/Rakefile
CHANGED
@@ -6,7 +6,7 @@ begin
|
|
6
6
|
Jeweler::Tasks.new do |gem|
|
7
7
|
gem.name = "rails-geocoder"
|
8
8
|
gem.summary = %Q{Add geocoding functionality to Rails models.}
|
9
|
-
gem.description = %Q{
|
9
|
+
gem.description = %Q{Geocoder adds object geocoding and database-agnostic distance calculations to Ruby on Rails. It does not rely on proprietary database functions so finding geocoded objects in a given area is easily done using out-of-the-box MySQL or even SQLite.}
|
10
10
|
gem.email = "alex@alexreisner.com"
|
11
11
|
gem.homepage = "http://github.com/alexreisner/geocoder"
|
12
12
|
gem.authors = ["Alex Reisner"]
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.8.
|
1
|
+
0.8.1
|
data/lib/geocoder.rb
CHANGED
@@ -210,7 +210,21 @@ module Geocoder
|
|
210
210
|
# Returns the XML response as a hash. This method is not intended for
|
211
211
|
# general use (prefer Geocoder.search).
|
212
212
|
#
|
213
|
+
# Google's XML document has incorrect encoding (says UTF-8 but is actually
|
214
|
+
# ISO 8859-1). We have to fix this or REXML won't parse it correctly.
|
215
|
+
# This may be fixed in the future; see the bug report at:
|
216
|
+
# http://code.google.com/p/gmaps-api-issues/issues/detail?id=233
|
217
|
+
#
|
213
218
|
def self.search(query)
|
219
|
+
if doc = _fetch_xml(query)
|
220
|
+
REXML::Document.new(doc.sub('UTF-8', 'ISO-8859-1'))
|
221
|
+
end
|
222
|
+
end
|
223
|
+
|
224
|
+
##
|
225
|
+
# Request an XML geo search result from Google.
|
226
|
+
#
|
227
|
+
def self._fetch_xml(query)
|
214
228
|
params = { :q => query, :output => "xml" }
|
215
229
|
url = "http://maps.google.com/maps/geo?" + params.to_query
|
216
230
|
|
@@ -218,19 +232,11 @@ module Geocoder
|
|
218
232
|
begin
|
219
233
|
resp = nil
|
220
234
|
timeout(3) do
|
221
|
-
|
235
|
+
Net::HTTP.get_response(URI.parse(url)).body
|
222
236
|
end
|
223
237
|
rescue SocketError, TimeoutError
|
224
238
|
return nil
|
225
239
|
end
|
226
|
-
|
227
|
-
# Google's XML document has incorrect encoding (says UTF-8 but is actually
|
228
|
-
# ISO 8859-1). Have to fix this or REXML won't parse correctly.
|
229
|
-
# This may be fixed in the future; see the bug report at:
|
230
|
-
# http://code.google.com/p/gmaps-api-issues/issues/detail?id=233
|
231
|
-
doc = resp.body.sub('UTF-8', 'ISO-8859-1')
|
232
|
-
|
233
|
-
REXML::Document.new(doc)
|
234
240
|
end
|
235
241
|
end
|
236
242
|
|
data/rails-geocoder.gemspec
CHANGED
@@ -5,12 +5,12 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{rails-geocoder}
|
8
|
-
s.version = "0.8.
|
8
|
+
s.version = "0.8.1"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Alex Reisner"]
|
12
|
-
s.date = %q{2009-10-
|
13
|
-
s.description = %q{
|
12
|
+
s.date = %q{2009-10-08}
|
13
|
+
s.description = %q{Geocoder adds object geocoding and database-agnostic distance calculations to Ruby on Rails. It does not rely on proprietary database functions so finding geocoded objects in a given area is easily done using out-of-the-box MySQL or even SQLite.}
|
14
14
|
s.email = %q{alex@alexreisner.com}
|
15
15
|
s.extra_rdoc_files = [
|
16
16
|
"LICENSE",
|
@@ -19,6 +19,7 @@ Gem::Specification.new do |s|
|
|
19
19
|
s.files = [
|
20
20
|
".document",
|
21
21
|
".gitignore",
|
22
|
+
"CHANGELOG.rdoc",
|
22
23
|
"LICENSE",
|
23
24
|
"README.rdoc",
|
24
25
|
"Rakefile",
|
@@ -26,6 +27,7 @@ Gem::Specification.new do |s|
|
|
26
27
|
"init.rb",
|
27
28
|
"lib/geocoder.rb",
|
28
29
|
"rails-geocoder.gemspec",
|
30
|
+
"test/fixtures/madison_square_garden.xml",
|
29
31
|
"test/geocoder_test.rb",
|
30
32
|
"test/test_helper.rb"
|
31
33
|
]
|
@@ -0,0 +1,16 @@
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8" ?>
|
2
|
+
<kml xmlns="http://earth.google.com/kml/2.0"><Response>
|
3
|
+
<name>4 Penn Plaza, New York, NY</name>
|
4
|
+
<Status>
|
5
|
+
<code>200</code>
|
6
|
+
<request>geocode</request>
|
7
|
+
</Status>
|
8
|
+
<Placemark id="p1">
|
9
|
+
<address>4 Penn Plaza, New York, NY 10001, USA</address>
|
10
|
+
<AddressDetails Accuracy="8" xmlns="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0"><Country><CountryNameCode>US</CountryNameCode><CountryName>USA</CountryName><AdministrativeArea><AdministrativeAreaName>NY</AdministrativeAreaName><SubAdministrativeArea><SubAdministrativeAreaName>New York</SubAdministrativeAreaName><Locality><LocalityName>New York</LocalityName><Thoroughfare><ThoroughfareName>4 Penn Plaza</ThoroughfareName></Thoroughfare><PostalCode><PostalCodeNumber>10001</PostalCodeNumber></PostalCode></Locality></SubAdministrativeArea></AdministrativeArea></Country></AddressDetails>
|
11
|
+
<ExtendedData>
|
12
|
+
<LatLonBox north="40.7527158" south="40.7464206" east="-73.9885076" west="-73.9948028" />
|
13
|
+
</ExtendedData>
|
14
|
+
<Point><coordinates>-73.9916733,40.7495760,0</coordinates></Point>
|
15
|
+
</Placemark>
|
16
|
+
</Response></kml>
|
data/test/geocoder_test.rb
CHANGED
@@ -1,8 +1,15 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
3
|
class GeocoderTest < Test::Unit::TestCase
|
4
|
-
|
5
|
-
def
|
6
|
-
|
4
|
+
|
5
|
+
def test_fetch_coordinates
|
6
|
+
v = Venue.new(*venue_params(:msg))
|
7
|
+
assert_equal [40.7495760, -73.9916733], v.fetch_coordinates
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_fetch_coordinates!
|
11
|
+
v = Venue.new(*venue_params(:msg))
|
12
|
+
v.fetch_coordinates!
|
13
|
+
assert_equal [40.7495760, -73.9916733], [v.latitude, v.longitude]
|
7
14
|
end
|
8
15
|
end
|
data/test/test_helper.rb
CHANGED
@@ -1,9 +1,68 @@
|
|
1
1
|
require 'rubygems'
|
2
2
|
require 'test/unit'
|
3
|
+
require 'activesupport'
|
3
4
|
|
4
5
|
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
5
6
|
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
7
|
+
|
8
|
+
##
|
9
|
+
# Simulate enough of ActiveRecord::Base that objects can be used for testing.
|
10
|
+
#
|
11
|
+
module ActiveRecord
|
12
|
+
class Base
|
13
|
+
|
14
|
+
def initialize
|
15
|
+
@attributes = {}
|
16
|
+
end
|
17
|
+
|
18
|
+
def read_attribute(attr_name)
|
19
|
+
@attributes[attr_name.to_s]
|
20
|
+
end
|
21
|
+
|
22
|
+
def write_attribute(attr_name, value)
|
23
|
+
attr_name = attr_name.to_s
|
24
|
+
@attributes[attr_name] = value
|
25
|
+
end
|
26
|
+
|
27
|
+
def self.named_scope(*args); end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
# Require Geocoder after ActiveRecord simulator.
|
6
32
|
require 'geocoder'
|
7
33
|
|
34
|
+
##
|
35
|
+
# Mock HTTP request to Google.
|
36
|
+
#
|
37
|
+
module Geocoder
|
38
|
+
def self._fetch_xml(query)
|
39
|
+
filename = File.join("test", "fixtures", "madison_square_garden.xml")
|
40
|
+
File.read(filename)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
##
|
45
|
+
# Geocoded model.
|
46
|
+
#
|
47
|
+
class Venue < ActiveRecord::Base
|
48
|
+
geocoded_by :address
|
49
|
+
|
50
|
+
def initialize(name, address)
|
51
|
+
super()
|
52
|
+
write_attribute :name, name
|
53
|
+
write_attribute :address, address
|
54
|
+
end
|
55
|
+
|
56
|
+
# could implement these with method_missing
|
57
|
+
# to simulate custom lat/lon methods
|
58
|
+
def latitude; read_attribute(:latitude); end
|
59
|
+
def longitude; read_attribute(:longitude); end
|
60
|
+
end
|
61
|
+
|
8
62
|
class Test::Unit::TestCase
|
63
|
+
def venue_params(abbrev)
|
64
|
+
{
|
65
|
+
:msg => ["Madison Square Garden", "4 Penn Plaza, New York, NY"]
|
66
|
+
}[abbrev]
|
67
|
+
end
|
9
68
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails-geocoder
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.8.
|
4
|
+
version: 0.8.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Alex Reisner
|
@@ -9,11 +9,11 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-10-
|
12
|
+
date: 2009-10-08 00:00:00 -04:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
16
|
-
description:
|
16
|
+
description: Geocoder adds object geocoding and database-agnostic distance calculations to Ruby on Rails. It does not rely on proprietary database functions so finding geocoded objects in a given area is easily done using out-of-the-box MySQL or even SQLite.
|
17
17
|
email: alex@alexreisner.com
|
18
18
|
executables: []
|
19
19
|
|
@@ -25,6 +25,7 @@ extra_rdoc_files:
|
|
25
25
|
files:
|
26
26
|
- .document
|
27
27
|
- .gitignore
|
28
|
+
- CHANGELOG.rdoc
|
28
29
|
- LICENSE
|
29
30
|
- README.rdoc
|
30
31
|
- Rakefile
|
@@ -32,6 +33,7 @@ files:
|
|
32
33
|
- init.rb
|
33
34
|
- lib/geocoder.rb
|
34
35
|
- rails-geocoder.gemspec
|
36
|
+
- test/fixtures/madison_square_garden.xml
|
35
37
|
- test/geocoder_test.rb
|
36
38
|
- test/test_helper.rb
|
37
39
|
has_rdoc: true
|