google_maps 0.3.0 → 0.3.1

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.
data/VERSION CHANGED
@@ -1 +1,2 @@
1
- 0.3.0
1
+ 0.3.1
2
+
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "google_maps"
8
- s.version = "0.3.0"
8
+ s.version = "0.3.1"
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-30"
12
+ s.date = "2013-05-22"
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 = [
@@ -37,7 +37,7 @@ Gem::Specification.new do |s|
37
37
  s.homepage = "http://github.com/appfolio/geocode"
38
38
  s.licenses = ["MIT"]
39
39
  s.require_paths = ["lib"]
40
- s.rubygems_version = "1.8.12"
40
+ s.rubygems_version = "1.8.25"
41
41
  s.summary = "Gem to interact with Google Maps v3 API"
42
42
 
43
43
  if s.respond_to? :specification_version then
@@ -30,12 +30,11 @@ module GoogleMaps
30
30
  ]
31
31
 
32
32
  ACCURATE_TYPES = [ "street_address", "premise", "subpremise" ]
33
-
34
-
35
33
 
36
- attr_accessor :formatted_address, :location_type, :latitude, :longitude, :types
34
+ attr_accessor :formatted_address, :location_type, :latitude, :longitude, :types, :address_components
37
35
 
38
36
  def initialize(json)
37
+ self.address_components = json['address_components']
39
38
  self.formatted_address = json['formatted_address']
40
39
  if geometry_json = json['geometry']
41
40
  self.location_type = ActiveSupport::StringInquirer.new(geometry_json['location_type'].downcase)
@@ -45,15 +44,38 @@ module GoogleMaps
45
44
  self.longitude = BigDecimal(geometry_json['location']['lng'].to_s)
46
45
  end
47
46
  end
48
-
49
- self.types = json['types']
47
+ self.types = json['types']
50
48
  end
51
49
 
52
-
53
50
  def street_address?
54
51
  (ACCURATE_TYPES & self.types).present?
55
52
  end
56
53
 
54
+ def method_missing(method_sym, *args, &block)
55
+ if TYPES.include?(method_sym.to_s)
56
+ define_address_component_accessor_for_type(method_sym)
57
+ send(method_sym, args.first)
58
+ else
59
+ super
60
+ end
61
+ end
62
+
63
+ def respond_to?(method_sym, include_private = false)
64
+ TYPES.include?(method_sym.to_s) || super
65
+ end
66
+
67
+ private
68
+
69
+ def define_address_component_accessor_for_type(type_symbol)
70
+ class_eval %Q{
71
+ def #{type_symbol}(arg=nil)
72
+ component = address_components.select { |ac| ac['types'].include?('#{type_symbol.to_s}') }.first
73
+ return nil if component.blank?
74
+ (arg.to_s == 'short') ? component['short_name'] : component['long_name']
75
+ end
76
+ }
77
+ end
78
+
57
79
  end
58
80
  end
59
81
  end
@@ -44,6 +44,63 @@ class GoogleMaps::GeocoderTest < ActiveSupport::TestCase
44
44
  assert manhattan_ny.location_type.approximate?
45
45
  assert_equal ["sublocality", "political"], manhattan_ny.types
46
46
  end
