geocoder 1.0.1 → 1.0.2

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.

@@ -0,0 +1,20 @@
1
+ # encoding: utf-8
2
+ require 'test_helper'
3
+
4
+ class HttpsTest < Test::Unit::TestCase
5
+
6
+ def setup
7
+ Geocoder::Configuration.set_defaults
8
+ end
9
+
10
+ def test_uses_https_for_secure_query
11
+ Geocoder::Configuration.use_https = true
12
+ g = Geocoder::Lookup::Google.new
13
+ assert_match /^https:/, g.send(:query_url, {:a => 1, :b => 2})
14
+ end
15
+
16
+ def test_uses_http_by_default
17
+ g = Geocoder::Lookup::Google.new
18
+ assert_match /^http:/, g.send(:query_url, {:a => 1, :b => 2})
19
+ end
20
+ end
@@ -0,0 +1,41 @@
1
+ # encoding: utf-8
2
+ require 'test_helper'
3
+
4
+ class InputHandlingTest < Test::Unit::TestCase
5
+
6
+ def test_ip_address_detection
7
+ assert Geocoder.send(:ip_address?, "232.65.123.94")
8
+ assert Geocoder.send(:ip_address?, "666.65.123.94") # technically invalid
9
+ assert !Geocoder.send(:ip_address?, "232.65.123.94.43")
10
+ assert !Geocoder.send(:ip_address?, "232.65.123")
11
+ end
12
+
13
+ def test_blank_query_detection
14
+ assert Geocoder.send(:blank_query?, nil)
15
+ assert Geocoder.send(:blank_query?, "")
16
+ assert Geocoder.send(:blank_query?, "\t ")
17
+ assert !Geocoder.send(:blank_query?, "a")
18
+ assert !Geocoder.send(:blank_query?, "Москва") # no ASCII characters
19
+ end
20
+
21
+ def test_coordinates_detection
22
+ lookup = Geocoder::Lookup::Google.new
23
+ assert lookup.send(:coordinates?, "51.178844,5")
24
+ assert lookup.send(:coordinates?, "51.178844, -1.826189")
25
+ assert !lookup.send(:coordinates?, "232.65.123")
26
+ end
27
+
28
+ def test_does_not_choke_on_nil_address
29
+ all_lookups.each do |l|
30
+ Geocoder::Configuration.lookup = l
31
+ assert_nothing_raised { Venue.new("Venue", nil).geocode }
32
+ end
33
+ end
34
+
35
+ def test_extract_coordinates
36
+ coords = [-23,47]
37
+ l = Landmark.new("Madagascar", coords[0], coords[1])
38
+ assert_equal coords, Geocoder::Calculations.extract_coordinates(l)
39
+ assert_equal coords, Geocoder::Calculations.extract_coordinates(coords)
40
+ end
41
+ end
@@ -0,0 +1,30 @@
1
+ # encoding: utf-8
2
+ require 'test_helper'
3
+
4
+ class LookupTest < Test::Unit::TestCase
5
+
6
+ def test_search_returns_empty_array_when_no_results
7
+ street_lookups.each do |l|
8
+ Geocoder::Configuration.lookup = l
9
+ assert_equal [], Geocoder.search("no results"),
10
+ "Lookup #{l} does not return empty array when no results."
11
+ end
12
+ end
13
+
14
+ def test_hash_to_query
15
+ g = Geocoder::Lookup::Google.new
16
+ assert_equal "a=1&b=2", g.send(:hash_to_query, {:a => 1, :b => 2})
17
+ end
18
+
19
+ def test_google_api_key
20
+ Geocoder::Configuration.api_key = "MY_KEY"
21
+ g = Geocoder::Lookup::Google.new
22
+ assert_match "key=MY_KEY", g.send(:query_url, "Madison Square Garden, New York, NY 10001, United States")
23
+ end
24
+
25
+ def test_yahoo_app_id
26
+ Geocoder::Configuration.api_key = "MY_KEY"
27
+ g = Geocoder::Lookup::Yahoo.new
28
+ assert_match "appid=MY_KEY", g.send(:query_url, "Madison Square Garden, New York, NY 10001, United States")
29
+ end
30
+ end
@@ -0,0 +1,29 @@
1
+ # encoding: utf-8
2
+ require 'test_helper'
3
+
4
+ class MethodAliasesTest < Test::Unit::TestCase
5
+
6
+ def setup
7
+ Geocoder::Configuration.set_defaults
8
+ end
9
+
10
+ def test_distance_from_is_alias_for_distance_to
11
+ v = Venue.new(*venue_params(:msg))
12
+ v.latitude, v.longitude = [40.750354, -73.993371]
13
+ assert_equal v.distance_from([30, -94]), v.distance_to([30, -94])
14
+ end
15
+
16
+ def test_fetch_coordinates_is_alias_for_geocode
17
+ v = Venue.new(*venue_params(:msg))
18
+ coords = [40.750354, -73.993371]
19
+ assert_equal coords, v.fetch_coordinates
20
+ assert_equal coords, [v.latitude, v.longitude]
21
+ end
22
+
23
+ def test_fetch_address_is_alias_for_reverse_geocode
24
+ v = Landmark.new(*landmark_params(:msg))
25
+ address = "4 Penn Plaza, New York, NY 10001, USA"
26
+ assert_equal address, v.fetch_address
27
+ assert_equal address, v.address
28
+ end
29
+ end
@@ -0,0 +1,35 @@
1
+ # encoding: utf-8
2
+ require 'test_helper'
3
+
4
+ begin
5
+ require 'mongoid'
6
+ require 'mongoid_test_helper'
7
+
8
+ class MongoidTest < Test::Unit::TestCase
9
+
10
+ def setup
11
+ Geocoder::Configuration.set_defaults
12
+ end
13
+
14
+ def test_geocoded_check
15
+ p = Place.new(*venue_params(:msg))
16
+ p.location = [40.750354, -73.993371]
17
+ assert p.geocoded?
18
+ end
19
+
20
+ def test_distance_to_returns_float
21
+ p = Place.new(*venue_params(:msg))
22
+ p.location = [40.750354, -73.993371]
23
+ assert p.distance_to([30, -94]).is_a?(Float)
24
+ end
25
+
26
+ def test_custom_coordinate_field_near_scope
27
+ location = [40.750354, -73.993371]
28
+ p = Place.near(location)
29
+ assert_equal p.selector[:location]['$nearSphere'], location.reverse
30
+ end
31
+ end
32
+
33
+ rescue LoadError => crash
34
+ warn 'Mongoid not installed, not tested.'
35
+ end
@@ -0,0 +1,28 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+
4
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+
7
+ Mongoid.configure do |config|
8
+ config.logger = Logger.new($stderr, :debug)
9
+ end
10
+
11
+ ##
12
+ # Geocoded model.
13
+ #
14
+ class Place
15
+ include Mongoid::Document
16
+ include Geocoder::Model::Mongoid
17
+
18
+ geocoded_by :address, :coordinates => :location
19
+ field :name
20
+ field :address
21
+ field :location, :type => Array
22
+
23
+ def initialize(name, address)
24
+ super()
25
+ write_attribute :name, name
26
+ write_attribute :address, address
27
+ end
28
+ end
@@ -0,0 +1,27 @@
1
+ # encoding: utf-8
2
+ require 'test_helper'
3
+
4
+ class ProxyTest < Test::Unit::TestCase
5
+
6
+ def setup
7
+ Geocoder::Configuration.set_defaults
8
+ end
9
+
10
+ def test_uses_proxy_when_specified
11
+ Geocoder::Configuration.http_proxy = 'localhost'
12
+ lookup = Geocoder::Lookup::Google.new
13
+ assert lookup.send(:http_client).proxy_class?
14
+ end
15
+
16
+ def test_doesnt_use_proxy_when_not_specified
17
+ lookup = Geocoder::Lookup::Google.new
18
+ assert !lookup.send(:http_client).proxy_class?
19
+ end
20
+
21
+ def test_exception_raised_on_bad_proxy_url
22
+ Geocoder::Configuration.http_proxy = ' \\_O< Quack Quack'
23
+ assert_raise Geocoder::ConfigurationError do
24
+ Geocoder::Lookup::Google.new.send(:http_client)
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,32 @@
1
+ # encoding: utf-8
2
+ require 'test_helper'
3
+
4
+ class ResultTest < Test::Unit::TestCase
5
+
6
+ def test_result_has_required_attributes
7
+ all_lookups.each do |l|
8
+ Geocoder::Configuration.lookup = l
9
+ result = Geocoder.search([45.423733, -75.676333]).first
10
+ assert_result_has_required_attributes(result)
11
+ end
12
+ end
13
+
14
+
15
+ private # ------------------------------------------------------------------
16
+
17
+ def assert_result_has_required_attributes(result)
18
+ m = "Lookup #{Geocoder::Configuration.lookup} does not support %s attribute."
19
+ assert result.coordinates.is_a?(Array), m % "coordinates"
20
+ assert result.latitude.is_a?(Float), m % "latitude"
21
+ assert result.longitude.is_a?(Float), m % "longitude"
22
+ assert result.city.is_a?(String), m % "city"
23
+ assert result.state.is_a?(String), m % "state"
24
+ assert result.state_code.is_a?(String), m % "state_code"
25
+ assert result.province.is_a?(String), m % "province"
26
+ assert result.province_code.is_a?(String), m % "province_code"
27
+ assert result.postal_code.is_a?(String), m % "postal_code"
28
+ assert result.country.is_a?(String), m % "country"
29
+ assert result.country_code.is_a?(String), m % "country_code"
30
+ assert_not_nil result.address, m % "address"
31
+ end
32
+ end
@@ -0,0 +1,95 @@
1
+ # encoding: utf-8
2
+ require 'test_helper'
3
+
4
+ class ServicesTest < Test::Unit::TestCase
5
+
6
+ def setup
7
+ Geocoder::Configuration.set_defaults
8
+ end
9
+
10
+
11
+ # --- Google ---
12
+
13
+ def test_google_result_components
14
+ result = Geocoder.search("Madison Square Garden, New York, NY").first
15
+ assert_equal "Manhattan",
16
+ result.address_components_of_type(:sublocality).first['long_name']
17
+ end
18
+
19
+ def test_google_returns_city_when_no_locality_in_result
20
+ result = Geocoder.search("no locality").first
21
+ assert_equal "Haram", result.city
22
+ end
23
+
24
+ def test_google_city_results_returns_nil_if_no_matching_component_types
25
+ result = Geocoder.search("no city data").first
26
+ assert_equal nil, result.city
27
+ end
28
+
29
+
30
+ # --- Yahoo ---
31
+
32
+ def test_yahoo_result_components
33
+ Geocoder::Configuration.lookup = :yahoo
34
+ result = Geocoder.search("Madison Square Garden, New York, NY").first
35
+ assert_equal "10001", result.postal_code
36
+ end
37
+
38
+ def test_yahoo_address_formatting
39
+ Geocoder::Configuration.lookup = :yahoo
40
+ result = Geocoder.search("Madison Square Garden, New York, NY").first
41
+ assert_equal "Madison Square Garden, New York, NY 10001, United States",
42
+ result.address
43
+ end
44
+
45
+
46
+ # --- Yandex ---
47
+
48
+ def test_yandex_with_invalid_key
49
+ # keep test output clean: suppress timeout warning
50
+ orig = $VERBOSE; $VERBOSE = nil
51
+ Geocoder::Configuration.lookup = :yandex
52
+ assert_equal [], Geocoder.search("invalid key")
53
+ $VERBOSE = orig
54
+ end
55
+
56
+
57
+ # --- Geocoder.ca ---
58
+
59
+ def test_geocoder_ca_result_components
60
+ Geocoder::Configuration.lookup = :geocoder_ca
61
+ result = Geocoder.search([45.423733, -75.676333]).first
62
+ assert_equal "CA", result.country_code
63
+ assert_equal "289 Somerset ST E, Ottawa, ON K1N6W1, Canada", result.address
64
+ end
65
+
66
+
67
+ # --- FreeGeoIp ---
68
+
69
+ def test_freegeoip_result_on_ip_address_search
70
+ result = Geocoder.search("74.200.247.59").first
71
+ assert result.is_a?(Geocoder::Result::Freegeoip)
72
+ end
73
+
74
+ def test_freegeoip_result_components
75
+ result = Geocoder.search("74.200.247.59").first
76
+ assert_equal "Plano, TX 75093, United States", result.address
77
+ end
78
+
79
+
80
+ # --- Bing ---
81
+
82
+ def test_bing_result_components
83
+ Geocoder::Configuration.lookup = :bing
84
+ result = Geocoder.search("Madison Square Garden, New York, NY").first
85
+ assert_equal "Madison Square Garden, NY", result.address
86
+ assert_equal "NY", result.state
87
+ assert_equal "New York", result.city
88
+ end
89
+
90
+ def test_bing_no_results
91
+ Geocoder::Configuration.lookup = :bing
92
+ results = Geocoder.search("no results")
93
+ assert_equal 0, results.length
94
+ end
95
+ end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: geocoder
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 1.0.1
5
+ version: 1.0.2
6
6
  platform: ruby
