geocoder 1.1.4 → 1.1.8

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 (110) hide show
  1. data/.travis.yml +4 -0
  2. data/{CHANGELOG.rdoc → CHANGELOG.md} +120 -39
  3. data/README.md +200 -54
  4. data/examples/autoexpire_cache_dalli.rb +62 -0
  5. data/examples/{autoexpire_cache.rb → autoexpire_cache_redis.rb} +4 -6
  6. data/lib/generators/geocoder/config/templates/initializer.rb +19 -23
  7. data/lib/geocoder/cache.rb +6 -1
  8. data/lib/geocoder/calculations.rb +45 -13
  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 +3 -1
  14. data/lib/geocoder/lookups/base.rb +89 -27
  15. data/lib/geocoder/lookups/bing.rb +21 -11
  16. data/lib/geocoder/lookups/esri.rb +48 -0
  17. data/lib/geocoder/lookups/freegeoip.rb +8 -4
  18. data/lib/geocoder/lookups/geocoder_ca.rb +11 -7
  19. data/lib/geocoder/lookups/google.rb +18 -9
  20. data/lib/geocoder/lookups/google_premier.rb +16 -8
  21. data/lib/geocoder/lookups/mapquest.rb +33 -5
  22. data/lib/geocoder/lookups/maxmind.rb +88 -0
  23. data/lib/geocoder/lookups/nominatim.rb +13 -8
  24. data/lib/geocoder/lookups/ovi.rb +52 -0
  25. data/lib/geocoder/lookups/test.rb +10 -0
  26. data/lib/geocoder/lookups/yahoo.rb +51 -38
  27. data/lib/geocoder/lookups/yandex.rb +17 -9
  28. data/lib/geocoder/models/mongoid.rb +1 -1
  29. data/lib/geocoder/query.rb +20 -5
  30. data/lib/geocoder/request.rb +7 -1
  31. data/lib/geocoder/results/base.rb +8 -2
  32. data/lib/geocoder/results/esri.rb +51 -0
  33. data/lib/geocoder/results/google.rb +34 -0
  34. data/lib/geocoder/results/mapquest.rb +46 -2
  35. data/lib/geocoder/results/maxmind.rb +135 -0
  36. data/lib/geocoder/results/nominatim.rb +0 -10
  37. data/lib/geocoder/results/ovi.rb +62 -0
  38. data/lib/geocoder/results/test.rb +2 -1
  39. data/lib/geocoder/results/yahoo.rb +9 -2
  40. data/lib/geocoder/results/yandex.rb +12 -2
  41. data/lib/geocoder/sql.rb +6 -4
  42. data/lib/geocoder/stores/active_record.rb +57 -30
  43. data/lib/geocoder/stores/base.rb +3 -2
  44. data/lib/geocoder/stores/mongo_base.rb +2 -1
  45. data/lib/geocoder/version.rb +1 -1
  46. data/lib/geocoder.rb +2 -4
  47. data/lib/hash_recursive_merge.rb +74 -0
  48. data/lib/oauth_util.rb +112 -0
  49. data/test/cache_test.rb +19 -0
  50. data/test/calculations_test.rb +10 -4
  51. data/test/configuration_test.rb +26 -51
  52. data/test/error_handling_test.rb +8 -4
  53. data/test/fixtures/bing_invalid_key +1 -0
  54. data/test/fixtures/esri_madison_square_garden +59 -0
  55. data/test/fixtures/esri_no_results +8 -0
  56. data/test/fixtures/esri_reverse +21 -0
  57. data/test/fixtures/mapquest_madison_square_garden +52 -0
  58. data/test/fixtures/mapquest_no_results +7 -0
  59. data/test/fixtures/maxmind_24_24_24_21 +1 -0
  60. data/test/fixtures/maxmind_24_24_24_22 +1 -0
  61. data/test/fixtures/maxmind_24_24_24_23 +1 -0
  62. data/test/fixtures/maxmind_24_24_24_24 +1 -0
  63. data/test/fixtures/maxmind_74_200_247_59 +1 -0
  64. data/test/fixtures/maxmind_invalid_key +1 -0
  65. data/test/fixtures/maxmind_no_results +1 -0
  66. data/test/fixtures/ovi_madison_square_garden +72 -0
  67. data/test/fixtures/ovi_no_results +8 -0
  68. data/test/fixtures/yahoo_error +1 -0
  69. data/test/fixtures/yahoo_invalid_key +2 -0
  70. data/test/fixtures/yahoo_madison_square_garden +52 -0
  71. data/test/fixtures/yahoo_no_results +10 -0
  72. data/test/fixtures/yahoo_over_limit +2 -0
  73. data/test/fixtures/yandex_no_city_and_town +112 -0
  74. data/test/https_test.rb +3 -3
  75. data/test/integration/smoke_test.rb +2 -2
  76. data/test/lookup_test.rb +84 -11
  77. data/test/near_test.rb +43 -0
  78. data/test/oauth_util_test.rb +30 -0
  79. data/test/proxy_test.rb +2 -2
  80. data/test/query_test.rb +13 -0
  81. data/test/request_test.rb +29 -0
  82. data/test/result_test.rb +12 -2
  83. data/test/services_test.rb +208 -57
  84. data/test/test_helper.rb +66 -100
  85. data/test/test_mode_test.rb +35 -26
  86. metadata +58 -35
  87. data/test/fixtures/mapquest_madison_square_garden.json +0 -27
  88. data/test/fixtures/mapquest_no_results.json +0 -1
  89. data/test/fixtures/yahoo_madison_square_garden.json +0 -52
  90. data/test/fixtures/yahoo_no_results.json +0 -10
  91. data/test/fixtures/yahoo_v1_madison_square_garden.json +0 -46
  92. data/test/fixtures/yahoo_v1_no_results.json +0 -10
  93. /data/test/fixtures/{bing_madison_square_garden.json → bing_madison_square_garden} +0 -0
  94. /data/test/fixtures/{bing_no_results.json → bing_no_results} +0 -0
  95. /data/test/fixtures/{bing_reverse.json → bing_reverse} +0 -0
  96. /data/test/fixtures/{freegeoip_74_200_247_59.json → freegeoip_74_200_247_59} +0 -0
  97. /data/test/fixtures/{freegeoip_no_results.json → freegeoip_no_results} +0 -0
  98. /data/test/fixtures/{geocoder_ca_madison_square_garden.json → geocoder_ca_madison_square_garden} +0 -0
  99. /data/test/fixtures/{geocoder_ca_no_results.json → geocoder_ca_no_results} +0 -0
  100. /data/test/fixtures/{geocoder_ca_reverse.json → geocoder_ca_reverse} +0 -0
  101. /data/test/fixtures/{google_garbage.json → google_garbage} +0 -0
  102. /data/test/fixtures/{google_madison_square_garden.json → google_madison_square_garden} +0 -0
  103. /data/test/fixtures/{google_no_city_data.json → google_no_city_data} +0 -0
  104. /data/test/fixtures/{google_no_locality.json → google_no_locality} +0 -0
  105. /data/test/fixtures/{google_no_results.json → google_no_results} +0 -0
  106. /data/test/fixtures/{nominatim_madison_square_garden.json → nominatim_madison_square_garden} +0 -0
  107. /data/test/fixtures/{nominatim_no_results.json → nominatim_no_results} +0 -0
  108. /data/test/fixtures/{yandex_invalid_key.json → yandex_invalid_key} +0 -0
  109. /data/test/fixtures/{yandex_kremlin.json → yandex_kremlin} +0 -0
  110. /data/test/fixtures/{yandex_no_results.json → yandex_no_results} +0 -0