47
+
48
+ def test_all_encompassing_json
49
+ GoogleMaps::Geocoder.expects(:url).with(:address => "80 Leonard Street, New York, NY 10013", :ssl => false, :sensor => false).returns(:url)
50
+
51
+ RestClient.expects(:get).with(:url).returns(all_encompassing_json)
52
+ results = GoogleMaps::Geocoder.locate!("80 Leonard Street, New York, NY 10013")
53
+
54
+ assert results.status.ok?
55
+
56
+ result = results.first
57
+ assert_equal "80", result.street_number
58
+ assert_equal "Leonard Street", result.route
59
+ assert_equal "Lower Manhattan", result.neighborhood
60
+ assert_equal "Manhattan", result.sublocality
61
+ assert_equal "New York", result.locality
62
+ assert_equal "New York", result.administrative_area_level_1
63
+ assert_equal "New York", result.administrative_area_level_2
64
+ assert_equal "New York", result.administrative_area_level_3
65
+ assert_equal "United States", result.country
66
+ assert_equal "10013", result.postal_code
67
+ assert_equal "Premise Long", result.premise
68
+ assert_equal "Subpremise Long", result.subpremise
69
+ assert_equal "Pretty Mountain Long", result.natural_feature
70
+ assert_equal "La Guardia", result.airport
71
+ assert_equal "Central Park", result.park
72
+ assert_equal "Statue of Liberty", result.point_of_interest
73
+ assert_equal "272510 Long", result.post_box
74
+ assert_equal "13.5", result.floor
75
+ assert_equal nil, result.room
76
+
77
+ assert_equal "80", result.street_number(:short)
78
+ assert_equal "Leonard St", result.route(:short)
79
+ assert_equal "Lower Manhattan", result.neighborhood(:short)
80
+ assert_equal "Manhattan", result.sublocality(:short)
81
+ assert_equal "New York", result.locality(:short)
82
+ assert_equal "NY", result.administrative_area_level_1(:short)
83
+ assert_equal "New York", result.administrative_area_level_2(:short)
84
+ assert_equal "New York", result.administrative_area_level_3(:short)
85
+ assert_equal "US", result.country(:short)
86
+ assert_equal "10013", result.postal_code(:short)
87
+ assert_equal "Premise Short", result.premise(:short)
88
+ assert_equal "Subpremise Short", result.subpremise(:short)
89
+ assert_equal "Pretty Mountain Short", result.natural_feature(:short)
90
+ assert_equal "La Guardia", result.airport(:short)
91
+ assert_equal "Central Park", result.park(:short)
92
+ assert_equal "Statue of Liberty", result.point_of_interest(:short)
93
+ assert_equal "272510 Short", result.post_box(:short)
94
+ assert_equal "13.5", result.floor(:short)
95
+ assert_equal nil, result.room(:short)
96
+
97
+ assert result.respond_to?(:street_number)
98
+ assert result.respond_to?(:street_address?)
99
+
100
+ assert_raise NoMethodError do
101
+ result.this_field_is_not_present_in_address_components
102
+ end
103
+ end
47
104
 
48
105
  def test_locate__error
49
106
  error = StandardError.new("there was an error doing that")
@@ -34,4 +34,8 @@ class ActiveSupport::TestCase
34
34
  "{\n \"results\" : [\n {\n \"address_components\" : [\n {\n \"long_name\" : \"New York\",\n \"short_name\" : \"New York\",\n \"types\" : [ \"locality\", \"political\" ]\n },\n {\n \"long_name\" : \"New York\",\n \"short_name\" : \"New York\",\n \"types\" : [ \"administrative_area_level_2\", \"political\" ]\n },\n {\n \"long_name\" : \"New York\",\n \"short_name\" : \"NY\",\n \"types\" : [ \"administrative_area_level_1\", \"political\" ]\n },\n {\n \"long_name\" : \"United States\",\n \"short_name\" : \"US\",\n \"types\" : [ \"country\", \"political\" ]\n }\n ],\n \"formatted_address\" : \"New York, NY, USA\",\n \"geometry\" : {\n \"bounds\" : {\n \"northeast\" : {\n \"lat\" : 40.91524130,\n \"lng\" : -73.7002720\n },\n \"southwest\" : {\n \"lat\" : 40.4959080,\n \"lng\" : -74.25908790\n }\n },\n \"location\" : {\n \"lat\" : 40.71435280,\n \"lng\" : -74.00597309999999\n },\n \"location_type\" : \"APPROXIMATE\",\n \"viewport\" : {\n \"northeast\" : {\n \"lat\" : 40.84953420,\n \"lng\" : -73.74985430\n },\n \"southwest\" : {\n \"lat\" : 40.57889640,\n \"lng\" : -74.26209190\n }\n }\n },\n \"types\" : [ \"locality\", \"political\" ]\n },\n {\n \"address_components\" : [\n {\n \"long_name\" : \"Manhattan\",\n \"short_name\" : \"Manhattan\",\n \"types\" : [ \"sublocality\", \"political\" ]\n },\n {\n \"long_name\" : \"New York\",\n \"short_name\" : \"New York\",\n \"types\" : [ \"locality\", \"political\" ]\n },\n {\n \"long_name\" : \"New York\",\n \"short_name\" : \"New York\",\n \"types\" : [ \"administrative_area_level_2\", \"political\" ]\n },\n {\n \"long_name\" : \"New York\",\n \"short_name\" : \"NY\",\n \"types\" : [ \"administrative_area_level_1\", \"political\" ]\n },\n {\n \"long_name\" : \"United States\",\n \"short_name\" : \"US\",\n \"types\" : [ \"country\", \"political\" ]\n }\n ],\n \"formatted_address\" : \"Manhattan, New York, NY, USA\",\n \"geometry\" : {\n \"bounds\" : {\n \"northeast\" : {\n \"lat\" : 40.8822140,\n \"lng\" : -73.9070\n },\n \"southwest\" : {\n \"lat\" : 40.67954790,\n \"lng\" : -74.0472850\n }\n },\n \"location\" : {\n \"lat\" : 40.78343450,\n \"lng\" : -73.96624950\n },\n \"location_type\" : \"APPROXIMATE\",\n \"viewport\" : {\n \"northeast\" : {\n \"lat\" : 40.8200450,\n \"lng\" : -73.90331300000001\n },\n \"southwest\" : {\n \"lat\" : 40.6980780,\n \"lng\" : -74.03514899999999\n }\n }\n },\n \"types\" : [ \"sublocality\", \"political\" ]\n }\n ],\n \"status\" : \"OK\"\n}\n"
