geocoder2 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (146) hide show
  1. data/.gitignore +5 -0
  2. data/.travis.yml +27 -0
  3. data/CHANGELOG.md +329 -0
  4. data/LICENSE +20 -0
  5. data/README.md +796 -0
  6. data/Rakefile +25 -0
  7. data/bin/geocode2 +5 -0
  8. data/examples/autoexpire_cache_dalli.rb +62 -0
  9. data/examples/autoexpire_cache_redis.rb +28 -0
  10. data/examples/cache_bypass.rb +48 -0
  11. data/gemfiles/Gemfile.mongoid-2.4.x +15 -0
  12. data/json?address=26+leonard+street%2C+Belmont&key=AIzaSyDoltU6YL8XeIQrSLFGk6ZfpKaWkPukwYQ&language=en +68 -0
  13. data/lib/generators/geocoder2/config/config_generator.rb +14 -0
  14. data/lib/generators/geocoder2/config/templates/initializer.rb +21 -0
  15. data/lib/geocoder2/cache.rb +89 -0
  16. data/lib/geocoder2/calculations.rb +389 -0
  17. data/lib/geocoder2/cli.rb +121 -0
  18. data/lib/geocoder2/configuration.rb +130 -0
  19. data/lib/geocoder2/configuration_hash.rb +11 -0
  20. data/lib/geocoder2/exceptions.rb +21 -0
  21. data/lib/geocoder2/lookup.rb +86 -0
  22. data/lib/geocoder2/lookups/baidu.rb +54 -0
  23. data/lib/geocoder2/lookups/base.rb +266 -0
  24. data/lib/geocoder2/lookups/bing.rb +47 -0
  25. data/lib/geocoder2/lookups/dstk.rb +20 -0
  26. data/lib/geocoder2/lookups/esri.rb +48 -0
  27. data/lib/geocoder2/lookups/freegeoip.rb +43 -0
  28. data/lib/geocoder2/lookups/geocoder_ca.rb +54 -0
  29. data/lib/geocoder2/lookups/geocoder_us.rb +39 -0
  30. data/lib/geocoder2/lookups/google.rb +69 -0
  31. data/lib/geocoder2/lookups/google_premier.rb +47 -0
  32. data/lib/geocoder2/lookups/mapquest.rb +59 -0
  33. data/lib/geocoder2/lookups/maxmind.rb +88 -0
  34. data/lib/geocoder2/lookups/nominatim.rb +44 -0
  35. data/lib/geocoder2/lookups/ovi.rb +62 -0
  36. data/lib/geocoder2/lookups/test.rb +44 -0
  37. data/lib/geocoder2/lookups/yahoo.rb +86 -0
  38. data/lib/geocoder2/lookups/yandex.rb +54 -0
  39. data/lib/geocoder2/models/active_record.rb +46 -0
  40. data/lib/geocoder2/models/base.rb +42 -0
  41. data/lib/geocoder2/models/mongo_base.rb +60 -0
  42. data/lib/geocoder2/models/mongo_mapper.rb +26 -0
  43. data/lib/geocoder2/models/mongoid.rb +32 -0
  44. data/lib/geocoder2/query.rb +107 -0
  45. data/lib/geocoder2/railtie.rb +26 -0
  46. data/lib/geocoder2/request.rb +23 -0
  47. data/lib/geocoder2/results/baidu.rb +79 -0
  48. data/lib/geocoder2/results/base.rb +67 -0
  49. data/lib/geocoder2/results/bing.rb +48 -0
  50. data/lib/geocoder2/results/dstk.rb +6 -0
  51. data/lib/geocoder2/results/esri.rb +51 -0
  52. data/lib/geocoder2/results/freegeoip.rb +45 -0
  53. data/lib/geocoder2/results/geocoder_ca.rb +60 -0
  54. data/lib/geocoder2/results/geocoder_us.rb +39 -0
  55. data/lib/geocoder2/results/google.rb +124 -0
  56. data/lib/geocoder2/results/google_premier.rb +6 -0
  57. data/lib/geocoder2/results/mapquest.rb +51 -0
  58. data/lib/geocoder2/results/maxmind.rb +135 -0
  59. data/lib/geocoder2/results/nominatim.rb +94 -0
  60. data/lib/geocoder2/results/ovi.rb +62 -0
  61. data/lib/geocoder2/results/test.rb +16 -0
  62. data/lib/geocoder2/results/yahoo.rb +55 -0
  63. data/lib/geocoder2/results/yandex.rb +80 -0
  64. data/lib/geocoder2/sql.rb +106 -0
  65. data/lib/geocoder2/stores/active_record.rb +272 -0
  66. data/lib/geocoder2/stores/base.rb +120 -0
  67. data/lib/geocoder2/stores/mongo_base.rb +89 -0
  68. data/lib/geocoder2/stores/mongo_mapper.rb +13 -0
  69. data/lib/geocoder2/stores/mongoid.rb +13 -0
  70. data/lib/geocoder2/version.rb +3 -0
  71. data/lib/geocoder2.rb +55 -0
  72. data/lib/hash_recursive_merge.rb +74 -0
  73. data/lib/oauth_util.rb +112 -0
  74. data/lib/tasks/geocoder2.rake +27 -0
  75. data/test/active_record_test.rb +15 -0
  76. data/test/cache_test.rb +35 -0
  77. data/test/calculations_test.rb +211 -0
  78. data/test/configuration_test.rb +78 -0
  79. data/test/custom_block_test.rb +32 -0
  80. data/test/error_handling_test.rb +43 -0
  81. data/test/fixtures/baidu_invalid_key +1 -0
  82. data/test/fixtures/baidu_no_results +1 -0
  83. data/test/fixtures/baidu_reverse +1 -0
  84. data/test/fixtures/baidu_shanghai_pearl_tower +12 -0
  85. data/test/fixtures/bing_invalid_key +1 -0
  86. data/test/fixtures/bing_madison_square_garden +40 -0
  87. data/test/fixtures/bing_no_results +16 -0
  88. data/test/fixtures/bing_reverse +42 -0
  89. data/test/fixtures/esri_madison_square_garden +59 -0
  90. data/test/fixtures/esri_no_results +8 -0
  91. data/test/fixtures/esri_reverse +21 -0
  92. data/test/fixtures/freegeoip_74_200_247_59 +12 -0
  93. data/test/fixtures/freegeoip_no_results +1 -0
  94. data/test/fixtures/geocoder_ca_madison_square_garden +1 -0
  95. data/test/fixtures/geocoder_ca_no_results +1 -0
  96. data/test/fixtures/geocoder_ca_reverse +34 -0
  97. data/test/fixtures/geocoder_us_madison_square_garden +1 -0
  98. data/test/fixtures/geocoder_us_no_results +1 -0
  99. data/test/fixtures/google_garbage +456 -0
  100. data/test/fixtures/google_madison_square_garden +57 -0
  101. data/test/fixtures/google_no_city_data +44 -0
  102. data/test/fixtures/google_no_locality +51 -0
  103. data/test/fixtures/google_no_results +4 -0
  104. data/test/fixtures/google_over_limit +4 -0
  105. data/test/fixtures/mapquest_error +16 -0
  106. data/test/fixtures/mapquest_invalid_api_key +16 -0
  107. data/test/fixtures/mapquest_invalid_request +16 -0
  108. data/test/fixtures/mapquest_madison_square_garden +52 -0
  109. data/test/fixtures/mapquest_no_results +16 -0
  110. data/test/fixtures/maxmind_24_24_24_21 +1 -0
  111. data/test/fixtures/maxmind_24_24_24_22 +1 -0
  112. data/test/fixtures/maxmind_24_24_24_23 +1 -0
  113. data/test/fixtures/maxmind_24_24_24_24 +1 -0
  114. data/test/fixtures/maxmind_74_200_247_59 +1 -0
  115. data/test/fixtures/maxmind_invalid_key +1 -0
  116. data/test/fixtures/maxmind_no_results +1 -0
  117. data/test/fixtures/nominatim_madison_square_garden +150 -0
  118. data/test/fixtures/nominatim_no_results +1 -0
  119. data/test/fixtures/ovi_madison_square_garden +72 -0
  120. data/test/fixtures/ovi_no_results +8 -0
  121. data/test/fixtures/yahoo_error +1 -0
  122. data/test/fixtures/yahoo_invalid_key +2 -0
  123. data/test/fixtures/yahoo_madison_square_garden +52 -0
  124. data/test/fixtures/yahoo_no_results +10 -0
  125. data/test/fixtures/yahoo_over_limit +2 -0
  126. data/test/fixtures/yandex_invalid_key +1 -0
  127. data/test/fixtures/yandex_kremlin +48 -0
  128. data/test/fixtures/yandex_no_city_and_town +112 -0
  129. data/test/fixtures/yandex_no_results +16 -0
  130. data/test/geocoder_test.rb +59 -0
  131. data/test/https_test.rb +16 -0
  132. data/test/integration/smoke_test.rb +26 -0
  133. data/test/lookup_test.rb +117 -0
  134. data/test/method_aliases_test.rb +25 -0
  135. data/test/mongoid_test.rb +46 -0
  136. data/test/mongoid_test_helper.rb +43 -0
  137. data/test/near_test.rb +61 -0
  138. data/test/oauth_util_test.rb +30 -0
  139. data/test/proxy_test.rb +36 -0
  140. data/test/query_test.rb +52 -0
  141. data/test/request_test.rb +29 -0
  142. data/test/result_test.rb +42 -0
  143. data/test/services_test.rb +393 -0
  144. data/test/test_helper.rb +289 -0
  145. data/test/test_mode_test.rb +59 -0
  146. metadata +213 -0
