broken-geocoder 1.3.4

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.
Files changed (119) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +467 -0
  3. data/LICENSE +20 -0
  4. data/README.md +1193 -0
  5. data/bin/geocode +5 -0
  6. data/examples/autoexpire_cache_dalli.rb +62 -0
  7. data/examples/autoexpire_cache_redis.rb +28 -0
  8. data/examples/cache_bypass.rb +48 -0
  9. data/examples/reverse_geocode_job.rb +40 -0
  10. data/lib/generators/geocoder/config/config_generator.rb +14 -0
  11. data/lib/generators/geocoder/config/templates/initializer.rb +21 -0
  12. data/lib/generators/geocoder/maxmind/geolite_city_generator.rb +28 -0
  13. data/lib/generators/geocoder/maxmind/geolite_country_generator.rb +28 -0
  14. data/lib/generators/geocoder/maxmind/templates/migration/geolite_city.rb +30 -0
  15. data/lib/generators/geocoder/maxmind/templates/migration/geolite_country.rb +17 -0
  16. data/lib/geocoder.rb +48 -0
  17. data/lib/geocoder/cache.rb +90 -0
  18. data/lib/geocoder/calculations.rb +431 -0
  19. data/lib/geocoder/cli.rb +121 -0
  20. data/lib/geocoder/configuration.rb +129 -0
  21. data/lib/geocoder/configuration_hash.rb +11 -0
  22. data/lib/geocoder/esri_token.rb +38 -0
  23. data/lib/geocoder/exceptions.rb +37 -0
  24. data/lib/geocoder/ip_address.rb +13 -0
  25. data/lib/geocoder/kernel_logger.rb +25 -0
  26. data/lib/geocoder/logger.rb +47 -0
  27. data/lib/geocoder/lookup.rb +110 -0
  28. data/lib/geocoder/lookups/baidu.rb +59 -0
  29. data/lib/geocoder/lookups/baidu_ip.rb +59 -0
  30. data/lib/geocoder/lookups/base.rb +325 -0
  31. data/lib/geocoder/lookups/bing.rb +80 -0
  32. data/lib/geocoder/lookups/dstk.rb +20 -0
  33. data/lib/geocoder/lookups/esri.rb +64 -0
  34. data/lib/geocoder/lookups/freegeoip.rb +51 -0
  35. data/lib/geocoder/lookups/geocoder_ca.rb +53 -0
  36. data/lib/geocoder/lookups/geocoder_us.rb +43 -0
  37. data/lib/geocoder/lookups/geocodio.rb +42 -0
  38. data/lib/geocoder/lookups/geoip2.rb +45 -0
  39. data/lib/geocoder/lookups/geoportail_lu.rb +65 -0
  40. data/lib/geocoder/lookups/google.rb +91 -0
  41. data/lib/geocoder/lookups/google_places_details.rb +50 -0
  42. data/lib/geocoder/lookups/google_premier.rb +47 -0
  43. data/lib/geocoder/lookups/here.rb +62 -0
  44. data/lib/geocoder/lookups/ipapi_com.rb +86 -0
  45. data/lib/geocoder/lookups/ipinfo_io.rb +55 -0
  46. data/lib/geocoder/lookups/latlon.rb +59 -0
  47. data/lib/geocoder/lookups/mapbox.rb +53 -0
  48. data/lib/geocoder/lookups/mapquest.rb +59 -0
  49. data/lib/geocoder/lookups/mapzen.rb +15 -0
  50. data/lib/geocoder/lookups/maxmind.rb +90 -0
  51. data/lib/geocoder/lookups/maxmind_geoip2.rb +69 -0
  52. data/lib/geocoder/lookups/maxmind_local.rb +65 -0
  53. data/lib/geocoder/lookups/nominatim.rb +52 -0
  54. data/lib/geocoder/lookups/okf.rb +44 -0
  55. data/lib/geocoder/lookups/opencagedata.rb +58 -0
  56. data/lib/geocoder/lookups/ovi.rb +62 -0
  57. data/lib/geocoder/lookups/pelias.rb +64 -0
  58. data/lib/geocoder/lookups/pointpin.rb +68 -0
  59. data/lib/geocoder/lookups/postcode_anywhere_uk.rb +51 -0
  60. data/lib/geocoder/lookups/smarty_streets.rb +50 -0
  61. data/lib/geocoder/lookups/telize.rb +55 -0
  62. data/lib/geocoder/lookups/test.rb +44 -0
  63. data/lib/geocoder/lookups/yandex.rb +58 -0
  64. data/lib/geocoder/models/active_record.rb +50 -0
  65. data/lib/geocoder/models/base.rb +39 -0
  66. data/lib/geocoder/models/mongo_base.rb +62 -0
  67. data/lib/geocoder/models/mongo_mapper.rb +26 -0
  68. data/lib/geocoder/models/mongoid.rb +32 -0
  69. data/lib/geocoder/query.rb +111 -0
  70. data/lib/geocoder/railtie.rb +26 -0
  71. data/lib/geocoder/request.rb +83 -0
  72. data/lib/geocoder/results/baidu.rb +79 -0
  73. data/lib/geocoder/results/baidu_ip.rb +62 -0
  74. data/lib/geocoder/results/base.rb +67 -0
  75. data/lib/geocoder/results/bing.rb +52 -0
  76. data/lib/geocoder/results/dstk.rb +6 -0
  77. data/lib/geocoder/results/esri.rb +75 -0
  78. data/lib/geocoder/results/freegeoip.rb +45 -0
  79. data/lib/geocoder/results/geocoder_ca.rb +60 -0
  80. data/lib/geocoder/results/geocoder_us.rb +39 -0
  81. data/lib/geocoder/results/geocodio.rb +70 -0
  82. data/lib/geocoder/results/geoip2.rb +62 -0
  83. data/lib/geocoder/results/geoportail_lu.rb +69 -0
  84. data/lib/geocoder/results/google.rb +139 -0
  85. data/lib/geocoder/results/google_places_details.rb +35 -0
  86. data/lib/geocoder/results/google_premier.rb +6 -0
  87. data/lib/geocoder/results/here.rb +71 -0
  88. data/lib/geocoder/results/ipapi_com.rb +45 -0
  89. data/lib/geocoder/results/ipinfo_io.rb +48 -0
  90. data/lib/geocoder/results/latlon.rb +71 -0
  91. data/lib/geocoder/results/mapbox.rb +47 -0
  92. data/lib/geocoder/results/mapquest.rb +48 -0
  93. data/lib/geocoder/results/mapzen.rb +5 -0
  94. data/lib/geocoder/results/maxmind.rb +135 -0
  95. data/lib/geocoder/results/maxmind_geoip2.rb +9 -0
  96. data/lib/geocoder/results/maxmind_local.rb +49 -0
  97. data/lib/geocoder/results/nominatim.rb +99 -0
  98. data/lib/geocoder/results/okf.rb +106 -0
  99. data/lib/geocoder/results/opencagedata.rb +90 -0
  100. data/lib/geocoder/results/ovi.rb +71 -0
  101. data/lib/geocoder/results/pelias.rb +58 -0
  102. data/lib/geocoder/results/pointpin.rb +40 -0
  103. data/lib/geocoder/results/postcode_anywhere_uk.rb +42 -0
  104. data/lib/geocoder/results/smarty_streets.rb +106 -0
  105. data/lib/geocoder/results/telize.rb +45 -0
  106. data/lib/geocoder/results/test.rb +33 -0
  107. data/lib/geocoder/results/yandex.rb +92 -0
  108. data/lib/geocoder/sql.rb +107 -0
  109. data/lib/geocoder/stores/active_record.rb +305 -0
  110. data/lib/geocoder/stores/base.rb +116 -0
  111. data/lib/geocoder/stores/mongo_base.rb +58 -0
  112. data/lib/geocoder/stores/mongo_mapper.rb +13 -0
  113. data/lib/geocoder/stores/mongoid.rb +13 -0
  114. data/lib/geocoder/version.rb +3 -0
  115. data/lib/hash_recursive_merge.rb +74 -0
  116. data/lib/maxmind_database.rb +109 -0
  117. data/lib/tasks/geocoder.rake +38 -0
  118. data/lib/tasks/maxmind.rake +73 -0
  119. metadata +167 -0