35
35
  end
36
36
 
37
+ def all_encompassing_json
38
+ "{\n \"results\" : [\n {\n \"address_components\" : [\n {\n \"long_name\" : \"80\",\n \"short_name\" : \"80\",\n \"types\" : [ \"street_number\" ]\n },\n {\n \"long_name\" : \"Leonard Street\",\n \"short_name\" : \"Leonard St\",\n \"types\" : [ \"route\" ]\n },\n {\n \"long_name\" : \"Lower Manhattan\",\n \"short_name\" : \"Lower Manhattan\",\n \"types\" : [ \"neighborhood\", \"political\" ]\n },\n {\n \"long_name\" : \"Manhattan\",\n \"short_name\" : \"Manhattan\",\n \"types\" : [ \"sublocality\", \"political\" ]\n },\n {\n \"long_name\" : \"New York\",\n \"short_name\" : \"New York\",\n \"types\" : [ \"locality\", \"political\" ]\n },\n {\n \"long_name\" : \"New York\",\n \"short_name\" : \"New York\",\n \"types\" : [ \"administrative_area_level_3\", \"political\" ]\n },\n {\n \"long_name\" : \"New York\",\n \"short_name\" : \"New York\",\n \"types\" : [ \"administrative_area_level_2\", \"political\" ]\n },\n {\n \"long_name\" : \"New York\",\n \"short_name\" : \"NY\",\n \"types\" : [ \"administrative_area_level_1\", \"political\" ]\n },\n {\n \"long_name\" : \"United States\",\n \"short_name\" : \"US\",\n \"types\" : [ \"country\", \"political\" ]\n },\n {\n \"long_name\" : \"10013\",\n \"short_name\" : \"10013\",\n \"types\" : [ \"postal_code\" ]\n },\n {\n \"long_name\" : \"Premise Long\",\n \"short_name\" : \"Premise Short\",\n \"types\" : [ \"premise\" ]\n },\n {\n \"long_name\" : \"Subpremise Long\",\n \"short_name\" : \"Subpremise Short\",\n \"types\" : [ \"subpremise\" ]\n },\n {\n \"long_name\" : \"Pretty Mountain Long\",\n \"short_name\" : \"Pretty Mountain Short\",\n \"types\" : [ \"natural_feature\" ]\n },\n {\n \"long_name\" : \"La Guardia\",\n \"short_name\" : \"La Guardia\",\n \"types\" : [ \"airport\" ]\n },\n {\n \"long_name\" : \"Central Park\",\n \"short_name\" : \"Central Park\",\n \"types\" : [ \"park\" ]\n },\n {\n \"long_name\" : \"Statue of Liberty\",\n \"short_name\" : \"Statue of Liberty\",\n \"types\" : [ \"point_of_interest\" ]\n },\n {\n \"long_name\" : \"272510 Long\",\n \"short_name\" : \"272510 Short\",\n \"types\" : [ \"post_box\" ]\n },\n {\n \"long_name\" : \"13.5\",\n \"short_name\" : \"13.5\",\n \"types\" : [ \"floor\" ]\n }\n ],\n \"formatted_address\" : \"80 Leonard Street, New York, NY 10013, USA\",\n \"geometry\" : {\n \"location\" : {\n \"lat\" : 40.71729149999999,\n \"lng\" : -74.00512060\n },\n \"location_type\" : \"ROOFTOP\",\n \"viewport\" : {\n \"northeast\" : {\n \"lat\" : 40.71864048029149,\n \"lng\" : -74.00377161970849\n },\n \"southwest\" : {\n \"lat\" : 40.71594251970850,\n \"lng\" : -74.00646958029149\n }\n }\n },\n \"postcode_localities\" : [],\n \"types\" : [ \"street_address\" ]\n }\n ],\n \"status\" : \"OK\"\n}\n"
39
+ end
40
+
37
41
  end
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: 19
4
+ hash: 17
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 3
9
- - 0
10
- version: 0.3.0
9
+ - 1
10
+ version: 0.3.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Tushar Ranka
@@ -15,10 +15,9 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-05-30 00:00:00 Z
18
+ date: 2013-05-22 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
- type: :runtime
22
21
  requirement: &id001 !ruby/object:Gem::Requirement
