geocoder 1.1.5 → 1.1.6

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.

Files changed (84) hide show
  1. data/.travis.yml +4 -0
  2. data/{CHANGELOG.rdoc → CHANGELOG.md} +90 -37
  3. data/README.md +96 -45
  4. data/examples/autoexpire_cache.rb +1 -3
  5. data/lib/generators/geocoder/config/templates/initializer.rb +18 -22
  6. data/lib/geocoder.rb +2 -4
  7. data/lib/geocoder/cache.rb +2 -1
  8. data/lib/geocoder/calculations.rb +12 -12
  9. data/lib/geocoder/cli.rb +10 -11
  10. data/lib/geocoder/configuration.rb +67 -43
  11. data/lib/geocoder/configuration_hash.rb +11 -0
  12. data/lib/geocoder/exceptions.rb +3 -0
  13. data/lib/geocoder/lookup.rb +1 -1
  14. data/lib/geocoder/lookups/base.rb +59 -16
  15. data/lib/geocoder/lookups/bing.rb +21 -11
  16. data/lib/geocoder/lookups/freegeoip.rb +8 -4
  17. data/lib/geocoder/lookups/geocoder_ca.rb +11 -7
  18. data/lib/geocoder/lookups/google.rb +18 -9
  19. data/lib/geocoder/lookups/google_premier.rb +16 -8
  20. data/lib/geocoder/lookups/mapquest.rb +12 -5
  21. data/lib/geocoder/lookups/maxmind.rb +72 -0
  22. data/lib/geocoder/lookups/nominatim.rb +13 -8
  23. data/lib/geocoder/lookups/test.rb +4 -0
  24. data/lib/geocoder/lookups/yahoo.rb +38 -11
  25. data/lib/geocoder/lookups/yandex.rb +17 -9
  26. data/lib/geocoder/query.rb +17 -8
  27. data/lib/geocoder/request.rb +7 -1
  28. data/lib/geocoder/results/google.rb +6 -0
  29. data/lib/geocoder/results/maxmind.rb +136 -0
  30. data/lib/geocoder/results/nominatim.rb +0 -10
  31. data/lib/geocoder/sql.rb +6 -4
  32. data/lib/geocoder/stores/active_record.rb +5 -5
  33. data/lib/geocoder/stores/base.rb +3 -2
  34. data/lib/geocoder/version.rb +1 -1
  35. data/lib/hash_recursive_merge.rb +74 -0
  36. data/lib/oauth_util.rb +8 -2
  37. data/test/cache_test.rb +2 -2
  38. data/test/calculations_test.rb +4 -4
  39. data/test/configuration_test.rb +26 -51
  40. data/test/error_handling_test.rb +4 -4
  41. data/test/fixtures/bing_invalid_key +1 -0
  42. data/test/fixtures/{bing_madison_square_garden.json → bing_madison_square_garden} +0 -0
  43. data/test/fixtures/{bing_no_results.json → bing_no_results} +0 -0
  44. data/test/fixtures/{bing_reverse.json → bing_reverse} +0 -0
  45. data/test/fixtures/{freegeoip_74_200_247_59.json → freegeoip_74_200_247_59} +0 -0
  46. data/test/fixtures/{freegeoip_no_results.json → freegeoip_no_results} +0 -0
  47. data/test/fixtures/{geocoder_ca_madison_square_garden.json → geocoder_ca_madison_square_garden} +0 -0
  48. data/test/fixtures/{geocoder_ca_no_results.json → geocoder_ca_no_results} +0 -0
  49. data/test/fixtures/{geocoder_ca_reverse.json → geocoder_ca_reverse} +0 -0
  50. data/test/fixtures/{google_garbage.json → google_garbage} +0 -0
  51. data/test/fixtures/{google_madison_square_garden.json → google_madison_square_garden} +0 -0
  52. data/test/fixtures/{google_no_city_data.json → google_no_city_data} +0 -0
  53. data/test/fixtures/{google_no_locality.json → google_no_locality} +0 -0
  54. data/test/fixtures/{google_no_results.json → google_no_results} +0 -0
  55. data/test/fixtures/{mapquest_madison_square_garden.json → mapquest_madison_square_garden} +0 -0
  56. data/test/fixtures/{mapquest_no_results.json → mapquest_no_results} +0 -0
  57. data/test/fixtures/maxmind_24_24_24_21 +1 -0
  58. data/test/fixtures/maxmind_24_24_24_22 +1 -0
  59. data/test/fixtures/maxmind_24_24_24_23 +1 -0
  60. data/test/fixtures/maxmind_24_24_24_24 +1 -0
  61. data/test/fixtures/maxmind_74_200_247_59 +1 -0
  62. data/test/fixtures/maxmind_invalid_key +1 -0
  63. data/test/fixtures/maxmind_no_results +1 -0
  64. data/test/fixtures/{nominatim_madison_square_garden.json → nominatim_madison_square_garden} +0 -0
  65. data/test/fixtures/{nominatim_no_results.json → nominatim_no_results} +0 -0
  66. data/test/fixtures/{yahoo_error.json → yahoo_error} +0 -0
  67. data/test/fixtures/yahoo_invalid_key +2 -0
  68. data/test/fixtures/{yahoo_madison_square_garden.json → yahoo_madison_square_garden} +0 -0
  69. data/test/fixtures/{yahoo_no_results.json → yahoo_no_results} +0 -0
  70. data/test/fixtures/yahoo_over_limit +2 -0
  71. data/test/fixtures/{yandex_invalid_key.json → yandex_invalid_key} +0 -0
  72. data/test/fixtures/{yandex_kremlin.json → yandex_kremlin} +0 -0
  73. data/test/fixtures/{yandex_no_results.json → yandex_no_results} +0 -0
  74. data/test/https_test.rb +3 -3
  75. data/test/integration/smoke_test.rb +2 -2
  76. data/test/lookup_test.rb +82 -5
  77. data/test/oauth_util_test.rb +30 -0
  78. data/test/proxy_test.rb +2 -2
  79. data/test/query_test.rb +3 -0
  80. data/test/result_test.rb +2 -2
  81. data/test/services_test.rb +121 -44
  82. data/test/test_helper.rb +44 -109
  83. data/test/test_mode_test.rb +3 -3
  84. metadata +42 -33