@@ -0,0 +1,21 @@
1
+ {
2
+ "address": {
3
+ "Address": "4 Avenue Gustave Eiffel",
4
+ "Neighborhood": "7e Arrondissement",
5
+ "City": "Paris",
6
+ "Subregion": "Paris",
7
+ "Region": "Île-de-France",
8
+ "Postal": "75007",
9
+ "PostalExt": null,
10
+ "CountryCode": "FRA",
11
+ "Loc_name": "FRA.PointAddress"
12
+ },
13
+ "location": {
14
+ "x": 2.2956200048981574,
15
+ "y": 48.858129997357558,
16
+ "spatialReference": {
17
+ "wkid": 4326,
18
+ "latestWkid": 4326
19
+ }
20
+ }
21
+ }
@@ -0,0 +1,52 @@
1
+ {
2
+ "results": [
3
+ {
4
+ "locations": [
5
+ {
6
+ "latLng": {
7
+ "lng": -73.994637,
8
+ "lat": 40.720409
9
+ },
10
+ "adminArea4": "New York County",
11
+ "adminArea5Type": "City",
12
+ "adminArea4Type": "County",
13
+ "adminArea5": "New York",
14
+ "street": "46 West 31st Street",
15
+ "adminArea1": "US",
16
+ "adminArea3": "NY",
17
+ "type": "s",
18
+ "displayLatLng": {
19
+ "lng": -73.994637,
20
+ "lat": 40.720409
21
+ },
22
+ "linkId": 0,
23
+ "postalCode": "10001",
24
+ "sideOfStreet": "N",
25
+ "dragPoint": false,
26
+ "adminArea1Type": "Country",
27
+ "geocodeQuality": "CITY",
28
+ "geocodeQualityCode": "A5XAX",
29
+ "mapUrl": "http://www.mapquestapi.com/staticmap/v3/getmap?type=map&size=225,160&pois=purple-1,40.720409,-73.994637,0,0|&center=40.720409,-73.994637&zoom=9&key=Gmjtd|luua2hu2nd,7x=o5-lz8lg&rand=604519389",
30
+ "adminArea3Type": "State"
31
+ }
32
+ ],
33
+ "providedLocation": {
34
+ "location": "Madison Square Garden, New York, NY"
35
+ }
36
+ }
37
+ ],
38
+ "options": {
39
+ "ignoreLatLngInput": false,
40
+ "maxResults": -1,
41
+ "thumbMaps": true
42
+ },
43
+ "info": {
44
+ "copyright": {
45
+ "text": "© 2012 MapQuest, Inc.",
46
+ "imageUrl": "http://api.mqcdn.com/res/mqlogo.gif",
47
+ "imageAltText": "© 2012 MapQuest, Inc."
48
+ },
49
+ "statuscode": 0,
50
+ "messages": []
51
+ }
52
+ }
@@ -0,0 +1,7 @@
1
+ {
2
+ "results": [
3
+ {
4
+ "locations": []
5
+ }
6
+ ]
7
+ }
@@ -0,0 +1 @@
1
+ US
@@ -0,0 +1 @@
1
+ US,NY,Jamaica,40.6915,-73.8057
@@ -0,0 +1 @@
1
+ US,NY,Jamaica,,40.6915,-73.8057,501,718,"Road Runner","Road Runner"
@@ -0,0 +1 @@
1
+ US,"United States",NY,"New York",Jamaica,40.6915,-73.8057,501,718,America/New_York,NA,,"Road Runner","Road Runner",rr.com,"AS11351 Road Runner HoldCo LLC",Cable/DSL,residential,779,99,37,76,35
@@ -0,0 +1 @@
1
+ US,TX,Plano,75093,33.034698,-96.813400,623,972,"Layered Technologies , US","Layered Technologies , US",
@@ -0,0 +1 @@
1
+ ,,,,,,,,,,INVALID_LICENSE_KEY
@@ -0,0 +1 @@
1
+ ,,,,,,,,,,IP_NOT_FOUND
@@ -0,0 +1,72 @@
1
+ {
2
+ "Response": {
3
+ "MetaInfo": {
4
+ "Timestamp": "2013-02-08T16:26:39.382+0000"
5
+ },
6
+ "View": [
7
+ {
8
+ "_type": "SearchResultsViewType",
9
+ "ViewId": 0,
10
+ "Result": [
11
+ {
12
+ "Relevance": 1.0,
13
+ "MatchLevel": "houseNumber",
14
+ "MatchQuality": {
15
+ "State": 1.0,
16
+ "City": 1.0,
17
+ "Street": [
18
+ 1.0
19
+ ],
20
+ "HouseNumber": 1.0
21
+ },
22
+ "MatchType": "pointAddress",
23
+ "Location": {
24
+ "LocationId": "NT_ArsGdYbpo6dqjPQel9gTID_4",
25
+ "LocationType": "point",
26
+ "DisplayPosition": {
27
+ "Latitude": 40.7504692,
28
+ "Longitude": -73.9933777
29
+ },
30
+ "NavigationPosition": [
31
+ {
32
+ "Latitude": 40.7500305,
33
+ "Longitude": -73.9942398
34
+ }
35
+ ],
36
+ "MapView": {
37
+ "TopLeft": {
38
+ "Latitude": 40.7515934,
39
+ "Longitude": -73.9948616
40
+ },
41
+ "BottomRight": {
42
+ "Latitude": 40.7493451,
43
+ "Longitude": -73.9918938
44
+ }
45
+ },
46
+ "Address": {
47
+ "Label": "4 Penn Plz, New York, NY 10001, United States",
48
+ "Country": "USA",
49
+ "State": "NY",
50
+ "County": "New York",
51
+ "City": "New York",
52
+ "Street": "Penn Plz",
53
+ "HouseNumber": "4",
54
+ "PostalCode": "10001",
55
+ "AdditionalData": [
56
+ {
57
+ "value": "United States",
58
+ "key": "CountryName"
59
+ },
60
+ {
61
+ "value": "New York",
62
+ "key": "StateName"
63
+ }
64
+ ]
65
+ }
66
+ }
67
+ }
68
+ ]
69
+ }
70
+ ]
71
+ }
72
+ }
@@ -0,0 +1,8 @@
1
+ {
2
+ "Response": {
3
+ "MetaInfo": {
4
+ "Timestamp": "2013-02-08T16:41:16.723+0000"
5
+ },
6
+ "View": []
7
+ }
8
+ }
@@ -0,0 +1 @@
1
+ {"bossresponse":{"responsecode":"6000","reason":"internal error"}}
@@ -0,0 +1,2 @@
1
+ <?xml version='1.0' encoding='UTF-8'?>\n<yahoo:error xmlns:yahoo='http://yahooapis.com/v1/base.rng'\n xml:lang='en-US'>
2
+ <yahoo:description>Please provide valid credentials. OAuth oauth_problem="consumer_key_unknown", realm="yahooapis.com"</yahoo:description>\n</yahoo:error>
@@ -0,0 +1,52 @@
1
+ {
2
+ "bossresponse": {
3
+ "responsecode": "200",
4
+ "placefinder": {
5
+ "start": "0",
6
+ "count": "1",
7
+ "request": "flags=JXTSR&location=Madison%20Square%20Garden%2C%20NY%2C%20NY&%unsafe%appid=%5B%22dj0yJmk9ZmZ5NXFrNGhNcEthJmQ9WVdrOVFUSlhPV2x1TjJVbWNHbzlORE0wT0RFME9UWXkmcz1jb25zdW1lcnNlY3JldCZ4PTAy%22%2C%20%22b57b1b98eb21f171231f5b441cba505261d6c9bb%22%5D&gflags=AC&locale=en_US",
8
+ "results": [
9
+ {
10
+ "quality": "90",
11
+ "latitude": "40.750381",
12
+ "longitude": "-73.993988",
13
+ "offsetlat": "40.750381",
14
+ "offsetlon": "-73.993988",
15
+ "radius": "400",
16
+ "boundingbox": {
17
+ "north": "40.750832",
18
+ "south": "40.749931",
19
+ "east": "-73.993393",
20
+ "west": "-73.994591"
21
+ },
22
+ "name": "Madison Square Garden",
23
+ "line1": "Madison Square Garden",
24
+ "line2": "New York, NY 10001",
25
+ "line3": "",
26
+ "line4": "United States",
27
+ "cross": "",
28
+ "house": "",
29
+ "street": "",
30
+ "xstreet": "",
31
+ "unittype": "",
32
+ "unit": "",
33
+ "postal": "10001",
34
+ "neighborhood": "Garment District|Midtown|Midtown West|Manhattan",
35
+ "city": "New York",
36
+ "county": "New York County",
37
+ "state": "New York",
38
+ "country": "United States",
39
+ "countrycode": "US",
40
+ "statecode": "NY",
41
+ "countycode": "",
42
+ "timezone": "America/New_York",
43
+ "areacode": "212",
44
+ "uzip": "10001",
45
+ "hash": "",
46
+ "woeid": "23617041",
47
+ "woetype": "20"
48
+ }
49
+ ]
50
+ }
51
+ }
52
+ }
@@ -0,0 +1,10 @@
1
+ {
2
+ "bossresponse": {
3
+ "responsecode": "200",
4
+ "placefinder": {
5
+ "start": "0",
6
+ "count": "0",
7
+ "request": "flags=JXTSR&location=asdfasdf28394782sdfj2983&%unsafe%appid=%5B%22dj0yJmk9ZmZ5NXFrNGhNcEthJmQ9WVdrOVFUSlhPV2x1TjJVbWNHbzlORE0wT0RFME9UWXkmcz1jb25zdW1lcnNlY3JldCZ4PTAy%22%2C%20%22b57b1b98eb21f171231f5b441cba505261d6c9bb%22%5D&gflags=AC&locale=en_US"
8
+ }
9
+ }
10
+ }
@@ -0,0 +1,2 @@
1
+ <?xml version="1.0" encoding="utf-8"?> <yahoo:error xmlns:yahoo="http://yahooapis.com/v1/base.rng\ <http://yahooapis.com/v1/base.rng%5C>" xml:lang="en-US">
2
+ <yahoo:description>Rate Limit Exceeded</yahoo:description> <yahoo:detail>Key has exceeded its configured rate limit.</yahoo:detail> </yahoo:error>
@@ -0,0 +1,112 @@
1
+ {
2
+ "response":{
3
+ "GeoObjectCollection":{
4
+ "metaDataProperty":{
5
+ "GeocoderResponseMetaData":{
6
+ "request":"57.423359,55.892596",
7
+ "found":"3",
8
+ "results":"10",
9
+ "Point":{
10
+ "pos":"57.423359 55.892596"
11
+ }
12
+ }
13
+ },
14
+ "featureMember":[
15
+ {
16
+ "GeoObject":{
17
+ "metaDataProperty":{
18
+ "GeocoderMetaData":{
19
+ "kind":"area",
20
+ "text":"Россия, республика Башкортостан, Караидельский район",
21
+ "precision":"other",
22
+ "AddressDetails":{
23
+ "Country":{
24
+ "AddressLine":"республика Башкортостан, Караидельский район",
25
+ "CountryNameCode":"RU",
26
+ "CountryName":"Россия",
27
+ "AdministrativeArea":{
28
+ "AdministrativeAreaName":"республика Башкортостан",
29
+ "SubAdministrativeArea":{
30
+ "SubAdministrativeAreaName":"Караидельский район"
31
+ }
32
+ }
33
+ }
34
+ }
35
+ }
36
+ },
37
+ "description":"республика Башкортостан, Россия",
38
+ "name":"Караидельский район",
39
+ "boundedBy":{
40
+ "Envelope":{
41
+ "lowerCorner":"56.231384 55.462814",
42
+ "upperCorner":"57.705348 56.076117"
43
+ }
44
+ },
45
+ "Point":{
46
+ "pos":"57.423359 55.892596"
47
+ }
48
+ }
49
+ },
50
+ {
51
+ "GeoObject":{
52
+ "metaDataProperty":{
53
+ "GeocoderMetaData":{
54
+ "kind":"province",
55
+ "text":"Россия, республика Башкортостан",
56
+ "precision":"other",
57
+ "AddressDetails":{
58
+ "Country":{
59
+ "AddressLine":"республика Башкортостан",
60
+ "CountryNameCode":"RU",
61
+ "CountryName":"Россия",
62
+ "AdministrativeArea":{
63
+ "AdministrativeAreaName":"республика Башкортостан"
64
+ }
65
+ }
66
+ }
67
+ }
68
+ },
69
+ "description":"Россия",
70
+ "name":"республика Башкортостан",
71
+ "boundedBy":{
72
+ "Envelope":{
73
+ "lowerCorner":"53.157475 51.571991",
74
+ "upperCorner":"60.001577 56.533651"
75
+ }
76
+ },
77
+ "Point":{
78
+ "pos":"56.579526 54.127354"
79
+ }
80
+ }
81
+ },
82
+ {
83
+ "GeoObject":{
84
+ "metaDataProperty":{
85
+ "GeocoderMetaData":{
86
+ "kind":"country",
87
+ "text":"Россия",
88
+ "precision":"other",
89
+ "AddressDetails":{
90
+ "Country":{
91
+ "CountryNameCode":"RU",
92
+ "CountryName":"Россия"
93
+ }
94
+ }
95
+ }
96
+ },
97
+ "name":"Россия",
98
+ "boundedBy":{
99
+ "Envelope":{
100
+ "lowerCorner":"19.641673 36.84312",
101
+ "upperCorner":"179.999997 81.848739"
102
+ }
103
+ },
104
+ "Point":{
105
+ "pos":"37.617761 55.755773"
106
+ }
107
+ }
108
+ }
109
+ ]
110
+ }
111
+ }
112
+ }
data/test/https_test.rb CHANGED
@@ -4,13 +4,13 @@ require 'test_helper'
4
4
  class HttpsTest < Test::Unit::TestCase