23
22
  none: false
24
23
  requirements:
@@ -28,11 +27,11 @@ dependencies:
28
27
  segments:
29
28
  - 0
30
29
  version: "0"
30
+ type: :runtime
31
31
  version_requirements: *id001
32
32
  name: rest-client
33
33
  prerelease: false
34
34
  - !ruby/object:Gem::Dependency
35
- type: :runtime
36
35
  requirement: &id002 !ruby/object:Gem::Requirement
37
36
  none: false
38
37
  requirements:
@@ -42,11 +41,11 @@ dependencies:
42
41
  segments:
43
42
  - 0
44
43
  version: "0"
44
+ type: :runtime
45
45
  version_requirements: *id002
46
46
  name: activesupport
47
47
  prerelease: false
48
48
  - !ruby/object:Gem::Dependency
49
- type: :development
50
49
  requirement: &id003 !ruby/object:Gem::Requirement
51
50
  none: false
52
51
  requirements:
@@ -58,11 +57,11 @@ dependencies:
58
57
  - 0
59
58
  - 0
60
59
  version: 1.0.0
60
+ type: :development
61
61
  version_requirements: *id003
62
62
  name: bundler
63
63
  prerelease: false
64
64
  - !ruby/object:Gem::Dependency
65
- type: :development
66
65
  requirement: &id004 !ruby/object:Gem::Requirement
67
66
  none: false
68
67
  requirements:
@@ -74,11 +73,11 @@ dependencies:
74
73
  - 8
75
74
  - 3
76
75
  version: 1.8.3
76
+ type: :development
77
77
  version_requirements: *id004
78
78
  name: jeweler
79
79
  prerelease: false
80
80
  - !ruby/object:Gem::Dependency
81
- type: :development
82
81
  requirement: &id005 !ruby/object:Gem::Requirement
83
82
  none: false
84
83
  requirements:
@@ -88,6 +87,7 @@ dependencies:
88
87
  segments:
89
88
  - 0
90
89
  version: "0"
90
+ type: :development
91
91
  version_requirements: *id005
92
92
  name: mocha
93
93
  prerelease: false
@@ -146,7 +146,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
146
146
  requirements: []
147
147
 
148
148
  rubyforge_project:
149
- rubygems_version: 1.8.12
149
+ rubygems_version: 1.8.25
150
150
  signing_key:
151
151
  specification_version: 3
152
152
  summary: Gem to interact with Google Maps v3 API