@@ -2,6 +2,7 @@ rvm:
2
2
  - 1.8.7
3
3
  - 1.9.2
4
4
  - 1.9.3
5
+ - jruby-19mode
5
6
  gemfile:
6
7
  - Gemfile
7
8
  - gemfiles/Gemfile.mongoid-2.4.x
@@ -17,3 +18,6 @@ matrix:
17
18
  - rvm: 1.9.3
18
19
  gemfile: gemfiles/Gemfile.mongoid-2.4.x
19
20
  env: SSL_CERT_DIR=/etc/ssl/certs
21
+ - rvm: jruby-19mode
22
+ gemfile: gemfiles/Gemfile.mongoid-2.4.x
23
+ env: SSL_CERT_DIR=/etc/ssl/certs
@@ -1,8 +1,26 @@
1
- = Changelog
1
+ Changelog
2
+ =========
2
3
 
3
4
  Per-release changes to Geocoder.
4
5
 
5
- == 1.1.5 (2012 Nov 9)
6
+ 1.1.6 (2012 Dec 24)
7
+ -------------------
8
+
9
+ * Major changes to configuration syntax which allow for API-specific config options. Old config syntax is now DEPRECATED.
10
+ * Add support for MaxMind API (thanks github.com/gonzoyumo).
11
+ * Add optional Geocoder::InvalidApiKey exception for bad API credentials (Yahoo, Yandex, Bing, and Maxmind). Warn when bad key and exception not set in Geocoder.configure(:always_raise => [...]).
12
+ * Add support for X-Real-IP and X-Forwarded-For headers (thanks github.com/konsti).
13
+ * Add support for custom Nominatim host config: Geocoder.configure(:nominatim => {:host => "..."}).
14
+ * Raise exception when required API key is missing or incorrect format.
15
+ * Add support for Google's :region and :components parameters (thanks to github.com/tomlion).
16
+ * Fix: string escaping bug in OAuth lib (thanks github.com/m0thman).
17
+ * Fix: configured units were not always respected in SQL queries.
18
+ * Fix: in #nearbys, don't try to exclude self if not yet persisted.
19
+ * Fix: bug with cache stores that provided #delete but not #del.
20
+ * Change #nearbys so that it returns nil instead of [] when object is not geocoded.
21
+
22
+ 1.1.5 (2012 Nov 9)
23
+ ------------------
6
24
 
7
25
  * Replace support for old Yahoo Placefinder with Yahoo BOSS (thanks github.com/pwoltman).
8
26
  * Add support for actual Mapquest API (was previously just a proxy for Nominatim), including the paid service (thanks github.com/jedschneider).
@@ -11,7 +29,8 @@ Per-release changes to Geocoder.
11
29
  * Speed up 'near' queries by adding bounding box condition (thanks github.com/mlandauer).
12
30
  * Fix: don't redefine Object#hash in Yahoo result object (thanks github.com/m0thman).
13
31
 
14
- == 1.1.4 (2012 Oct 2)
32
+ 1.1.4 (2012 Oct 2)
33
+ ------------------
15
34
 
16
35
  * Deprecate Geocoder::Result::Nominatim#class and #type methods. Use #place_class and #place_type instead.
17
36
  * Add support for setting arbitrary parameters in geocoding request URL.
@@ -23,7 +42,8 @@ Per-release changes to Geocoder.
23
42
  * Fix: :exclude option to .near scope when primary key != :id (thanks github.com/smisml).
24
43
  * Much code refactoring (added Geocoder::Query class and Geocoder::Sql module).
25
44
 