5
5
 
6
6
  def test_uses_https_for_secure_query
7
- Geocoder::Configuration.use_https = true
7
+ Geocoder.configure(:use_https => true)
8
8
  g = Geocoder::Lookup::Google.new
9
- assert_match /^https:/, g.send(:query_url, Geocoder::Query.new("test"))
9
+ assert_match /^https:/, g.query_url(Geocoder::Query.new("test"))
10
10
  end
11
11
 
12
12
  def test_uses_http_by_default
13
13
  g = Geocoder::Lookup::Google.new
14
- assert_match /^http:/, g.send(:query_url, Geocoder::Query.new("test"))
14
+ assert_match /^http:/, g.query_url(Geocoder::Query.new("test"))
15
15
  end
16
16
  end
@@ -14,13 +14,13 @@ class SmokeTest < Test::Unit::TestCase
14
14
  end
15
15
 
16
16
  def test_simple_zip_code_search_with_ssl
17
- Geocoder::Configuration.use_https = true
17
+ Geocoder.configure(:use_https => true)
18
18
  result = Geocoder.search "27701"
19
19
  assert_not_nil (r = result.first)
20
20
  assert_equal "Durham", r.city
21
21
  assert_equal "North Carolina", r.state
22
22
  ensure
23
- Geocoder::Configuration.use_https = false
23
+ Geocoder.configure(:use_https => false)
24
24
  end