@@ -0,0 +1,26 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), *%w[ .. .. lib]))
2
+ require 'pathname'
3
+ require 'rubygems'
4
+ require 'test/unit'
5
+ require 'geocoder2'
6
+
7
+ class SmokeTest < Test::Unit::TestCase
8
+
9
+ def test_simple_zip_code_search
10
+ result = Geocoder2.search "27701"
11
+ assert_not_nil (r = result.first)
12
+ assert_equal "Durham", r.city
13
+ assert_equal "North Carolina", r.state
14
+ end
15
+
16
+ def test_simple_zip_code_search_with_ssl
17
+ Geocoder2.configure(:use_https => true)
18
+ result = Geocoder2.search "27701"
19
+ assert_not_nil (r = result.first)
20
+ assert_equal "Durham", r.city
21
+ assert_equal "North Carolina", r.state
22
+ ensure
23
+ Geocoder2.configure(:use_https => false)
24
+ end
25
+
26
+ end
@@ -0,0 +1,117 @@
1
+ # encoding: utf-8
2
+ require 'test_helper'
3
+
4
+ class LookupTest < Test::Unit::TestCase
5
+
6
+ def test_responds_to_name_method
7
+ Geocoder2::Lookup.all_services.each do |l|
8
+ lookup = Geocoder2::Lookup.get(l)
9
+ assert lookup.respond_to?(:name),
10
+ "Lookup #{l} does not respond to #name method."
11
+ end
12
+ end
13
+
14
+ def test_search_returns_empty_array_when_no_results
15
+ Geocoder2::Lookup.all_services_except_test.each do |l|
16
+ lookup = Geocoder2::Lookup.get(l)
17
+ set_api_key!(l)
18
+ assert_equal [], lookup.send(:results, Geocoder2::Query.new("no results")),
19
+ "Lookup #{l} does not return empty array when no results."
20
+ end
21
+ end
22
+
23
+ def test_query_url_contains_values_in_params_hash
24
+ Geocoder2::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 = Geocoder2::Lookup.get(l).query_url(Geocoder2::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 = Geocoder2::Lookup.get(l).query_url(Geocoder2::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
+ Geocoder2.configure(:always_raise => [Geocoder2::InvalidApiKey])
60
+ #Geocoder2::Lookup.all_services_except_test.each do |l|
61
+ [:bing, :yahoo, :yandex, :maxmind].each do |l|
62
+ lookup = Geocoder2::Lookup.get(l)
63
+ assert_raises Geocoder2::InvalidApiKey do
64
+ lookup.send(:results, Geocoder2::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
+ #Geocoder2::Lookup.all_services_except_test.each do |l|
73
+ [:bing, :yahoo, :yandex, :maxmind].each do |l|
74
+ Geocoder2.configure(:lookup => l)
75
+ set_api_key!(l)
76
+ assert_equal [], Geocoder2.search("invalid key")
77
+ end
78
+ ensure
79
+ $VERBOSE = orig
80
+ end
81
+
82
+ def test_does_not_choke_on_nil_address
83
+ Geocoder2::Lookup.all_services.each do |l|
84
+ Geocoder2.configure(:lookup => l)
85
+ assert_nothing_raised { Venue.new("Venue", nil).geocode }
86
+ end
87
+ end
88
+
89
+ def test_hash_to_query
90
+ g = Geocoder2::Lookup::Google.new
91
+ assert_equal "a=1&b=2", g.send(:hash_to_query, {:a => 1, :b => 2})
92
+ end
93
+
94
+ def test_google_api_key
95
+ Geocoder2.configure(:api_key => "MY_KEY")
96
+ g = Geocoder2::Lookup::Google.new
97
+ assert_match "key=MY_KEY", g.query_url(Geocoder2::Query.new("Madison Square Garden, New York, NY 10001, United States"))
98
+ end
99
+
100
+ def test_geocoder_ca_showpostal
101
+ Geocoder2.configure(:api_key => "MY_KEY")
102
+ g = Geocoder2::Lookup::GeocoderCa.new
103
+ assert_match "showpostal=1", g.query_url(Geocoder2::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 Geocoder2::ConfigurationError do
108
+ Geocoder2.configure(:lookup => :bing, :api_key => nil)
109
+ Geocoder2.search("Madison Square Garden, New York, NY 10001, United States")
110
+ end
111
+ end
112
+
113
+ def test_handle
114
+ assert_equal :google, Geocoder2::Lookup::Google.new.handle
115
+ assert_equal :geocoder_ca, Geocoder2::Lookup::GeocoderCa.new.handle
116
+ end
117
+ end
@@ -0,0 +1,25 @@
1
+ # encoding: utf-8
2
+ require 'test_helper'
3
+
4
+ class MethodAliasesTest < Test::Unit::TestCase
5
+
6
+ def test_distance_from_is_alias_for_distance_to
7
+ v = Venue.new(*venue_params(:msg))
8
+ v.latitude, v.longitude = [40.750354, -73.993371]
9
+ assert_equal v.distance_from([30, -94]), v.distance_to([30, -94])
10
+ end
11
+
12
+ def test_fetch_coordinates_is_alias_for_geocode
13
+ v = Venue.new(*venue_params(:msg))
14
+ coords = [40.750354, -73.993371]
15
+ assert_equal coords, v.fetch_coordinates
16
+ assert_equal coords, [v.latitude, v.longitude]
17
+ end
18
+
19
+ def test_fetch_address_is_alias_for_reverse_geocode
20
+ v = Landmark.new(*landmark_params(:msg))
21
+ address = "4 Penn Plaza, New York, NY 10001, USA"
22
+ assert_equal address, v.fetch_address
23
+ assert_equal address, v.address
24
+ end
25
+ end
@@ -0,0 +1,46 @@
1
+ # encoding: utf-8
2
+ require 'mongoid_test_helper'
3
+
4
+ class MongoidTest < Test::Unit::TestCase
5
+ def test_geocoded_check
6
+ p = Place.new(*venue_params(:msg))
7
+ p.location = [40.750354, -73.993371]
8
+ assert p.geocoded?
9
+ end
10
+
11
+ def test_distance_to_returns_float
12
+ p = Place.new(*venue_params(:msg))
13
+ p.location = [40.750354, -73.993371]
14
+ assert p.distance_to([30, -94]).is_a?(Float)
15
+ end
16
+
17
+ def test_custom_coordinate_field_near_scope
18
+ location = [40.750354, -73.993371]
19
+ p = Place.near(location)
20
+ key = Mongoid::VERSION >= "3" ? "location" : :location
21
+ assert_equal p.selector[key]['$nearSphere'], location.reverse
22
+ end
23
+
24
+ def test_model_configuration
25
+ p = Place.new(*venue_params(:msg))
26
+ p.location = [0, 0]
27
+
28
+ Place.geocoded_by :address, :coordinates => :location, :units => :km
29
+ assert_equal 111, p.distance_to([0,1]).round
30
+
31
+ Place.geocoded_by :address, :coordinates => :location, :units => :mi
32
+ assert_equal 69, p.distance_to([0,1]).round
33
+ end
34
+
35
+ def test_index_is_skipped_if_skip_option_flag
36
+ result = PlaceWithoutIndex.index_options.keys.flatten[0] == :coordinates
37
+ assert !result
38
+ end
39
+
40
+ def test_nil_radius_omits_max_distance
41
+ location = [40.750354, -73.993371]
42
+ p = Place.near(location, nil)
43
+ key = Mongoid::VERSION >= "3" ? "location" : :location
44
+ assert_equal nil, p.selector[key]['$maxDistance']
45
+ end
46
+ end
@@ -0,0 +1,43 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'test_helper'
4
+ require 'mongoid'
5
+ require 'geocoder2/models/mongoid'
6
+
7
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
8
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
9
+
10
+ if (::Mongoid::VERSION >= "3")
11
+ Mongoid.logger = Logger.new($stderr, :debug)
12
+ else
13
+ Mongoid.configure do |config|
14
+ config.logger = Logger.new($stderr, :debug)
15
+ end
16
+ end
17
+
18
+ ##
19
+ # Geocoded model.
20
+ #
21
+ class Place
22
+ include Mongoid::Document
23
+ include Geocoder2::Model::Mongoid
24
+
25
+ geocoded_by :address, :coordinates => :location
26
+ field :name
27
+ field :address
28
+ field :location, :type => Array
29
+
30
+ def initialize(name, address)
31
+ super()
32
+ write_attribute :name, name
33
+ write_attribute :address, address
34
+ end
35
+ end
36
+
37
+ class PlaceWithoutIndex
38
+ include Mongoid::Document
39
+ include Geocoder2::Model::Mongoid
40
+
41
+ field :location, :type => Array
42
+ geocoded_by :location, :skip_index => true
43
+ end
data/test/near_test.rb ADDED
@@ -0,0 +1,61 @@
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
+ def test_near_scope_options_with_custom_distance_column
39
+ result = Event.send(:near_scope_options, 1.0, 2.0, 5, :distance_column => 'calculated_distance')
40
+
41
+ assert_no_match /AS distance/, result[:select]
42
+ assert_match /AS calculated_distance/, result[:select]
43
+ assert_no_match /\bdistance\b/, result[:order]
44
+ assert_match /calculated_distance/, result[:order]
45
+ assert_no_consecutive_comma(result[:select])
46
+ end
47
+
48
+ def test_near_scope_options_with_custom_bearing_column
49
+ result = Event.send(:near_scope_options, 1.0, 2.0, 5, :bearing_column => 'calculated_bearing')
50
+
51
+ assert_no_match /AS bearing/, result[:select]
52
+ assert_match /AS calculated_bearing/, result[:select]
53
+ assert_no_consecutive_comma(result[:select])
54
+ end
55
+
56
+ private
57
+
58
+ def assert_no_consecutive_comma(string)
59
+ assert_no_match /, *,/, string, "two consecutive commas"
60
+ end
61
+ 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
@@ -0,0 +1,36 @@
1
+ # encoding: utf-8
2
+ require 'test_helper'
3
+
4
+ class ProxyTest < Test::Unit::TestCase
5
+
6
+ def test_uses_proxy_when_specified
7
+ Geocoder2.configure(:http_proxy => 'localhost')
8
+ lookup = Geocoder2::Lookup::Google.new
9
+ assert lookup.send(:http_client).proxy_class?
10
+ end
11
+
12
+ def test_doesnt_use_proxy_when_not_specified
13
+ lookup = Geocoder2::Lookup::Google.new
14
+ assert !lookup.send(:http_client).proxy_class?
15
+ end
16
+
17
+ def test_exception_raised_on_bad_proxy_url
18
+ Geocoder2.configure(:http_proxy => ' \\_O< Quack Quack')
19
+ assert_raise Geocoder2::ConfigurationError do
20
+ Geocoder2::Lookup::Google.new.send(:http_client)
21
+ end
22
+ end
23
+
24
+ def test_accepts_proxy_with_http_protocol
25
+ Geocoder2.configure(:http_proxy => 'http://localhost')
26
+ lookup = Geocoder2::Lookup::Google.new
27
+ assert lookup.send(:http_client).proxy_class?
28
+ end
29
+
30
+ def test_accepts_proxy_with_https_protocol
31
+ Geocoder2.configure(:https_proxy => 'https://localhost')
32
+ Geocoder2.configure(:use_https => true)
33
+ lookup = Geocoder2::Lookup::Google.new
34
+ assert lookup.send(:http_client).proxy_class?
35
+ end
36
+ end
@@ -0,0 +1,52 @@
1
+ # encoding: utf-8
2
+ require 'test_helper'
3
+
4
+ class QueryTest < Test::Unit::TestCase
5
+
6
+ def test_ip_address_detection
7
+ assert Geocoder2::Query.new("232.65.123.94").ip_address?
8
+ assert Geocoder2::Query.new("666.65.123.94").ip_address? # technically invalid
9
+ assert Geocoder2::Query.new("::ffff:12.34.56.78").ip_address?
10
+ assert !Geocoder2::Query.new("232.65.123.94.43").ip_address?
11
+ assert !Geocoder2::Query.new("232.65.123").ip_address?
12
+ assert !Geocoder2::Query.new("::ffff:123.456.789").ip_address?
13
+ assert !Geocoder2::Query.new("Test\n232.65.123.94").ip_address?
14
+ end
15
+
16
+ def test_blank_query_detection
17
+ assert Geocoder2::Query.new(nil).blank?
18
+ assert Geocoder2::Query.new("").blank?
19
+ assert Geocoder2::Query.new("\t ").blank?
20
+ assert !Geocoder2::Query.new("a").blank?
21
+ assert !Geocoder2::Query.new("Москва").blank? # no ASCII characters
22
+ assert !Geocoder2::Query.new("\na").blank?
23
+
24
+ assert Geocoder2::Query.new(nil, :params => {}).blank?
25
+ assert !Geocoder2::Query.new(nil, :params => {:woeid => 1234567}).blank?
26
+ end
27
+
28
+ def test_blank_query_detection_for_coordinates
29
+ assert Geocoder2::Query.new([nil,nil]).blank?
30
+ assert Geocoder2::Query.new([87,nil]).blank?
31
+ end
32
+
33
+ def test_coordinates_detection
34
+ assert Geocoder2::Query.new("51.178844,5").coordinates?
35
+ assert Geocoder2::Query.new("51.178844, -1.826189").coordinates?
36
+ assert !Geocoder2::Query.new("232.65.123").coordinates?
37
+ assert !Geocoder2::Query.new("Test\n51.178844, -1.826189").coordinates?
38
+ end
39
+
40
+ def test_loopback_ip_address
41
+ assert Geocoder2::Query.new("0.0.0.0").loopback_ip_address?
42
+ assert Geocoder2::Query.new("127.0.0.1").loopback_ip_address?
43
+ assert !Geocoder2::Query.new("232.65.123.234").loopback_ip_address?
44
+ assert !Geocoder2::Query.new("127 Main St.").loopback_ip_address?
45
+ assert !Geocoder2::Query.new("John Doe\n127 Main St.\nAnywhere, USA").loopback_ip_address?
46
+ end
47
+
48
+ def test_sanitized_text_with_array
49
+ q = Geocoder2::Query.new([43.1313,11.3131])
50
+ assert_equal "43.1313,11.3131", q.sanitized_text
51
+ end
52
+ end
@@ -0,0 +1,29 @@
1
+ # encoding: utf-8
2
+ require 'test_helper'
3
+
4
+ class RequestTest < Test::Unit::TestCase
5
+ class MockRequest
6
+ include Geocoder2::Request
7
+ attr_accessor :env, :ip
8
+ def initialize(env={}, ip="")
9
+ @env = env
10
+ @ip = ip
11
+ end
12
+ end
13
+ def test_http_x_real_ip
14
+ req = MockRequest.new({"HTTP_X_REAL_IP" => "74.200.247.59"})
15
+ assert req.location.is_a?(Geocoder2::Result::Freegeoip)
16
+ end
17
+ def test_http_x_forwarded_for_without_proxy
18
+ req = MockRequest.new({"HTTP_X_FORWARDED_FOR" => "74.200.247.59"})
19
+ assert req.location.is_a?(Geocoder2::Result::Freegeoip)
20
+ end
21
+ def test_http_x_forwarded_for_with_proxy
22
+ req = MockRequest.new({"HTTP_X_FORWARDED_FOR" => "74.200.247.59, 74.200.247.59"})
23
+ assert req.location.is_a?(Geocoder2::Result::Freegeoip)
24
+ end
25
+ def test_with_request_ip
26
+ req = MockRequest.new({}, "74.200.247.59")
27
+ assert req.location.is_a?(Geocoder2::Result::Freegeoip)
28
+ end
29
+ end
@@ -0,0 +1,42 @@
1
+ # encoding: utf-8
2
+ require 'test_helper'
3
+
4
+ class ResultTest < Test::Unit::TestCase
5
+
6
+ def test_result_has_required_attributes
7
+ Geocoder2::Lookup.all_services_except_test.each do |l|
8
+ Geocoder2.configure(:lookup => l)
9
+ set_api_key!(l)
10
+ result = Geocoder2.search([45.423733, -75.676333]).first
11
+ assert_result_has_required_attributes(result)
12
+ end
13
+ end
14
+
15
+ def test_yandex_result_without_city_does_not_raise_exception
16
+ assert_nothing_raised do
17
+ Geocoder2.configure(:lookup => :yandex)
18
+ set_api_key!(:yandex)
19
+ result = Geocoder2.search("no city and town").first
20
+ assert_equal "", result.city
21
+ end
22
+ end
23
+
24
+
25
+ private # ------------------------------------------------------------------
26
+
27
+ def assert_result_has_required_attributes(result)
28
+ m = "Lookup #{Geocoder2.config.lookup} does not support %s attribute."
29
+ assert result.coordinates.is_a?(Array), m % "coordinates"
30
+ assert result.latitude.is_a?(Float), m % "latitude"
31
+ assert result.longitude.is_a?(Float), m % "longitude"
32
+ assert result.city.is_a?(String), m % "city"
33
+ assert result.state.is_a?(String), m % "state"
34
+ assert result.state_code.is_a?(String), m % "state_code"
35
+ assert result.province.is_a?(String), m % "province"
36
+ assert result.province_code.is_a?(String), m % "province_code"
37
+ assert result.postal_code.is_a?(String), m % "postal_code"
38
+ assert result.country.is_a?(String), m % "country"
39
+ assert result.country_code.is_a?(String), m % "country_code"
40
+ assert_not_nil result.address, m % "address"
41
+ end
42
+ end