7
7
  authors:
8
8
  - Alex Reisner
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-05-17 00:00:00 -04:00
13
+ date: 2011-06-25 00:00:00 -04:00
14
14
  default_executable:
15
15
  dependencies: []
16
16
 
@@ -44,6 +44,8 @@ files:
44
44
  - lib/geocoder/lookups/yandex.rb
45
45
  - lib/geocoder/models/active_record.rb
46
46
  - lib/geocoder/models/base.rb
47
+ - lib/geocoder/models/mongo_base.rb
48
+ - lib/geocoder/models/mongo_mapper.rb
47
49
  - lib/geocoder/models/mongoid.rb
48
50
  - lib/geocoder/railtie.rb
49
51
  - lib/geocoder/request.rb
@@ -56,9 +58,15 @@ files:
56
58
  - lib/geocoder/results/yandex.rb
57
59
  - lib/geocoder/stores/active_record.rb
58
60
  - lib/geocoder/stores/base.rb
61
+ - lib/geocoder/stores/mongo_base.rb
62
+ - lib/geocoder/stores/mongo_mapper.rb
59
63
  - lib/geocoder/stores/mongoid.rb
60
64
  - lib/geocoder/version.rb
61
65
  - lib/tasks/geocoder.rake
66
+ - test/calculations_test.rb
67
+ - test/configuration_test.rb
68
+ - test/custom_block_test.rb
69
+ - test/error_handling_test.rb
62
70
  - test/fixtures/bing_madison_square_garden.json
63
71
  - test/fixtures/bing_no_results.json
64
72
  - test/fixtures/bing_reverse.json
@@ -78,6 +86,15 @@ files:
78
86
  - test/fixtures/yandex_kremlin.json
79
87
  - test/fixtures/yandex_no_results.json
80
88
  - test/geocoder_test.rb
89
+ - test/https_test.rb
90
+ - test/input_handling_test.rb
91
+ - test/lookup_test.rb
92
+ - test/method_aliases_test.rb
93
+ - test/mongoid_test.rb
94
+ - test/mongoid_test_helper.rb
95
+ - test/proxy_test.rb
96
+ - test/result_test.rb
97
+ - test/services_test.rb
81
98
  - test/test_helper.rb
82
99
  has_rdoc: true
83
100
  homepage: http://www.rubygeocoder.com
@@ -103,7 +120,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
103
120
  requirements: []
104
121
 
105
122
  rubyforge_project:
106
- rubygems_version: 1.5.1
123
+ rubygems_version: 1.6.1
107
124
  signing_key:
108
125
  specification_version: 3
109
126
  summary: Complete geocoding solution for Ruby.