25
25
 
26
26
  end
data/test/lookup_test.rb CHANGED
@@ -3,17 +3,85 @@ require 'test_helper'
3
3
 
4
4
  class LookupTest < Test::Unit::TestCase
5
5
 
6
+ def test_responds_to_name_method
7
+ Geocoder::Lookup.all_services.each do |l|
8
+ lookup = Geocoder::Lookup.get(l)
9
+ assert lookup.respond_to?(:name),
10
+ "Lookup #{l} does not respond to #name method."
11
+ end
12
+ end
13
+
6
14
  def test_search_returns_empty_array_when_no_results
7
15
  Geocoder::Lookup.all_services_except_test.each do |l|
8
16
  lookup = Geocoder::Lookup.get(l)
17
+ set_api_key!(l)
9
18
  assert_equal [], lookup.send(:results, Geocoder::Query.new("no results")),
10
19
  "Lookup #{l} does not return empty array when no results."
11
20
  end
12
21
  end
13
22
 
23
+ def test_query_url_contains_values_in_params_hash
24
+ Geocoder::Lookup.all_services_except_test.each do |l|
25
+ next if l == :freegeoip # does not use query string
26
+ set_api_key!(l)
27
+ url = Geocoder::Lookup.get(l).query_url(Geocoder::Query.new(
28
+ "test", :params => {:one_in_the_hand => "two in the bush"}
29
+ ))
30
+ # should be "+"s for all lookups except Yahoo
31
+ assert_match /one_in_the_hand=two(%20|\+)in(%20|\+)the(%20|\+)bush/, url,
32
+ "Lookup #{l} does not appear to support arbitrary params in URL"
33
+ end
34
+ end
35
+
36
+ {
37
+ :esri => :l,
38
+ :bing => :key,
39
+ :geocoder_ca => :auth,
40
+ :google => :language,
41
+ :google_premier => :language,
42
+ :mapquest => :key,
43
+ :maxmind => :l,
44
+ :nominatim => :"accept-language",
45
+ :yahoo => :locale,
46
+ :yandex => :plng
47
+ }.each do |l,p|
48
+ define_method "test_passing_param_to_#{l}_query_overrides_configuration_value" do
49
+ set_api_key!(l)
50
+ url = Geocoder::Lookup.get(l).query_url(Geocoder::Query.new(
51
+ "test", :params => {p => "xxxx"}
52
+ ))
53
+ assert_match /#{p}=xxxx/, url,
54
+ "Param passed to #{l} lookup does not override configuration value"
55
+ end
56
+ end
57
+
58
+ def test_raises_exception_on_invalid_key
59
+ Geocoder.configure(:always_raise => [Geocoder::InvalidApiKey])
60
+ #Geocoder::Lookup.all_services_except_test.each do |l|
61
+ [:bing, :yahoo, :yandex, :maxmind].each do |l|
62
+ lookup = Geocoder::Lookup.get(l)
63
+ assert_raises Geocoder::InvalidApiKey do
64
+ lookup.send(:results, Geocoder::Query.new("invalid key"))
65
+ end
66
+ end
67
+ end
68
+
69
+ def test_returns_empty_array_on_invalid_key
70
+ # keep test output clean: suppress timeout warning
71
+ orig = $VERBOSE; $VERBOSE = nil
72
+ #Geocoder::Lookup.all_services_except_test.each do |l|
73
+ [:bing, :yahoo, :yandex, :maxmind].each do |l|
74
+ Geocoder.configure(:lookup => l)
75
+ set_api_key!(l)
76
+ assert_equal [], Geocoder.search("invalid key")
77
+ end
78
+ ensure
79
+ $VERBOSE = orig
80
+ end
81
+
14
82
  def test_does_not_choke_on_nil_address
