google_maps 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/google_maps.gemspec +3 -2
- data/lib/google_maps/geocoder/location.rb +28 -0
- data/lib/google_maps/geocoder.rb +11 -5
- data/lib/google_maps.rb +28 -3
- data/test/google_maps/geocoder_test.rb +20 -6
- data/test/google_maps_test.rb +28 -0
- data/test/test_helper.rb +3 -2
- metadata +6 -5
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.2.0
|
data/google_maps.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "google_maps"
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.2.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Tushar Ranka"]
|
12
|
-
s.date = "2012-05-
|
12
|
+
s.date = "2012-05-15"
|
13
13
|
s.description = "General Purpose Library to interact with Google Maps v3 api. Geocoder. "
|
14
14
|
s.email = "tusharranka@gmail.com"
|
15
15
|
s.extra_rdoc_files = [
|
@@ -31,6 +31,7 @@ Gem::Specification.new do |s|
|
|
31
31
|
"lib/google_maps/geocoder/location.rb",
|
32
32
|
"lib/google_maps/geocoder/result.rb",
|
33
33
|
"test/google_maps/geocoder_test.rb",
|
34
|
+
"test/google_maps_test.rb",
|
34
35
|
"test/test_helper.rb"
|
35
36
|
]
|
36
37
|
s.homepage = "http://github.com/appfolio/geocode"
|
@@ -3,7 +3,35 @@ module GoogleMaps
|
|
3
3
|
class Location
|
4
4
|
|
5
5
|
LOCATION_TYPES = [ "ROOFTOP", "RANGE_INTERPOLATED", "GEOMETRIC_CENTER", "APPROXIMATE"]
|
6
|
+
TYPES = [
|
7
|
+
"street_address", # indicates a precise street address.
|
8
|
+
"route", # indicates a named route (such as "US 101").
|
9
|
+
"intersection", # indicates a major intersection, usually of two major roads.
|
10
|
+
"political", # indicates a political entity. Usually, this type indicates a polygon of some civil administration.
|
11
|
+
"country", # indicates the national political entity, and is typically the highest order type returned by the Geocoder.
|
12
|
+
"administrative_area_level_1", # indicates a first-order civil entity below the country level. Within the United States, these administrative levels are states. Not all nations exhibit these administrative levels.
|
13
|
+
"administrative_area_level_2", # indicates a second-order civil entity below the country level. Within the United States, these administrative levels are counties. Not all nations exhibit these administrative levels.
|
14
|
+
"administrative_area_level_3", # indicates a third-order civil entity below the country level. This type indicates a minor civil division. Not all nations exhibit these administrative levels.
|
15
|
+
"colloquial_area", # indicates a commonly-used alternative name for the entity.
|
16
|
+
"locality", # indicates an incorporated city or town political entity.
|
17
|
+
"sublocality", # indicates an first-order civil entity below a locality
|
18
|
+
"neighborhood", # indicates a named neighborhood
|
19
|
+
"premise", # indicates a named location, usually a building or collection of buildings with a common name
|
20
|
+
"subpremise", # indicates a first-order entity below a named location, usually a singular building within a collection of buildings with a common name
|
21
|
+
"postal_code", # indicates a postal code as used to address postal mail within the country.
|
22
|
+
"natural_feature", # indicates a prominent natural feature.
|
23
|
+
"airport", # indicates an airport.
|
24
|
+
"park", # indicates a named park.
|
25
|
+
"point_of_interest", # indicates a named point of interest. Typically, these "POI"s are prominent local entities that don't easily fit in another category such as "Empire State Building" or "Statue of Liberty."
|
26
|
+
"post_box", # indicates a specific postal box.
|
27
|
+
"street_number", # indicates the precise street number.
|
28
|
+
"floor", # indicates the floor of a building address.
|
29
|
+
"room", # indicates the room of a building address.
|
30
|
+
]
|
31
|
+
|
6
32
|
ACCURATE_TYPES = [ "street_address", "premise", "subpremise" ]
|
33
|
+
|
34
|
+
|
7
35
|
|
8
36
|
attr_accessor :formatted_address, :location_type, :latitude, :longitude, :types
|
9
37
|
|
data/lib/google_maps/geocoder.rb
CHANGED
@@ -2,26 +2,33 @@ require 'google_maps/geocoder/result'
|
|
2
2
|
require 'google_maps/geocoder/location'
|
3
3
|
|
4
4
|
module GoogleMaps
|
5
|
+
class GeocodeFailed < StandardError
|
6
|
+
def initialize(location, original_error)
|
7
|
+
super "Failed while geocoding '#{location}': #{original_error.class.name}: #{original_error.message}"
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
5
11
|
module Geocoder
|
6
12
|
|
7
13
|
URI_BASE = "maps.googleapis.com/maps/api/geocode/json"
|
8
14
|
|
9
15
|
def self.locate!(address, options = { })
|
10
16
|
options = {
|
11
|
-
:ssl
|
17
|
+
:ssl => false,
|
12
18
|
:address => address,
|
13
|
-
:sensor
|
19
|
+
:sensor => false
|
14
20
|
}.merge(options)
|
15
21
|
|
16
22
|
json = ActiveSupport::JSON.decode RestClient.get(url(options))
|
17
|
-
|
18
23
|
Geocoder::Result.new(json)
|
24
|
+
rescue => e
|
25
|
+
raise GeocodeFailed.new(address, e)
|
19
26
|
end
|
20
27
|
|
21
28
|
def self.url(options)
|
22
29
|
ssl = options.delete(:ssl)
|
23
30
|
|
24
|
-
if !options[:clientId] && ::GoogleMaps.enterprise_account
|
31
|
+
if !options[:clientId] && ::GoogleMaps.enterprise_account?
|
25
32
|
options.merge!(::GoogleMaps.geocoder_key_name => ::GoogleMaps.key)
|
26
33
|
end
|
27
34
|
|
@@ -36,6 +43,5 @@ module GoogleMaps
|
|
36
43
|
protocol = options[:ssl] ? "https" : "http"
|
37
44
|
"#{protocol}://#{URI_BASE}"
|
38
45
|
end
|
39
|
-
|
40
46
|
end
|
41
47
|
end
|
data/lib/google_maps.rb
CHANGED
@@ -4,13 +4,38 @@ require 'active_support/all'
|
|
4
4
|
require 'google_maps/geocoder'
|
5
5
|
|
6
6
|
module GoogleMaps
|
7
|
-
|
7
|
+
@@enterprise_account = false
|
8
|
+
@@key = nil
|
9
|
+
|
10
|
+
def self.configure(&block)
|
11
|
+
block.call(self)
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.key(*args)
|
15
|
+
if args.present?
|
16
|
+
@@key = args.first
|
17
|
+
else
|
18
|
+
@@key
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.use_enterprise_account(*args)
|
23
|
+
if args.first.present? && args.first == false
|
24
|
+
@@enterprise_account = false
|
25
|
+
else
|
26
|
+
@@enterprise_account = true
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.enterprise_account?
|
31
|
+
@@enterprise_account || false
|
32
|
+
end
|
8
33
|
|
9
34
|
def self.key_name
|
10
|
-
enterprise_account ?
|
35
|
+
enterprise_account? ? :client : :key
|
11
36
|
end
|
12
37
|
|
13
38
|
def self.geocoder_key_name
|
14
|
-
|
39
|
+
:clientId if enterprise_account?
|
15
40
|
end
|
16
41
|
end
|
@@ -1,5 +1,4 @@
|
|
1
1
|
require 'test_helper'
|
2
|
-
require 'mocha'
|
3
2
|
|
4
3
|
class GoogleMaps::GeocoderTest < ActiveSupport::TestCase
|
5
4
|
|
@@ -44,8 +43,25 @@ class GoogleMaps::GeocoderTest < ActiveSupport::TestCase
|
|
44
43
|
assert !manhattan_ny.location_type.rooftop?
|
45
44
|
assert manhattan_ny.location_type.approximate?
|
46
45
|
assert_equal ["sublocality", "political"], manhattan_ny.types
|
47
|
-
|
48
46
|
end
|
47
|
+
|
48
|
+
def test_locate__error
|
49
|
+
error = StandardError.new("there was an error doing that")
|
50
|
+
|
51
|
+
GoogleMaps::Geocoder.expects(:url).returns(:url)
|
52
|
+
RestClient.expects(:get).with(:url).raises(error)
|
53
|
+
error_raised = false
|
54
|
+
begin
|
55
|
+
GoogleMaps::Geocoder.locate!("New York")
|
56
|
+
rescue => e
|
57
|
+
error_raised = true
|
58
|
+
assert e.is_a?(GoogleMaps::GeocodeFailed), e.class.name
|
59
|
+
assert_equal "Failed while geocoding 'New York': StandardError: there was an error doing that", e.message
|
60
|
+
end
|
61
|
+
|
62
|
+
assert error_raised
|
63
|
+
end
|
64
|
+
|
49
65
|
|
50
66
|
def test_url_base_path
|
51
67
|
assert_equal "http://#{GoogleMaps::Geocoder::URI_BASE}", GoogleMaps::Geocoder.uri_base_path
|
@@ -59,12 +75,10 @@ class GoogleMaps::GeocoderTest < ActiveSupport::TestCase
|
|
59
75
|
end
|
60
76
|
|
61
77
|
def test_url__with_client_id
|
62
|
-
GoogleMaps.key
|
63
|
-
GoogleMaps.
|
78
|
+
GoogleMaps.key "bar"
|
79
|
+
GoogleMaps.use_enterprise_account
|
64
80
|
assert_url_parts expected_url("address=A&sensor=false&clientId=foo"), GoogleMaps::Geocoder.url(ordered_hash(:sensor => false, :address => "A", :clientId => 'foo'))
|
65
81
|
assert_url_parts expected_url("address=A&sensor=false&clientId=bar"), GoogleMaps::Geocoder.url(ordered_hash(:sensor => false, :address => "A"))
|
66
|
-
#assert_equal expected_url("address=A&sensor=false&clientId=foo"), GoogleMaps::Geocoder.url(ordered_hash(:sensor => false, :address => "A", :clientId => 'foo'))
|
67
|
-
#
|
68
82
|
end
|
69
83
|
|
70
84
|
private
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class GoogleMaps::GeocoderTest < ActiveSupport::TestCase
|
4
|
+
def test_configure__free_key
|
5
|
+
GoogleMaps.configure do |maps|
|
6
|
+
maps.key "foobar"
|
7
|
+
end
|
8
|
+
|
9
|
+
assert_equal "foobar", GoogleMaps.key
|
10
|
+
assert_equal false, GoogleMaps.enterprise_account?
|
11
|
+
assert_equal :key, GoogleMaps.key_name
|
12
|
+
assert_equal nil, GoogleMaps.geocoder_key_name
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_configure__enterprise_key
|
16
|
+
GoogleMaps.configure do |maps|
|
17
|
+
maps.key "foobarenterprise"
|
18
|
+
maps.use_enterprise_account
|
19
|
+
end
|
20
|
+
|
21
|
+
assert_equal "foobarenterprise", GoogleMaps.key
|
22
|
+
assert GoogleMaps.enterprise_account?
|
23
|
+
assert_equal :client, GoogleMaps.key_name
|
24
|
+
assert_equal :clientId, GoogleMaps.geocoder_key_name
|
25
|
+
end
|
26
|
+
|
27
|
+
|
28
|
+
end
|
data/test/test_helper.rb
CHANGED
@@ -15,14 +15,15 @@ require 'test/unit'
|
|
15
15
|
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
16
16
|
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
17
17
|
require 'google_maps'
|
18
|
+
require 'mocha'
|
18
19
|
|
19
20
|
class ActiveSupport::TestCase
|
20
21
|
|
21
22
|
setup :clear_configuration
|
22
23
|
|
23
24
|
def clear_configuration
|
24
|
-
GoogleMaps.
|
25
|
-
GoogleMaps.
|
25
|
+
GoogleMaps.send :class_variable_set, :@@key, nil
|
26
|
+
GoogleMaps.send :class_variable_set, :@@enterprise_account, false
|
26
27
|
end
|
27
28
|
|
28
29
|
def castilian_json
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: google_maps
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 23
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
-
|
10
|
-
version: 0.
|
8
|
+
- 2
|
9
|
+
- 0
|
10
|
+
version: 0.2.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Tushar Ranka
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2012-05-
|
18
|
+
date: 2012-05-15 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
type: :runtime
|
@@ -115,6 +115,7 @@ files:
|
|
115
115
|
- lib/google_maps/geocoder/location.rb
|
116
116
|
- lib/google_maps/geocoder/result.rb
|
117
117
|
- test/google_maps/geocoder_test.rb
|
118
|
+
- test/google_maps_test.rb
|
118
119
|
- test/test_helper.rb
|
119
120
|
homepage: http://github.com/appfolio/geocode
|
120
121
|
licenses:
|