26
- == 1.1.3 (2012 Aug 26)
45
+ 1.1.3 (2012 Aug 26)
46
+ -------------------
27
47
 
28
48
  * Add support for Mapquest geocoding service (thanks github.com/razorinc).
29
49
  * Add :test lookup for easy testing of apps using Geocoder (thanks github.com/mguterl).
@@ -36,7 +56,8 @@ Per-release changes to Geocoder.
36
56
  * Fix: bug in distance_from_sql method (error occurred when coordinates not found).
37
57
  * Fix: incompatibility with Mongoid 3.0.x (thanks github.com/petergoldstein).
38
58
 
39
- == 1.1.2 (2012 May 24)
59
+ 1.1.2 (2012 May 24)
60
+ -------------------
40
61
 
41
62
  * Add ability to specify default units and distance calculation method (thanks github.com/abravalheri).
42
63
  * Add new (optional) configuration syntax (thanks github.com/abravalheri).
@@ -47,7 +68,8 @@ Per-release changes to Geocoder.
47
68
  * Fix: properly recognize IP addresses with ::ffff: prefix (thanks github.com/brian-ewell).
48
69
  * Fix: avoid exception during calculations when coordinates not known (thanks github.com/flori).
49
70
 
50
- == 1.1.1 (2012 Feb 16)
71
+ 1.1.1 (2012 Feb 16)
72
+ -------------------
51
73
 
52
74
  * Add distance_from_sql class method to geocoded class (thanks github.com/dwilkie).
53
75
  * Add OverQueryLimitError and raise when relevant for Google lookup.
@@ -55,7 +77,8 @@ Per-release changes to Geocoder.
55
77
  * Fix: within_bounding_box now uses correct lat/lon DB columns (thanks github.com/kongo).
56
78
  * Fix: error accessing city in some cases with Yandex result (thanks github.com/kor6n and sld).
57
79
 
58
- == 1.1.0 (2011 Dec 3)
80
+ 1.1.0 (2011 Dec 3)
81
+ ------------------
59
82
 
60
83
  * A block passed to geocoded_by is now always executed, even if the geocoding service returns no results. This means you need to make sure you have results before trying to assign data to your object.