15
83
  Geocoder::Lookup.all_services.each do |l|
16
- Geocoder::Configuration.lookup = l
84
+ Geocoder.configure(:lookup => l)
17
85
  assert_nothing_raised { Venue.new("Venue", nil).geocode }
18
86
  end
19
87
  end
@@ -24,21 +92,26 @@ class LookupTest < Test::Unit::TestCase
24
92
  end
25
93
 
26
94
  def test_google_api_key
27
- Geocoder::Configuration.api_key = "MY_KEY"
95
+ Geocoder.configure(:api_key => "MY_KEY")
28
96
  g = Geocoder::Lookup::Google.new
29
- assert_match "key=MY_KEY", g.send(:query_url, Geocoder::Query.new("Madison Square Garden, New York, NY 10001, United States"))
30
- end
31
-
32
- def test_yahoo_app_id
33
- Geocoder::Configuration.api_key = "MY_KEY"
34
- g = Geocoder::Lookup::Yahoo.new
35
- assert_match "appid=MY_KEY", g.send(:query_url, Geocoder::Query.new("Madison Square Garden, New York, NY 10001, United States"))
97
+ assert_match "key=MY_KEY", g.query_url(Geocoder::Query.new("Madison Square Garden, New York, NY 10001, United States"))
36
98
  end
