geocoder 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of geocoder might be problematic. Click here for more details.
- data/CHANGELOG.rdoc +6 -0
- data/README.rdoc +8 -1
- data/Rakefile +1 -7
- data/lib/geocoder/configuration.rb +6 -1
- data/lib/geocoder/lookups/base.rb +12 -4
- data/lib/geocoder/lookups/freegeoip.rb +1 -1
- data/lib/geocoder/models/base.rb +1 -9
- data/lib/geocoder/results/google.rb +1 -0
- data/lib/geocoder/version.rb +1 -1
- data/test/fixtures/google_no_city_data.json +44 -0
- data/test/geocoder_test.rb +17 -0
- data/test/test_helper.rb +10 -3
- metadata +67 -66
data/CHANGELOG.rdoc
CHANGED
@@ -2,6 +2,12 @@
|
|
2
2
|
|
3
3
|
Per-release changes to Geocoder.
|
4
4
|
|
5
|
+
== 1.0.1 (2011 May 17)
|
6
|
+
|
7
|
+
* Add option to not rescue from certain exceptions (thanks github.com/ahmedrb).
|
8
|
+
* Fix STI child/parent geocoding bug (thanks github.com/ogennadi).
|
9
|
+
* Other bugfixes.
|
10
|
+
|
5
11
|
== 1.0.0 (2011 May 9)
|
6
12
|
|
7
13
|
* Add command line interface.
|
data/README.rdoc
CHANGED
@@ -8,7 +8,7 @@ Geocoder is a complete geocoding solution for Ruby. With Rails it adds geocoding
|
|
8
8
|
* Supports multiple Ruby versions: Ruby 1.8.7, 1.9.2, and JRuby.
|
9
9
|
* Supports multiple databases: MySQL, PostgreSQL, SQLite, and MongoDB (1.7.0 and higher).
|
10
10
|
* Supports Rails 3. If you need to use it with Rails 2 please see the <tt>rails2</tt> branch (no longer maintained, limited feature set).
|
11
|
-
* Works very well outside of Rails
|
11
|
+
* Works very well outside of Rails, you just need to install either the +json+ (for MRI) or +json_pure+ (for JRuby) gem.
|
12
12
|
|
13
13
|
|
14
14
|
== Install
|
@@ -446,6 +446,13 @@ Geocoder comes with a test suite (just run <tt>rake test</tt>) that mocks Active
|
|
446
446
|
http://github.com/alexreisner/geocoder_test
|
447
447
|
|
448
448
|
|
449
|
+
== Error Handling
|
450
|
+
|
451
|
+
By default Geocoder will rescue any exceptions raised by calls to the geocoding service and return an empty array (using warn() to inform you of the error). You can override this and implement custom error handling for certain exceptions by using the <tt>:always_raise</tt> option:
|
452
|
+
|
453
|
+
Geocoder::Configuration.always_raise = [SocketError, TimeoutError]
|
454
|
+
|
455
|
+
|
449
456
|
== Known Issue
|
450
457
|
|
451
458
|
You cannot use the +near+ scope with another scope that provides an +includes+ option because the +SELECT+ clause generated by +near+ will overwrite it (or vice versa). Instead, try using +joins+ and pass a <tt>:select</tt> option to the +near+ scope to get the columns you want. For example:
|
data/Rakefile
CHANGED
@@ -12,14 +12,8 @@ task :default => :test
|
|
12
12
|
|
13
13
|
require 'rake/rdoctask'
|
14
14
|
Rake::RDocTask.new do |rdoc|
|
15
|
-
if File.exist?('VERSION')
|
16
|
-
version = File.read('VERSION')
|
17
|
-
else
|
18
|
-
version = ""
|
19
|
-
end
|
20
|
-
|
21
15
|
rdoc.rdoc_dir = 'rdoc'
|
22
|
-
rdoc.title = "
|
16
|
+
rdoc.title = "Geocoder #{Geocoder::VERSION}"
|
23
17
|
rdoc.rdoc_files.include('*.rdoc')
|
24
18
|
rdoc.rdoc_files.include('lib/**/*.rb')
|
25
19
|
end
|
@@ -28,7 +28,12 @@ module Geocoder
|
|
28
28
|
[:cache, nil],
|
29
29
|
|
30
30
|
# prefix (string) to use for all cache keys
|
31
|
-
[:cache_prefix, "geocoder:"]
|
31
|
+
[:cache_prefix, "geocoder:"],
|
32
|
+
|
33
|
+
# exceptions that should not be rescued by default
|
34
|
+
# (if you want to implement custom error handling);
|
35
|
+
# supports SocketError and TimeoutError
|
36
|
+
[:always_raise, []]
|
32
37
|
]
|
33
38
|
end
|
34
39
|
|
@@ -90,16 +90,24 @@ module Geocoder
|
|
90
90
|
eval("Geocoder::Result::#{self.class.to_s.split(":").last}")
|
91
91
|
end
|
92
92
|
|
93
|
+
##
|
94
|
+
# Raise exception instead of warning for specified exceptions.
|
95
|
+
#
|
96
|
+
def raise_error(err)
|
97
|
+
raise err if Geocoder::Configuration.always_raise.include?(err.class)
|
98
|
+
end
|
99
|
+
|
100
|
+
|
93
101
|
##
|
94
102
|
# Returns a parsed search result (Ruby hash).
|
95
103
|
#
|
96
104
|
def fetch_data(query, reverse = false)
|
97
105
|
begin
|
98
106
|
parse_raw_data fetch_raw_data(query, reverse)
|
99
|
-
rescue SocketError
|
100
|
-
warn "Geocoding API connection cannot be established."
|
101
|
-
rescue TimeoutError
|
102
|
-
warn "Geocoding API not responding fast enough " +
|
107
|
+
rescue SocketError => err
|
108
|
+
raise_error(err) or warn "Geocoding API connection cannot be established."
|
109
|
+
rescue TimeoutError => err
|
110
|
+
raise_error(err) or warn "Geocoding API not responding fast enough " +
|
103
111
|
"(see Geocoder::Configuration.timeout to set limit)."
|
104
112
|
end
|
105
113
|
end
|
@@ -10,7 +10,7 @@ module Geocoder::Lookup
|
|
10
10
|
# don't look up a loopback address, just return the stored result
|
11
11
|
return [reserved_result(query)] if loopback_address?(query)
|
12
12
|
begin
|
13
|
-
return
|
13
|
+
return (doc = fetch_data(query, reverse)) ? [doc] : []
|
14
14
|
rescue StandardError # Freegeoip.net returns HTML on bad request
|
15
15
|
return []
|
16
16
|
end
|
data/lib/geocoder/models/base.rb
CHANGED
@@ -28,21 +28,13 @@ module Geocoder
|
|
28
28
|
private # ----------------------------------------------------------------
|
29
29
|
|
30
30
|
def geocoder_init(options)
|
31
|
-
unless
|
31
|
+
unless @geocoder_options
|
32
32
|
@geocoder_options = {}
|
33
33
|
require "geocoder/stores/#{geocoder_file_name}"
|
34
34
|
include eval("Geocoder::Store::" + geocoder_module_name)
|
35
35
|
end
|
36
36
|
@geocoder_options.merge! options
|
37
37
|
end
|
38
|
-
|
39
|
-
def geocoder_initialized?
|
40
|
-
begin
|
41
|
-
included_modules.include? eval("Geocoder::Store::" + geocoder_module_name)
|
42
|
-
rescue NameError
|
43
|
-
false
|
44
|
-
end
|
45
|
-
end
|
46
38
|
end
|
47
39
|
end
|
48
40
|
end
|
data/lib/geocoder/version.rb
CHANGED
@@ -0,0 +1,44 @@
|
|
1
|
+
{
|
2
|
+
"status": "OK",
|
3
|
+
"results": [ {
|
4
|
+
"types": [ "postal_code" ],
|
5
|
+
"formatted_address": "55100, Turkey",
|
6
|
+
"address_components": [{
|
7
|
+
"long_name": "55100",
|
8
|
+
"short_name": "55100",
|
9
|
+
"types": ["postal_code"]
|
10
|
+
},
|
11
|
+
{
|
12
|
+
"long_name": "Turkey",
|
13
|
+
"short_name": "TR",
|
14
|
+
"types": ["country", "political"]
|
15
|
+
}],
|
16
|
+
"geometry": {
|
17
|
+
"location": {
|
18
|
+
"lat": 41.3112221,
|
19
|
+
"lng": 36.3322118
|
20
|
+
},
|
21
|
+
"location_type": "APPROXIMATE",
|
22
|
+
"viewport": {
|
23
|
+
"southwest": {
|
24
|
+
"lat": 41.2933411,
|
25
|
+
"lng": 36.3066331
|
26
|
+
},
|
27
|
+
"northeast": {
|
28
|
+
"lat": 41.3291031,
|
29
|
+
"lng": 36.3577906
|
30
|
+
}
|
31
|
+
},
|
32
|
+
"bounds": {
|
33
|
+
"southwest": {
|
34
|
+
"lat": 41.2933411,
|
35
|
+
"lng": 36.3066331
|
36
|
+
},
|
37
|
+
"northeast": {
|
38
|
+
"lat": 41.3291031,
|
39
|
+
"lng": 36.3577906
|
40
|
+
}
|
41
|
+
}
|
42
|
+
}
|
43
|
+
} ]
|
44
|
+
}
|
data/test/geocoder_test.rb
CHANGED
@@ -357,6 +357,19 @@ class GeocoderTest < Test::Unit::TestCase
|
|
357
357
|
end
|
358
358
|
|
359
359
|
|
360
|
+
def test_always_raise_timeout_error
|
361
|
+
Geocoder::Configuration.always_raise = [TimeoutError]
|
362
|
+
assert_raise(TimeoutError) { Geocoder.search("timeout") }
|
363
|
+
Geocoder::Configuration.always_raise = []
|
364
|
+
end
|
365
|
+
|
366
|
+
|
367
|
+
def test_always_raise_socket_error
|
368
|
+
Geocoder::Configuration.always_raise = [SocketError]
|
369
|
+
assert_raise(SocketError) { Geocoder.search("socket_error") }
|
370
|
+
Geocoder::Configuration.always_raise = []
|
371
|
+
end
|
372
|
+
|
360
373
|
# --- Google ---
|
361
374
|
|
362
375
|
def test_google_result_components
|
@@ -370,6 +383,10 @@ class GeocoderTest < Test::Unit::TestCase
|
|
370
383
|
assert_equal "Haram", result.city
|
371
384
|
end
|
372
385
|
|
386
|
+
def test_google_city_results_returns_nil_if_no_matching_component_types
|
387
|
+
result = Geocoder.search("no city data").first
|
388
|
+
assert_equal nil, result.city
|
389
|
+
end
|
373
390
|
|
374
391
|
# --- Yahoo ---
|
375
392
|
|
data/test/test_helper.rb
CHANGED
@@ -62,10 +62,12 @@ module Geocoder
|
|
62
62
|
private #-----------------------------------------------------------------
|
63
63
|
def fetch_raw_data(query, reverse = false)
|
64
64
|
raise TimeoutError if query == "timeout"
|
65
|
+
raise SocketError if query == "socket_error"
|
65
66
|
file = case query
|
66
|
-
when "no results";
|
67
|
-
when "no locality";
|
68
|
-
|
67
|
+
when "no results"; :no_results
|
68
|
+
when "no locality"; :no_locality
|
69
|
+
when "no city data"; :no_city_data
|
70
|
+
else :madison_square_garden
|
69
71
|
end
|
70
72
|
read_fixture "google_#{file}.json"
|
71
73
|
end
|
@@ -75,6 +77,7 @@ module Geocoder
|
|
75
77
|
private #-----------------------------------------------------------------
|
76
78
|
def fetch_raw_data(query, reverse = false)
|
77
79
|
raise TimeoutError if query == "timeout"
|
80
|
+
raise SocketError if query == "socket_error"
|
78
81
|
file = case query
|
79
82
|
when "no results"; :no_results
|
80
83
|
else :madison_square_garden
|
@@ -87,6 +90,7 @@ module Geocoder
|
|
87
90
|
private #-----------------------------------------------------------------
|
88
91
|
def fetch_raw_data(query, reverse = false)
|
89
92
|
raise TimeoutError if query == "timeout"
|
93
|
+
raise SocketError if query == "socket_error"
|
90
94
|
file = case query
|
91
95
|
when "no results"; :no_results
|
92
96
|
when "invalid key"; :invalid_key
|
@@ -100,6 +104,7 @@ module Geocoder
|
|
100
104
|
private #-----------------------------------------------------------------
|
101
105
|
def fetch_raw_data(query, reverse = false)
|
102
106
|
raise TimeoutError if query == "timeout"
|
107
|
+
raise SocketError if query == "socket_error"
|
103
108
|
if reverse
|
104
109
|
read_fixture "geocoder_ca_reverse.json"
|
105
110
|
else
|
@@ -116,6 +121,7 @@ module Geocoder
|
|
116
121
|
private #-----------------------------------------------------------------
|
117
122
|
def fetch_raw_data(query, reverse = false)
|
118
123
|
raise TimeoutError if query == "timeout"
|
124
|
+
raise SocketError if query == "socket_error"
|
119
125
|
read_fixture "freegeoip_74_200_247_59.json"
|
120
126
|
end
|
121
127
|
end
|
@@ -124,6 +130,7 @@ module Geocoder
|
|
124
130
|
private #-----------------------------------------------------------------
|
125
131
|
def fetch_raw_data(query, reverse = false)
|
126
132
|
raise TimeoutError if query == "timeout"
|
133
|
+
raise SocketError if query == "socket_error"
|
127
134
|
if reverse
|
128
135
|
read_fixture "bing_reverse.json"
|
129
136
|
else
|
metadata
CHANGED
@@ -2,82 +2,83 @@
|
|
2
2
|
name: geocoder
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 1.0.
|
5
|
+
version: 1.0.1
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
|
-
|
8
|
+
- Alex Reisner
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-05-
|
13
|
+
date: 2011-05-17 00:00:00 -04:00
|
14
14
|
default_executable:
|
15
15
|
dependencies: []
|
16
16
|
|
17
17
|
description: Provides object geocoding (by street or IP address), reverse geocoding (coordinates to street address), distance queries for ActiveRecord and Mongoid, result caching, and more. Designed for Rails but works with Sinatra and other Rack frameworks too.
|
18
18
|
email:
|
19
|
-
|
19
|
+
- alex@alexreisner.com
|
20
20
|
executables:
|
21
|
-
|
21
|
+
- geocode
|
22
22
|
extensions: []
|
23
23
|
|
24
24
|
extra_rdoc_files: []
|
25
25
|
|
26
26
|
files:
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
27
|
+
- .gitignore
|
28
|
+
- CHANGELOG.rdoc
|
29
|
+
- LICENSE
|
30
|
+
- README.rdoc
|
31
|
+
- Rakefile
|
32
|
+
- bin/geocode
|
33
|
+
- lib/geocoder.rb
|
34
|
+
- lib/geocoder/cache.rb
|
35
|
+
- lib/geocoder/calculations.rb
|
36
|
+
- lib/geocoder/cli.rb
|
37
|
+
- lib/geocoder/configuration.rb
|
38
|
+
- lib/geocoder/lookups/base.rb
|
39
|
+
- lib/geocoder/lookups/bing.rb
|
40
|
+
- lib/geocoder/lookups/freegeoip.rb
|
41
|
+
- lib/geocoder/lookups/geocoder_ca.rb
|
42
|
+
- lib/geocoder/lookups/google.rb
|
43
|
+
- lib/geocoder/lookups/yahoo.rb
|
44
|
+
- lib/geocoder/lookups/yandex.rb
|
45
|
+
- lib/geocoder/models/active_record.rb
|
46
|
+
- lib/geocoder/models/base.rb
|
47
|
+
- lib/geocoder/models/mongoid.rb
|
48
|
+
- lib/geocoder/railtie.rb
|
49
|
+
- lib/geocoder/request.rb
|
50
|
+
- lib/geocoder/results/base.rb
|
51
|
+
- lib/geocoder/results/bing.rb
|
52
|
+
- lib/geocoder/results/freegeoip.rb
|
53
|
+
- lib/geocoder/results/geocoder_ca.rb
|
54
|
+
- lib/geocoder/results/google.rb
|
55
|
+
- lib/geocoder/results/yahoo.rb
|
56
|
+
- lib/geocoder/results/yandex.rb
|
57
|
+
- lib/geocoder/stores/active_record.rb
|
58
|
+
- lib/geocoder/stores/base.rb
|
59
|
+
- lib/geocoder/stores/mongoid.rb
|
60
|
+
- lib/geocoder/version.rb
|
61
|
+
- lib/tasks/geocoder.rake
|
62
|
+
- test/fixtures/bing_madison_square_garden.json
|
63
|
+
- test/fixtures/bing_no_results.json
|
64
|
+
- test/fixtures/bing_reverse.json
|
65
|
+
- test/fixtures/freegeoip_74_200_247_59.json
|
66
|
+
- test/fixtures/geocoder_ca_madison_square_garden.json
|
67
|
+
- test/fixtures/geocoder_ca_no_results.json
|
68
|
+
- test/fixtures/geocoder_ca_reverse.json
|
69
|
+
- test/fixtures/google_garbage.json
|
70
|
+
- test/fixtures/google_madison_square_garden.json
|
71
|
+
- test/fixtures/google_no_city_data.json
|
72
|
+
- test/fixtures/google_no_locality.json
|
73
|
+
- test/fixtures/google_no_results.json
|
74
|
+
- test/fixtures/yahoo_garbage.json
|
75
|
+
- test/fixtures/yahoo_madison_square_garden.json
|
76
|
+
- test/fixtures/yahoo_no_results.json
|
77
|
+
- test/fixtures/yandex_invalid_key.json
|
78
|
+
- test/fixtures/yandex_kremlin.json
|
79
|
+
- test/fixtures/yandex_no_results.json
|
80
|
+
- test/geocoder_test.rb
|
81
|
+
- test/test_helper.rb
|
81
82
|
has_rdoc: true
|
82
83
|
homepage: http://www.rubygeocoder.com
|
83
84
|
licenses: []
|
@@ -86,19 +87,19 @@ post_install_message:
|
|
86
87
|
rdoc_options: []
|
87
88
|
|
88
89
|
require_paths:
|
89
|
-
|
90
|
+
- lib
|
90
91
|
required_ruby_version: !ruby/object:Gem::Requirement
|
91
92
|
none: false
|
92
93
|
requirements:
|
93
|
-
|
94
|
-
|
95
|
-
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: "0"
|
96
97
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
97
98
|
none: false
|
98
99
|
requirements:
|
99
|
-
|
100
|
-
|
101
|
-
|
100
|
+
- - ">="
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: "0"
|
102
103
|
requirements: []
|
103
104
|
|
104
105
|
rubyforge_project:
|