61
84
  * Fix issues with joins and row counts (issues #49, 86, and 108) by not using GROUP BY clause with ActiveRecord scopes.
@@ -67,17 +90,20 @@ Per-release changes to Geocoder.
67
90
  * Add support for API key to Geocoder.ca geocoding service (thanks github.com/ryanLonac).
68
91
  * Add support for state to Yandex results (thanks github.com/tipugin).
69
92
 
70
- == 1.0.5 (2011 Oct 26)
93
+ 1.0.5 (2011 Oct 26)
94
+ -------------------
71
95
 
72
96
  * Fix error with `rake assets:precompile` (thanks github.com/Sush).
73
97
  * Fix HTTPS support (thanks github.com/rsanheim).
74
98
  * Improve cache interface.
75
99
 
76
- == 1.0.4 (2011 Sep 18)
100
+ 1.0.4 (2011 Sep 18)
101
+ -------------------
77
102
 
78
103
  * Remove klass method from rake task, which could conflict with app methods (thanks github.com/mguterl).
79
104
 
80
- == 1.0.3 (2011 Sep 17)
105
+ 1.0.3 (2011 Sep 17)
106
+ -------------------
81
107
 
82
108
  * Add support for Google Premier geocoding service (thanks github.com/steveh).
83
109
  * Update Google API URL (thanks github.com/soorajb).
@@ -85,19 +111,22 @@ Per-release changes to Geocoder.
85
111
  * Fix: rake assets:precompile (Rails 3.1) not working in some situations.
86
112
  * Fix: stop double-adjusting units when using kilometers (thanks github.com/hairyheron).
87
113
 
88
- == 1.0.2 (2011 June 25)
114
+ 1.0.2 (2011 June 25)
115
+ --------------------
89
116
 
90
117
  * Add support for MongoMapper (thanks github.com/spagalloco).
91
118
  * Fix: user-specified coordinates field wasn't working with Mongoid (thanks github.com/thisduck).
92
119
  * Fix: invalid location given to near scope was returning all results (Active Record) or error (Mongoid) (thanks github.com/ogennadi).
93
120
 
94
- == 1.0.1 (2011 May 17)
121
+ 1.0.1 (2011 May 17)
122
+ -------------------
95
123
 
96
124
  * Add option to not rescue from certain exceptions (thanks github.com/ahmedrb).
97
125
  * Fix STI child/parent geocoding bug (thanks github.com/ogennadi).
98
126
  * Other bugfixes.
99
127
 
100
- == 1.0.0 (2011 May 9)
128
+ 1.0.0 (2011 May 9)
129
+ ------------------
101
130
 
102
131
  * Add command line interface.
103
132
  * Add support for local proxy (thanks github.com/Olivier).
@@ -106,11 +135,13 @@ Per-release changes to Geocoder.
106
135
  * Fix single table inheritance bug (reported by github.com/enrico).
107
136
  * Fix bug when Google result supplies no city (thanks github.com/jkeen).
108
137
 
109
- == 0.9.13 (2011 Apr 11)
138
+ 0.9.13 (2011 Apr 11)
139
+ --------------------
110
140
 
111
141
  * Fix "can't find special index: 2d" error when using Mongoid with Ruby 1.8.
112
142
 
113
- == 0.9.12 (2011 Apr 6)
143
+ 0.9.12 (2011 Apr 6)
144
+ -------------------
114
145
 
115
146
  * Add support for Mongoid.
116
147
  * Add bearing_to/from methods to geocoded objects.
@@ -121,7 +152,8 @@ Per-release changes to Geocoder.
121
152
  * DEPRECATION: Geocoder.near should not take <tt>:limit</tt> or <tt>:offset</tt> options.
122
153
  * DEPRECATION: Change argument format of all methods that take lat/lon as separate arguments. Now you must pass the coordinates as an array [lat,lon], but you may alternatively pass a address string (will look up coordinates) or a geocoded object (or any object that implements a to_coordinates method which returns a [lat,lon] array).
123
154
 
124
- == 0.9.11 (2011 Mar 25)
155
+ 0.9.11 (2011 Mar 25)
156
+ --------------------
125
157
 
126
158
  * Add support for result caching.
127
159
  * Add support for Geocoder.ca geocoding service.
@@ -131,12 +163,14 @@ Per-release changes to Geocoder.
131
163
  * DEPRECATION: <tt>Geocoder.search</tt> now returns an array instead of a single result.
132
164
  * DEPRECATION: <tt>obj.nearbys</tt> second argument is now an options hash (instead of units). Please change <tt>obj.nearbys(20, :km)</tt> to: <tt>obj.nearbys(20, :units => :km)</tt>.
133
165
 
134
- == 0.9.10 (2011 Mar 9)
166
+ 0.9.10 (2011 Mar 9)
167
+ -------------------
135
168
 
136
169
  * Fix broken scopes (github.com/mikepinde).
137
170
  * Fix broken Ruby 1.9 and JRuby compatibility (don't require json gem).
138
171
 
139
- == 0.9.9 (2011 Mar 9)
172
+ 0.9.9 (2011 Mar 9)
173
+ ------------------
140
174
 
141
175
  * Add support for IP address geocoding via FreeGeoIp.net.
142
176
  * Add support for Yahoo PlaceFinder geocoding API.
@@ -151,7 +185,8 @@ Per-release changes to Geocoder.
151
185
  * DEPRECATION: <tt>fetch_address!</tt> has been superceded by +reverse_geocode+ (then save your object manually).
152
186
  * Fix: don't die when trying to get coordinates with a nil address (github.com/zmack).
153
187
 
154
- == 0.9.8 (2011 Feb 8)
188
+ 0.9.8 (2011 Feb 8)
189
+ ------------------
155
190
 
156
191
  * Include <tt>geocode:all</tt> Rake task in gem (was missing!).
157
192
  * Add <tt>Geocoder.search</tt> for access to Google's full response.
@@ -159,87 +194,105 @@ Per-release changes to Geocoder.
159
194
  * Emit warnings on Google connection problems and errors.
160
195
  * Refactor: insert Geocoder into ActiveRecord via Railtie.
161
196
 
162
- == 0.9.7 (2011 Feb 1)
197
+ 0.9.7 (2011 Feb 1)
198
+ ------------------
163
199
 
164
200
  * Add reverse geocoding (+reverse_geocoded_by+).
165
201
  * Prevent exception (uninitialized constant Geocoder::Net) when net/http not already required (github.com/sleepycat).
166
202
  * Refactor: split monolithic Geocoder module into several smaller ones.
167
203
 
168
- == 0.9.6 (2011 Jan 19)
204
+ 0.9.6 (2011 Jan 19)
205
+ -------------------
169
206
 
170
207
  * Fix incompatibility with will_paginate gem.
171
208
  * Include table names in GROUP BY clause of nearby scope to avoid ambiguity in joins (github.com/matchu).
172
209
 
173
- == 0.9.5 (2010 Oct 15)
210
+ 0.9.5 (2010 Oct 15)
211
+ -------------------
174
212
 
175
213
  * Fix broken PostgreSQL compatibility (now 100% compatible).
176
214
  * Switch from Google's XML to JSON geocoding API.
177
215
  * Separate Rails 2 and Rails 3-compatible branches.
178
216
  * Don't allow :conditions hash in 'options' argument to 'nearbys' method (was deprecated in 0.9.3).
179
217
 
180
- == 0.9.4 (2010 Aug 2)
218
+ 0.9.4 (2010 Aug 2)
219
+ ------------------
181
220
 
182
221
  * Google Maps API key no longer required (uses geocoder v3).
183
222
 
184
- == 0.9.3 (2010 Aug 2)
223
+ 0.9.3 (2010 Aug 2)
224
+ ------------------
185
225
 
186
226
  * Fix incompatibility with Rails 3 RC 1.
187
227
  * Deprecate 'options' argument to 'nearbys' method.
188
228
  * Allow inclusion of 'nearbys' in Arel method chains.
189
229
 
190
- == 0.9.2 (2010 Jun 3)
230
+ 0.9.2 (2010 Jun 3)
231
+ ------------------
191
232
 
192
233
  * Fix LIMIT clause bug in PostgreSQL (reported by github.com/kenzie).
193
234
 
194
- == 0.9.1 (2010 May 4)
235
+ 0.9.1 (2010 May 4)
236
+ ------------------
195
237
 
196
238
  * Use scope instead of named_scope in Rails 3.
197
239
 
198
- == 0.9.0 (2010 Apr 2)
240
+ 0.9.0 (2010 Apr 2)
241
+ ------------------
199
242
 
200
243
  * Fix bug in PostgreSQL support (caused "PGError: ERROR: column "distance" does not exist"), reported by github.com/developish.
201
244
 
202
- == 0.8.9 (2010 Feb 11)
245
+ 0.8.9 (2010 Feb 11)
246
+ -------------------
203
247
 
204
248
  * Add Rails 3 compatibility.
205
249
  * Avoid querying Google when query would be an empty string.
206
250
 
207
- == 0.8.8 (2009 Dec 7)
251
+ 0.8.8 (2009 Dec 7)
252
+ ------------------
208
253
 
209
254
  * Automatically select a less accurate but compatible distance algorithm when SQLite database detected (fixes SQLite incompatibility).
210
255
 
211
- == 0.8.7 (2009 Nov 4)
256
+ 0.8.7 (2009 Nov 4)
257
+ ------------------
212
258
 
213
259
  * Added Geocoder.geographic_center method.
214
260
  * Replaced _get_coordinates class method with read_coordinates instance method.
215
261
 
216
- == 0.8.6 (2009 Oct 27)
262
+ 0.8.6 (2009 Oct 27)
263
+ -------------------
217
264
 
218
265
  * The fetch_coordinates method now assigns coordinates to attributes (behaves like fetch_coordinates! used to) and fetch_coordinates! both assigns and saves the attributes.
219
266
  * Added geocode:all rake task.
220
267
 
221
- == 0.8.5 (2009 Oct 26)
268
+ 0.8.5 (2009 Oct 26)
269
+ -------------------
222
270
 
223
271
  * Avoid calling deprecated method from within Geocoder itself.
224
272
 
225
- == 0.8.4 (2009 Oct 23)
273
+ 0.8.4 (2009 Oct 23)
274
+ -------------------
226
275
 
227
276
  * Deprecate <tt>find_near</tt> class method in favor of +near+ named scope.
228
277
 
229
- == 0.8.3 (2009 Oct 23)
278
+ 0.8.3 (2009 Oct 23)
279
+ -------------------
230
280
 
231
281
  * Update Google URL query string parameter to reflect recent changes in Google's API.
232
282
 
233
- == 0.8.2 (2009 Oct 12)
283
+ 0.8.2 (2009 Oct 12)
284
+ -------------------
234
285
 
235
286
  * Allow a model's geocoder search string method to be something other than an ActiveRecord attribute.
236
287
  * Clean up documentation.
237
288
 
238
- == 0.8.1 (2009 Oct 8)
289
+ 0.8.1 (2009 Oct 8)
290
+ ------------------
239
291
 
240
292
  * Extract XML-fetching code from <tt>Geocoder.search</tt> and place in Geocoder._fetch_xml (for ease of mocking).
241
293
  * Add tests for coordinate-fetching instance methods.
242
294
 
243
- == 0.8.0 (2009 Oct 1)
295
+ 0.8.0 (2009 Oct 1)
296
+ ------------------
244
297
 
245
298
  First release.
data/README.md CHANGED
@@ -7,18 +7,20 @@ Geocoder is a complete geocoding solution for Ruby. With Rails it adds geocoding
7
7
  Compatibility
8
8
  -------------
9
9
 
10
- * Supports multiple Ruby versions: Ruby 1.8.7, 1.9.2, and JRuby.
10
+ * Supports multiple Ruby versions: Ruby 1.8.7, 1.9.2, 1.9.3, and JRuby.
11
11
  * Supports multiple databases: MySQL, PostgreSQL, SQLite, and MongoDB (1.7.0 and higher).
12
- * Supports Rails 3. If you need to use it with Rails 2 please see the `rails2` branch (no longer maintained, limited feature set).
12
+ * Supports Rails 3.x. If you need to use it with Rails 2 please see the `rails2` branch (no longer maintained, limited feature set).
13
13
  * Works very well outside of Rails, you just need to install either the `json` (for MRI) or `json_pure` (for JRuby) gem.
14
14
 
15
15
 
16
- Install
17
- -------
16
+ Installation
17
+ ------------
18
+
19
+ Install Geocoder like any other Ruby gem:
18
20
 
19
- ### As a Gem
21
+ gem install geocoder
20
22
 
21
- Add to your Gemfile:
23
+ Or, if you're using Rails/Bundler, add this to your Gemfile:
22
24
 
23
25
  gem "geocoder"
24
26
 
@@ -26,21 +28,13 @@ and run at the command prompt:
26
28
 
27
29
  bundle install
28
30
 
29
- ### Or As a Plugin
30
-
31
- At the command prompt:
32
-
33
- rails plugin install git://github.com/alexreisner/geocoder.git
34
-
35
31
 
36
- Configure Object Geocoding
37
- --------------------------
38
-
39
- In the below, note that addresses may be street or IP addresses.
32
+ Object Geocoding
33
+ ----------------
40
34
 
41
35
  ### ActiveRecord
42
36
 
43
- Your model must have two attributes (database columns) for storing latitude and longitude coordinates. By default they should be called `latitude` and `longitude` but this can be changed (see "More on Configuration" below):
37
+ Your model must have two attributes (database columns) for storing latitude and longitude coordinates. By default they should be called `latitude` and `longitude` but this can be changed (see "Model Configuration" below):
44
38
 
45
39
  rails generate migration AddLatitudeAndLongitudeToModel latitude:float longitude:float
46
40
  rake db:migrate
@@ -81,6 +75,10 @@ Reverse geocoding is similar:
81
75
  reverse_geocoded_by :coordinates
82
76
  after_validation :reverse_geocode # auto-fetch address
83
77
 
78
+ Once you've set up your model you'll need to create the necessary spatial indices in your database:
79
+
80
+ rake db:mongoid:create_indexes
81
+
84
82
  Be sure to read _Latitude/Longitude Order_ in the _Notes on MongoDB_ section below on how to properly retrieve latitude/longitude coordinates from your objects.
85
83
 
86
84
  ### MongoMapper
@@ -126,9 +124,11 @@ To find objects by location, use the following scopes:
126
124
 
127
125
  With geocoded objects you can do things like this:
128
126
 
129
- obj.nearbys(30) # other objects within 30 miles
130
- obj.distance_from([40.714,-100.234]) # distance from arbitrary point to object
131
- obj.bearing_to("Paris, France") # direction from object to arbitrary point
127
+ if obj.geocoded?
128
+ obj.nearbys(30) # other objects within 30 miles
129
+ obj.distance_from([40.714,-100.234]) # distance from arbitrary point to object
130
+ obj.bearing_to("Paris, France") # direction from object to arbitrary point
131
+ end
132
132
 
133
133
  Some utility methods are also available:
134
134
 
@@ -181,8 +181,8 @@ To calculate accurate distance and bearing with SQLite or MongoDB:
181
181
  The `bearing_from/to` methods take a single argument which can be: a `[lat,lon]` array, a geocoded object, or a geocodable address (string). The `distance_from/to` methods also take a units argument (`:mi` or `:km`).
182
182
 
183
183
 
184
- More on Configuration
185
- ---------------------
184
+ Model Configuration
185
+ -------------------
186
186
 
187
187
  You are not stuck with using the `latitude` and `longitude` database column names (with ActiveRecord) or the `coordinates` array (Mongo) for storing coordinates. For example:
188
188
 
@@ -257,32 +257,37 @@ If you're familiar with the results returned by the geocoding service you're usi
257
257
  Geocoding Services
258
258
  ------------------
259
259
 
260
- By default Geocoder uses Google's geocoding API to fetch coordinates and street addresses (FreeGeoIP is used for IP address info). However there are several other APIs supported, as well as a variety of settings. Please see the listing and comparison below for details on specific geocoding services (not all settings are supported by all services). Some common configuration options are:
260
+ By default Geocoder uses Google's geocoding API to fetch coordinates and street addresses (FreeGeoIP is the default for IP address info). However there are several other APIs supported, as well as a variety of settings. Please see the listing and comparison below for details on specific geocoding services (not all settings are supported by all services). Some common configuration options are:
261
261
 
262
262
  # config/initializers/geocoder.rb
263
- Geocoder.configure do |config|
263
+ Geocoder.configure(
264
264
 
265
265
  # geocoding service (see below for supported options):
266
- config.lookup = :yandex
266
+ :lookup => :yandex,
267
267
 
268
268
  # to use an API key:
269
- config.api_key = "..."
269
+ :api_key => "...",
270
270
 
271
271
  # geocoding service request timeout, in seconds (default 3):
272
- config.timeout = 5
272
+ :timeout => 5,
273
273
 
274
274
  # set default units to kilometers:
275
- config.units = :km
275
+ :units => :km,
276
276
 
277
277
  # caching (see below for details):
278
- config.cache = Redis.new
279
- config.cache_prefix = "..."
278
+ :cache => Redis.new,
279
+ :cache_prefix => "..."
280
280
 
281
- end
281
+ )
282
+
283
+ Please see lib/geocoder/configuration.rb for a complete list of configuration options. Additionally, some lookups have their own configuration options, some of which are directly supported by Geocoder. For example, to specify a value for Google's `bounds` parameter:
284
+
285
+ # with Google:
286
+ Geocoder.search("Paris", :bounds => [[32.1,-95.9], [33.9,-94.3]])
282
287
 
283
- Please see lib/geocoder/configuration.rb for a complete list of configuration options. Additionally, some lookups have their own configuration options which are listed in the comparison chart below, and as of version 1.2.0 you can pass arbitrary parameters to any geocoding service. For example, to use Nominatim's `countrycodes` parameter:
288
+ Please see the [source code for each lookup](https://github.com/alexreisner/geocoder/tree/master/lib/geocoder/lookups) to learn about directly supported parameters. Parameters which are not directly supported can be specified using the `:params` option, by which you can pass arbitrary parameters to any geocoding service. For example, to use Nominatim's `countrycodes` parameter:
284
289
 
285
- Geocoder::Configuration.lookup = :nominatim
290
+ # with Nominatim:
286
291
  Geocoder.search("Paris", :params => {:countrycodes => "gb,de,fr,es,us"})
287
292
 
288
293
 
@@ -302,13 +307,13 @@ The following is a comparison of the supported geocoding APIs. The "Limitations"
302
307
  * **Documentation**: http://code.google.com/apis/maps/documentation/geocoding/#JSON
303
308
  * **Terms of Service**: http://code.google.com/apis/maps/terms.html#section_10_12
304
309
  * **Limitations**: "You must not use or display the Content without a corresponding Google map, unless you are explicitly permitted to do so in the Maps APIs Documentation, or through written permission from Google." "You must not pre-fetch, cache, or store any Content, except that you may store: (i) limited amounts of Content for the purpose of improving the performance of your Maps API Implementation..."
305
- * **Notes**: To use Google Premier set `Geocoder::Configuration.lookup = :google_premier` and `Geocoder::Configuration.api_key = [key, client, channel]`.
310
+ * **Notes**: To use Google Premier set `Geocoder.configure(:lookup => :google_premier, :api_key => [key, client, channel])`.
306
311
 
307
312
  #### Yahoo BOSS (`:yahoo`)
308
313
 
309
314
  Yahoo BOSS is **not a free service**. As of November 17, 2012 Yahoo no longer offers a free geocoding API.
310
315
 
311
- * **API key**: requires OAuth consumer key and secret (set `Geocoder::Configuration.api_key = [key, secret]`)
316
+ * **API key**: requires OAuth consumer key and secret (set `Geocoder.configure(:api_key => [key, secret])`)
312
317
  * **Key signup**: http://developer.yahoo.com/boss/geo/
313
318
  * **Quota**: unlimited, but subject to usage fees
314
319
  * **Region**: world
@@ -368,7 +373,7 @@ Yahoo BOSS is **not a free service**. As of November 17, 2012 Yahoo no longer of
368
373
  * **API key**: required for the licensed API, do not use for open tier
369
374
  * **Quota**: ?
370
375
  * **HTTP Headers**: in order to use the licensed API you can configure the http_headers to include a referer as so:
371
- `Geocoder::Configuration.http_headers = { "Referer" => "http://foo.com" }`
376
+ `Geocoder.configure(:http_headers => { "Referer" => "http://foo.com" })`
372
377
  You can also allow a blank referer from the API management console via mapquest but it is potentially a security risk that someone else could use your API key from another domain.
373
378
  * **Region**: world
374
379
  * **SSL support**: no
@@ -388,13 +393,24 @@ Yahoo BOSS is **not a free service**. As of November 17, 2012 Yahoo no longer of
388
393
  * **Terms of Service**: ?
389
394
  * **Limitations**: ?
390
395
 
396
+ #### MaxMind Web Services (`:maxmind`)
397
+
398
+ * **API key**: required
399
+ * **Quota**: Request Packs can be purchased
400
+ * **Region**: world
401
+ * **SSL support**: yes
402
+ * **Languages**: English
403
+ * **Documentation**: http://www.maxmind.com/app/web_services
404
+ * **Terms of Service**: ?
405
+ * **Limitations**: ?
406
+
391
407
 
392
408
  Caching
393
409
  -------
394
410
 
395
411
  It's a good idea, when relying on any external service, to cache retrieved data. When implemented correctly it improves your app's response time and stability. It's easy to cache geocoding results with Geocoder, just configure a cache store:
396
412
 
397
- Geocoder::Configuration.cache = Redis.new
413
+ Geocoder.configure(:cache => Redis.new)
398
414
 
399
415
  This example uses Redis, but the cache store can be any object that supports these methods:
400
416
 
@@ -407,7 +423,7 @@ Even a plain Ruby hash will work, though it's not a great choice (cleared out wh
407
423
 
408
424
  You can also set a custom prefix to be used for cache keys:
409
425
 
410
- Geocoder::Configuration.cache_prefix = "..."
426
+ Geocoder.configure(:cache_prefix => "...")
411
427
 
412
428
  By default the prefix is `geocoder:`
413
429
 
@@ -463,7 +479,7 @@ You can use Geocoder outside of Rails by calling the `Geocoder.search` method:
463
479
 
464
480
  results = Geocoder.search("McCarren Park, Brooklyn, NY")
465
481
 
466
- This returns an array of `Geocoder::Result` objects with all information provided by the geocoding service. Please see above and in the code for details.
482
+ This returns an array of `Geocoder::Result` objects with all data provided by the geocoding service.
467
483
 
468
484
 
469
485
  Testing Apps that Use Geocoder
@@ -471,7 +487,7 @@ Testing Apps that Use Geocoder
471
487
 
472
488
  When writing tests for an app that uses Geocoder it may be useful to avoid network calls and have Geocoder return consistent, configurable results. To do this, configure and use the `:test` lookup. For example:
473
489
 
474
- Geocoder::Configuration.lookup = :test
490
+ Geocoder.configure(:lookup => :test)
475
491
 
476
492
  Geocoder::Lookup::Test.add_stub(
477
493
  "New York, NY", [
@@ -507,7 +523,6 @@ When you install the Geocoder gem it adds a `geocode` command to your shell. You
507
523
 
508
524
  There are also a number of options for setting the geocoding API, key, and language, viewing the raw JSON reponse, and more. Please run `geocode -h` for details.
509
525
 
510
-
511
526
  Notes on MongoDB
512
527
  ----------------
513
528
 
@@ -529,6 +544,13 @@ Calling `obj.coordinates` directly returns the internal representation of the co
529
544
 
530
545
  For consistency with the rest of Geocoder, always use the `to_coordinates` method instead.
531
546
 
547
+ Notes on Non-Rails Frameworks
548
+ -----------------------------
549
+
550
+ If you are using Geocoder with ActiveRecord and a framework other than Rails (like Sinatra or Padrino) you will need to add this in your model before calling Geocoder methods:
551
+
552
+ extend Geocoder::Model::ActiveRecord
553
+
532
554
  Optimisation of Distance Queries
533
555
  --------------------------------
534
556
 
@@ -569,15 +591,44 @@ http://github.com/alexreisner/geocoder_test
569
591
  Error Handling
570
592
  --------------
571
593
 
572
- 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 `:always_raise` option:
594
+ By default Geocoder will rescue any exceptions raised by calls to a geocoding service and return an empty array (using warn() to inform you of the error). You can override this on a per-exception basis, and also have Geocoder raise its own exceptions for certain events (eg: API quota exceeded) by using the `:always_raise` option:
573
595
 
574
- Geocoder::Configuration.always_raise = [SocketError, TimeoutError]
596
+ Geocoder.configure(:always_raise => [SocketError, TimeoutError])
575
597
 
576
598
  You can also do this to raise all exceptions:
577
599
 
578
- Geocoder::Configuration.always_raise = :all
600
+ Geocoder.configure(:always_raise => :all)
601
+
602
+ The raise-able exceptions are:
603
+
604
+ SocketError
605
+ TimeoutError
606
+ Geocoder::OverQueryLimitError
607
+ Geocoder::RequestDenied
608
+ Geocoder::InvalidRequest
609
+ Geocoder::InvalidApiKey
610
+
611
+ Note that not all lookups support all exceptions.
612
+
613
+
614
+ Troubleshooting
615
+ ---------------
616
+
617
+ ### Mongoid
618
+
619
+ If you get one of these errors:
620
+
621
+ uninitialized constant Geocoder::Model::Mongoid
622
+ uninitialized constant Geocoder::Model::Mongoid::Mongo
623
+
624
+ you should check your Gemfile to make sure the Mongoid gem is listed _before_ Geocoder. If Mongoid isn't loaded when Geocoder is initialized, Geocoder will not load support for Mongoid.
625
+
626
+ ### ActiveRecord
627
+
628
+ A lot of debugging time can be saved by understanding how Geocoder works with ActiveRecord. When you use the `near` scope or the `nearbys` method of a geocoded object, Geocoder creates an ActiveModel::Relation object which adds some attributes (eg: distance, bearing) to the SELECT clause. It also adds a condition to the WHERE clause to check that distance is within the given radius. Because the SELECT clause is modified, anything else that modifies the SELECT clause may produce strange results, for example:
579
629
 
580
- See `lib/geocoder/exceptions.rb` for a list of raise-able exceptions.
630
+ * using the `pluck` method (selects only a single column)
631
+ * specifying another model through `includes` (selects columns from other tables)
581
632
 
582
633
 
583
634
  Known Issue