37
99
 
38
100
  def test_geocoder_ca_showpostal
39
- Geocoder::Configuration.api_key = "MY_KEY"
101
+ Geocoder.configure(:api_key => "MY_KEY")
40
102
  g = Geocoder::Lookup::GeocoderCa.new
41
- assert_match "showpostal=1", g.send(:query_url, Geocoder::Query.new("Madison Square Garden, New York, NY 10001, United States"))
103
+ assert_match "showpostal=1", g.query_url(Geocoder::Query.new("Madison Square Garden, New York, NY 10001, United States"))
104
+ end
105
+
106
+ def test_raises_configuration_error_on_missing_key
107
+ assert_raises Geocoder::ConfigurationError do
108
+ Geocoder.configure(:lookup => :bing, :api_key => nil)
109
+ Geocoder.search("Madison Square Garden, New York, NY 10001, United States")
110
+ end
42
111
  end
43
112
 
113
+ def test_handle
114
+ assert_equal :google, Geocoder::Lookup::Google.new.handle
115
+ assert_equal :geocoder_ca, Geocoder::Lookup::GeocoderCa.new.handle
116
+ end
44
117
  end
data/test/near_test.rb ADDED
@@ -0,0 +1,43 @@
1
+ require 'test_helper'
2
+
3
+ class NearTest < Test::Unit::TestCase
4
+
5
+ def test_near_scope_options_without_sqlite_includes_bounding_box_condition
6
+ result = Event.send(:near_scope_options, 1.0, 2.0, 5)
7
+
8
+ assert_match /test_table_name.latitude BETWEEN 0.9276\d* AND 1.0723\d* AND test_table_name.longitude BETWEEN 1.9276\d* AND 2.0723\d* AND /,
9
+ result[:conditions][0]
10
+ end
11
+
12
+ def test_near_scope_options_with_defaults
13
+ result = Event.send(:near_scope_options, 1.0, 2.0, 5)
14
+
15
+ assert_match /AS distance/, result[:select]
16
+ assert_match /AS bearing/, result[:select]
17
+ assert_no_consecutive_comma(result[:select])
18
+ end
19
+
20
+ def test_near_scope_options_with_no_distance
21
+ result = Event.send(:near_scope_options, 1.0, 2.0, 5, :select_distance => false)
22
+
23
+ assert_no_match /AS distance/, result[:select]
24
+ assert_match /AS bearing/, result[:select]
25
+ assert_no_match /distance/, result[:condition]
26
+ assert_no_match /distance/, result[:order]
27
+ assert_no_consecutive_comma(result[:select])
28
+ end
29
+
30
+ def test_near_scope_options_with_no_bearing
31
+ result = Event.send(:near_scope_options, 1.0, 2.0, 5, :select_bearing => false)
32
+
33
+ assert_match /AS distance/, result[:select]
34
+ assert_no_match /AS bearing/, result[:select]
35
+ assert_no_consecutive_comma(result[:select])
36
+ end
37
+
38
+ private
39
+
40
+ def assert_no_consecutive_comma(string)
41
+ assert_no_match /, *,/, string, "two consecutive commas"
42
+ end
43
+ end
@@ -0,0 +1,30 @@
1
+ # encoding: utf-8
2
+ require 'test_helper'
3
+ require 'cgi'
4
+ require 'uri'
5
+
6
+ class OauthUtilTest < Test::Unit::TestCase
7
+ def test_query_string_escapes_single_quote
8
+ base_url = "http://example.com?location=d'iberville"
9
+
10
+ o = OauthUtil.new
11
+ o.consumer_key = 'consumer_key'
12
+ o.consumer_secret = 'consumer_secret'
13
+
14
+ query_string = o.sign(URI.parse(base_url)).query_string
15
+
16
+ assert_match "location=d%27iberville", query_string
17
+ end
18
+
19
+ def test_query_string_sorts_url_keys
20
+ base_url = "http://example.com?a_param=a&z_param=b&b_param=c&n_param=d"
21
+
22
+ o = OauthUtil.new
23
+ o.consumer_key = 'consumer_key'
24
+ o.consumer_secret = 'consumer_secret'
25
+
26
+ query_string = o.sign(URI.parse(base_url)).query_string
27
+
28
+ assert_match /.*a_param=.*b_param=.*n_param=.*z_param=.*/, query_string
29
+ end
30
+ end
data/test/proxy_test.rb CHANGED
@@ -4,7 +4,7 @@ require 'test_helper'
4
4
  class ProxyTest < Test::Unit::TestCase