@@ -0,0 +1,431 @@
1
+ module Geocoder
2
+ module Calculations
3
+ extend self
4
+
5
+ ##
6
+ # Compass point names, listed clockwise starting at North.
7
+ #
8
+ # If you want bearings named using more, fewer, or different points
9
+ # override Geocoder::Calculations.COMPASS_POINTS with your own array.
10
+ #
11
+ COMPASS_POINTS = %w[N NE E SE S SW W NW]
12
+
13
+ ##
14
+ # Radius of the Earth, in kilometers.
15
+ # Value taken from: http://en.wikipedia.org/wiki/Earth_radius
16
+ #
17
+ EARTH_RADIUS = 6371.0
18
+
19
+ ##
20
+ # Conversion factor: multiply by kilometers to get miles.
21
+ #
22
+ KM_IN_MI = 0.621371192
23
+
24
+ ##
25
+ # Conversion factor: multiply by nautical miles to get miles.
26
+ #
27
+ KM_IN_NM = 0.539957
28
+
29
+ ##
30
+ # Conversion factor: multiply by radians to get degrees.
31
+ #
32
+ DEGREES_PER_RADIAN = 57.2957795
33
+
34
+ # Not a number constant
35
+ NAN = defined?(::Float::NAN) ? ::Float::NAN : 0 / 0.0
36
+
37
+ ##
38
+ # Returns true if all given arguments are valid latitude/longitude values.
39
+ #
40
+ def coordinates_present?(*args)
41
+ args.each do |a|
42
+ # note that Float::NAN != Float::NAN
43
+ # still, this could probably be improved:
44
+ return false if (!a.is_a?(Numeric) or a.to_s == "NaN")
45
+ end
46
+ true
47
+ end
48
+
49
+ ##
50
+ # Distance spanned by one degree of latitude in the given units.
51
+ #
52
+ def latitude_degree_distance(units = nil)
53
+ units ||= Geocoder.config.units
54
+ 2 * Math::PI * earth_radius(units) / 360
55
+ end
56
+
57
+ ##
58
+ # Distance spanned by one degree of longitude at the given latitude.
59
+ # This ranges from around 69 miles at the equator to zero at the poles.
60
+ #
61
+ def longitude_degree_distance(latitude, units = nil)
62
+ units ||= Geocoder.config.units
63
+ latitude_degree_distance(units) * Math.cos(to_radians(latitude))
64
+ end
65
+
66
+ ##
67
+ # Distance between two points on Earth (Haversine formula).
68
+ # Takes two points and an options hash.
69
+ # The points are given in the same way that points are given to all
70
+ # Geocoder methods that accept points as arguments. They can be:
71
+ #
72
+ # * an array of coordinates ([lat,lon])
73
+ # * a geocodable address (string)
74
+ # * a geocoded object (one which implements a +to_coordinates+ method
75
+ # which returns a [lat,lon] array
76
+ #
77
+ # The options hash supports:
78
+ #
79
+ # * <tt>:units</tt> - <tt>:mi</tt> or <tt>:km</tt>
80
+ # Use Geocoder.configure(:units => ...) to configure default units.
81
+ #
82
+ def distance_between(point1, point2, options = {})
83
+
84
+ # set default options
85
+ options[:units] ||= Geocoder.config.units
86
+
87
+ # convert to coordinate arrays
88
+ point1 = extract_coordinates(point1)
89
+ point2 = extract_coordinates(point2)
90
+
91
+ # convert degrees to radians
92
+ point1 = to_radians(point1)
93
+ point2 = to_radians(point2)
94
+
95
+ # compute deltas
96
+ dlat = point2[0] - point1[0]
97
+ dlon = point2[1] - point1[1]
98
+
99
+ a = (Math.sin(dlat / 2))**2 + Math.cos(point1[0]) *
100
+ (Math.sin(dlon / 2))**2 * Math.cos(point2[0])
101
+ c = 2 * Math.atan2( Math.sqrt(a), Math.sqrt(1-a))
102
+ c * earth_radius(options[:units])
103
+ end
104
+
105
+ ##
106
+ # Bearing between two points on Earth.
107
+ # Returns a number of degrees from due north (clockwise).
108
+ #
109
+ # See Geocoder::Calculations.distance_between for
110
+ # ways of specifying the points. Also accepts an options hash:
111
+ #
112
+ # * <tt>:method</tt> - <tt>:linear</tt> or <tt>:spherical</tt>;
113
+ # the spherical method is "correct" in that it returns the shortest path
114
+ # (one along a great circle) but the linear method is less confusing
115
+ # (returns due east or west when given two points with the same latitude).
116
+ # Use Geocoder.configure(:distances => ...) to configure calculation method.
117
+ #
118
+ # Based on: http://www.movable-type.co.uk/scripts/latlong.html
119
+ #
120
+ def bearing_between(point1, point2, options = {})
121
+
122
+ # set default options
123
+ options[:method] ||= Geocoder.config.distances
124
+ options[:method] = :linear unless options[:method] == :spherical
125
+
126
+ # convert to coordinate arrays
127
+ point1 = extract_coordinates(point1)
128
+ point2 = extract_coordinates(point2)
129
+
130
+ # convert degrees to radians
131
+ point1 = to_radians(point1)
132
+ point2 = to_radians(point2)
133
+
134
+ # compute deltas
135
+ dlat = point2[0] - point1[0]
136
+ dlon = point2[1] - point1[1]
137
+
138
+ case options[:method]
139
+ when :linear
140
+ y = dlon
141
+ x = dlat
142
+
143
+ when :spherical
144
+ y = Math.sin(dlon) * Math.cos(point2[0])
145
+ x = Math.cos(point1[0]) * Math.sin(point2[0]) -
146
+ Math.sin(point1[0]) * Math.cos(point2[0]) * Math.cos(dlon)
147
+ end
148
+
149
+ bearing = Math.atan2(x,y)
150
+ # Answer is in radians counterclockwise from due east.
151
+ # Convert to degrees clockwise from due north:
152
+ (90 - to_degrees(bearing) + 360) % 360
153
+ end
154
+
155
+ ##
156
+ # Translate a bearing (float) into a compass direction (string, eg "North").
157
+ #
158
+ def compass_point(bearing, points = COMPASS_POINTS)
159
+ seg_size = 360 / points.size
160
+ points[((bearing + (seg_size / 2)) % 360) / seg_size]
161
+ end
162
+
163
+ ##
164
+ # Compute the geographic center (aka geographic midpoint, center of
165
+ # gravity) for an array of geocoded objects and/or [lat,lon] arrays
166
+ # (can be mixed). Any objects missing coordinates are ignored. Follows
167
+ # the procedure documented at http://www.geomidpoint.com/calculation.html.
168
+ #
169
+ def geographic_center(points)
170
+
171
+ # convert objects to [lat,lon] arrays and convert degrees to radians
172
+ coords = points.map{ |p| to_radians(extract_coordinates(p)) }
173
+
174
+ # convert to Cartesian coordinates
175
+ x = []; y = []; z = []
176
+ coords.each do |p|
177
+ x << Math.cos(p[0]) * Math.cos(p[1])
178
+ y << Math.cos(p[0]) * Math.sin(p[1])
179
+ z << Math.sin(p[0])
180
+ end
181
+
182
+ # compute average coordinate values
183
+ xa, ya, za = [x,y,z].map do |c|
184
+ c.inject(0){ |tot,i| tot += i } / c.size.to_f
185
+ end
186
+
187
+ # convert back to latitude/longitude
188
+ lon = Math.atan2(ya, xa)
189
+ hyp = Math.sqrt(xa**2 + ya**2)
190
+ lat = Math.atan2(za, hyp)
191
+
192
+ # return answer in degrees
193
+ to_degrees [lat, lon]
194
+ end
195
+
196
+ ##
197
+ # Returns coordinates of the southwest and northeast corners of a box
198
+ # with the given point at its center. The radius is the shortest distance
199
+ # from the center point to any side of the box (the length of each side
200
+ # is twice the radius).
201
+ #
202
+ # This is useful for finding corner points of a map viewport, or for
203
+ # roughly limiting the possible solutions in a geo-spatial search
204
+ # (ActiveRecord queries use it thusly).
205
+ #
206
+ # See Geocoder::Calculations.distance_between for
207
+ # ways of specifying the point. Also accepts an options hash:
208
+ #
209
+ # * <tt>:units</tt> - <tt>:mi</tt> or <tt>:km</tt>.
210
+ # Use Geocoder.configure(:units => ...) to configure default units.
211
+ #
212
+ def bounding_box(point, radius, options = {})
213
+ lat,lon = extract_coordinates(point)
214
+ radius = radius.to_f
215
+ units = options[:units] || Geocoder.config.units
216
+ [
217
+ lat - (radius / latitude_degree_distance(units)),
218
+ lon - (radius / longitude_degree_distance(lat, units)),
219
+ lat + (radius / latitude_degree_distance(units)),
220
+ lon + (radius / longitude_degree_distance(lat, units))
221
+ ]
222
+ end
223
+
224
+ ##
225
+ # Random point within a circle of provided radius centered
226
+ # around the provided point
227
+ # Takes one point, one radius, and an options hash.
228
+ # The points are given in the same way that points are given to all
229
+ # Geocoder methods that accept points as arguments. They can be:
230
+ #
231
+ # * an array of coordinates ([lat,lon])
232
+ # * a geocodable address (string)
233
+ # * a geocoded object (one which implements a +to_coordinates+ method
234
+ # which returns a [lat,lon] array
235
+ #
236
+ # The options hash supports:
237
+ #
238
+ # * <tt>:units</tt> - <tt>:mi</tt> or <tt>:km</tt>
239
+ # Use Geocoder.configure(:units => ...) to configure default units.
240
+ # * <tt>:seed</tt> - The seed for the random number generator
241
+ def random_point_near(center, radius, options = {})
242
+
243
+ # set default options
244
+ options[:units] ||= Geocoder.config.units
245
+
246
+ random = Random.new(options[:seed] || Random.new_seed)
247
+
248
+ # convert to coordinate arrays
249
+ center = extract_coordinates(center)
250
+
251
+ earth_circumference = 2 * Math::PI * earth_radius(options[:units])
252
+ max_degree_delta = 360.0 * (radius / earth_circumference)
253
+
254
+ # random bearing in radians
255
+ theta = 2 * Math::PI * random.rand
256
+
257
+ # random radius, use the square root to ensure a uniform
258
+ # distribution of points over the circle
259
+ r = Math.sqrt(random.rand) * max_degree_delta
260
+
261
+ delta_lat, delta_long = [r * Math.cos(theta), r * Math.sin(theta)]
262
+ [center[0] + delta_lat, center[1] + delta_long]
263
+ end
264
+
265
+ ##
266
+ # Given a start point, heading (in degrees), and distance, provides
267
+ # an endpoint.
268
+ # The starting point is given in the same way that points are given to all
269
+ # Geocoder methods that accept points as arguments. It can be:
270
+ #
271
+ # * an array of coordinates ([lat,lon])
272
+ # * a geocodable address (string)
273
+ # * a geocoded object (one which implements a +to_coordinates+ method
274
+ # which returns a [lat,lon] array
275
+ #
276
+ def endpoint(start, heading, distance, options = {})
277
+ options[:units] ||= Geocoder.config.units
278
+ radius = earth_radius(options[:units])
279
+
280
+ start = extract_coordinates(start)
281
+
282
+ # convert degrees to radians
283
+ start = to_radians(start)
284
+
285
+ lat = start[0]
286
+ lon = start[1]
287
+ heading = to_radians(heading)
288
+ distance = distance.to_f
289
+
290
+ end_lat = Math.asin(Math.sin(lat)*Math.cos(distance/radius) +
291
+ Math.cos(lat)*Math.sin(distance/radius)*Math.cos(heading))
292
+
293
+ end_lon = lon+Math.atan2(Math.sin(heading)*Math.sin(distance/radius)*Math.cos(lat),
294
+ Math.cos(distance/radius)-Math.sin(lat)*Math.sin(end_lat))
295
+
296
+ to_degrees [end_lat, end_lon]
297
+ end
298
+
299
+ ##
300
+ # Convert degrees to radians.
301
+ # If an array (or multiple arguments) is passed,
302
+ # converts each value and returns array.
303
+ #
304
+ def to_radians(*args)
305
+ args = args.first if args.first.is_a?(Array)
306
+ if args.size == 1
307
+ args.first * (Math::PI / 180)
308
+ else
309
+ args.map{ |i| to_radians(i) }
310
+ end
311
+ end
312
+
313
+ ##
314
+ # Convert radians to degrees.
315
+ # If an array (or multiple arguments) is passed,
316
+ # converts each value and returns array.
317
+ #
318
+ def to_degrees(*args)
319
+ args = args.first if args.first.is_a?(Array)
320
+ if args.size == 1
321
+ (args.first * 180.0) / Math::PI
322
+ else
323
+ args.map{ |i| to_degrees(i) }
324
+ end
325
+ end
326
+
327
+ def distance_to_radians(distance, units = nil)
328
+ units ||= Geocoder.config.units
329
+ distance.to_f / earth_radius(units)
330
+ end
331
+
332
+ def radians_to_distance(radians, units = nil)
333
+ units ||= Geocoder.config.units
334
+ radians * earth_radius(units)
335
+ end
336
+
337
+ ##
338
+ # Convert miles to kilometers.
339
+ #
340
+ def to_kilometers(mi)
341
+ mi * mi_in_km
342
+ end
343
+
344
+ ##
345
+ # Convert kilometers to miles.
346
+ #
347
+ def to_miles(km)
348
+ km * km_in_mi
349
+ end
350
+
351
+ ##
352
+ # Convert kilometers to nautical miles.
353
+ #
354
+ def to_nautical_miles(km)
355
+ km * km_in_nm
356
+ end
357
+
358
+ ##
359
+ # Radius of the Earth in the given units (:mi or :km).
360
+ # Use Geocoder.configure(:units => ...) to configure default units.
361
+ #
362
+ def earth_radius(units = nil)
363
+ units ||= Geocoder.config.units
364
+ case units
365
+ when :km; EARTH_RADIUS
366
+ when :mi; to_miles(EARTH_RADIUS)
367
+ when :nm; to_nautical_miles(EARTH_RADIUS)
368
+ end
369
+ end
370
+
371
+ ##
372
+ # Conversion factor: km to mi.
373
+ #
374
+ def km_in_mi
375
+ KM_IN_MI
376
+ end
377
+
378
+ ##
379
+ # Conversion factor: km to nm.
380
+ #
381
+ def km_in_nm
382
+ KM_IN_NM
383
+ end
384
+
385
+
386
+
387
+ ##
388
+ # Conversion factor: mi to km.
389
+ #
390
+ def mi_in_km
391
+ 1.0 / KM_IN_MI
392
+ end
393
+
394
+ ##
395
+ # Conversion factor: nm to km.
396
+ #
397
+ def nm_in_km
398
+ 1.0 / KM_IN_NM
399
+ end
400
+
401
+ ##
402
+ # Takes an object which is a [lat,lon] array, a geocodable string,
403
+ # or an object that implements +to_coordinates+ and returns a
404
+ # [lat,lon] array. Note that if a string is passed this may be a slow-
405
+ # running method and may return nil.
406
+ #
407
+ def extract_coordinates(point)
408
+ case point
409
+ when Array
410
+ if point.size == 2
411
+ lat, lon = point
412
+ if !lat.nil? && lat.respond_to?(:to_f) and
413
+ !lon.nil? && lon.respond_to?(:to_f)
414
+ then
415
+ return [ lat.to_f, lon.to_f ]
416
+ end
417
+ end
418
+ when String
419
+ point = Geocoder.coordinates(point) and return point
420
+ else
421
+ if point.respond_to?(:to_coordinates)
422
+ if Array === array = point.to_coordinates
423
+ return extract_coordinates(array)
424
+ end
425
+ end
426
+ end
427
+ [ NAN, NAN ]
428
+ end
429
+ end
430
+ end
431
+
@@ -0,0 +1,121 @@
1
+ require 'geocoder'
2
+ require 'optparse'
3
+
4
+ module Geocoder
5
+ class Cli
6
+
7
+ def self.run(args, out = STDOUT)
8
+ show_url = false
9
+ show_json = false
10
+
11
+ # remove arguments that are probably coordinates so they are not
12
+ # processed as arguments (eg: -31.96047031,115.84274631)
13
+ coords = args.select{ |i| i.match(/^-\d/) }
14
+ args -= coords
15
+
16
+ OptionParser.new{ |opts|
17
+ opts.banner = "Usage:\n geocode [options] <location>"
18
+ opts.separator "\nOptions: "
19
+
20
+ opts.on("-k <key>", "--key <key>",
21
+ "Key for geocoding API (usually optional). Enclose multi-part keys in quotes and separate parts by spaces") do |key|
22
+ if (key_parts = key.split(/\s+/)).size > 1
23
+ Geocoder.configure(:api_key => key_parts)
24
+ else
25
+ Geocoder.configure(:api_key => key)
26
+ end
27
+ end
28
+
29
+ opts.on("-l <language>", "--language <language>",
30
+ "Language of output (see API docs for valid choices)") do |language|
31
+ Geocoder.configure(:language => language)
32
+ end
33
+
34
+ opts.on("-p <proxy>", "--proxy <proxy>",
35
+ "HTTP proxy server to use (user:pass@host:port)") do |proxy|
36
+ Geocoder.configure(:http_proxy => proxy)
37
+ end
38
+
39
+ opts.on("-s <service>", Geocoder::Lookup.all_services_except_test, "--service <service>",
40
+ "Geocoding service: #{Geocoder::Lookup.all_services_except_test * ', '}") do |service|
41
+ Geocoder.configure(:lookup => service.to_sym)
42
+ Geocoder.configure(:ip_lookup => service.to_sym)
43
+ end
44
+
45
+ opts.on("-t <seconds>", "--timeout <seconds>",
46
+ "Maximum number of seconds to wait for API response") do |timeout|
47
+ Geocoder.configure(:timeout => timeout.to_i)
48
+ end
49
+
50
+ opts.on("-j", "--json", "Print API's raw JSON response") do
51
+ show_json = true
52
+ end
53
+
54
+ opts.on("-u", "--url", "Print URL for API query instead of result") do
55
+ show_url = true
56
+ end
57
+
58
+ opts.on_tail("-v", "--version", "Print version number") do
59
+ require "geocoder/version"
60
+ out << "Geocoder #{Geocoder::VERSION}\n"
61
+ exit
62
+ end
63
+
64
+ opts.on_tail("-h", "--help", "Print this help") do
65
+ out << "Look up geographic information about a location.\n\n"
66
+ out << opts
67
+ out << "\nCreated and maintained by Alex Reisner, available under the MIT License.\n"
68
+ out << "Report bugs and contribute at http://github.com/alexreisner/geocoder\n"
69
+ exit
70
+ end
71
+ }.parse!(args)
72
+
73
+ # concatenate args with coords that might have been removed
74
+ # before option processing
75
+ query = (args + coords).join(" ")
76
+
77
+ if query == ""
78
+ out << "Please specify a location (run `geocode -h` for more info).\n"
79
+ exit 1
80
+ end
81
+
82
+ if show_url and show_json
83
+ out << "You can only specify one of -j and -u.\n"
84
+ exit 2
85
+ end
86
+
87
+ if show_url
88
+ q = Geocoder::Query.new(query)
89
+ out << q.url + "\n"
90
+ exit 0
91
+ end
92
+
93
+ if show_json
94
+ q = Geocoder::Query.new(query)
95
+ out << q.lookup.send(:fetch_raw_data, q) + "\n"
96
+ exit 0
97
+ end
98
+
99
+ if (result = Geocoder.search(query).first)
100
+ google = Geocoder::Lookup.get(:google)
101
+ lines = [
102
+ ["Latitude", result.latitude],
103
+ ["Longitude", result.longitude],
104
+ ["Full address", result.address],
105
+ ["City", result.city],
106
+ ["State/province", result.state],
107
+ ["Postal code", result.postal_code],
108
+ ["Country", result.country],
109
+ ["Google map", google.map_link_url(result.coordinates)],
110
+ ]
111
+ lines.each do |line|
112
+ out << (line[0] + ": ").ljust(18) + line[1].to_s + "\n"
113
+ end
114
+ exit 0
115
+ else
116
+ out << "Location '#{query}' not found.\n"
117
+ exit 1
118
+ end
119
+ end
120
+ end
121
+ end