5
5
 
6
6
  def test_uses_proxy_when_specified
7
- Geocoder::Configuration.http_proxy = 'localhost'
7
+ Geocoder.configure(:http_proxy => 'localhost')
8
8
  lookup = Geocoder::Lookup::Google.new
9
9
  assert lookup.send(:http_client).proxy_class?
10
10
  end
@@ -15,7 +15,7 @@ class ProxyTest < Test::Unit::TestCase
15
15
  end
16
16
 
17
17
  def test_exception_raised_on_bad_proxy_url
18
- Geocoder::Configuration.http_proxy = ' \\_O< Quack Quack'
18
+ Geocoder.configure(:http_proxy => ' \\_O< Quack Quack')
19
19
  assert_raise Geocoder::ConfigurationError do
20
20
  Geocoder::Lookup::Google.new.send(:http_client)
21
21
  end
data/test/query_test.rb CHANGED
@@ -10,6 +10,7 @@ class QueryTest < Test::Unit::TestCase
10
10
  assert !Geocoder::Query.new("232.65.123.94.43").ip_address?
11
11
  assert !Geocoder::Query.new("232.65.123").ip_address?
12
12
  assert !Geocoder::Query.new("::ffff:123.456.789").ip_address?
13
+ assert !Geocoder::Query.new("Test\n232.65.123.94").ip_address?
13
14
  end
14
15
 
15
16
  def test_blank_query_detection
@@ -18,17 +19,29 @@ class QueryTest < Test::Unit::TestCase
18
19
  assert Geocoder::Query.new("\t ").blank?
19
20
  assert !Geocoder::Query.new("a").blank?
20
21
  assert !Geocoder::Query.new("Москва").blank? # no ASCII characters
22
+ assert !Geocoder::Query.new("\na").blank?
23
+
24
+ assert Geocoder::Query.new(nil, :params => {}).blank?
25
+ assert !Geocoder::Query.new(nil, :params => {:woeid => 1234567}).blank?
26
+ end
27
+
28
+ def test_blank_query_detection_for_coordinates
29
+ assert Geocoder::Query.new([nil,nil]).blank?
30
+ assert Geocoder::Query.new([87,nil]).blank?
21
31
  end
22
32
 
23
33
  def test_coordinates_detection
24
34
  assert Geocoder::Query.new("51.178844,5").coordinates?
25
35
  assert Geocoder::Query.new("51.178844, -1.826189").coordinates?
26
36
  assert !Geocoder::Query.new("232.65.123").coordinates?
37
+ assert !Geocoder::Query.new("Test\n51.178844, -1.826189").coordinates?
27
38
  end
28
39
 
29
40
  def test_loopback_ip_address
30
41
  assert Geocoder::Query.new("0.0.0.0").loopback_ip_address?
31
42
  assert Geocoder::Query.new("127.0.0.1").loopback_ip_address?
32
43
  assert !Geocoder::Query.new("232.65.123.234").loopback_ip_address?
44
+ assert !Geocoder::Query.new("127 Main St.").loopback_ip_address?
45
+ assert !Geocoder::Query.new("John Doe\n127 Main St.\nAnywhere, USA").loopback_ip_address?
33
46
  end
34
47
  end