google_maps_geocoder 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source "http://rubygems.org"
2
+
3
+ group :development do
4
+ gem 'activesupport'
5
+ gem 'jeweler'
6
+ gem 'rack'
7
+ gem 'rcov'
8
+ gem 'rspec'
9
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2011 Roderick Monje
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,47 @@
1
+ = google_maps_geocoder
2
+
3
+ A simple PORO wrapper for geocoding with Google Maps.
4
+
5
+ chez_barack = GoogleMapsGeocoder.new('1600 Pennsylvania Washington')
6
+
7
+ chez_barack.formatted_address
8
+ => "1600 Pennsylvania Ave NW, Washington D.C., DC 20500, USA"
9
+
10
+ chez_barack.city
11
+ => "Washington D.C."
12
+
13
+ chez_barack.state_long_name
14
+ => "District of Columbia"
15
+
16
+ chez_barack.state_short_name
17
+ => "DC"
18
+
19
+ The complete, hopefully self-explanatory, API is:
20
+
21
+ city
22
+ country_long_name
23
+ country_short_name
24
+ county
25
+ exact_match?
26
+ formatted_address
27
+ formatted_street_address
28
+ lat
29
+ lng
30
+ partial_match?
31
+ postal_code
32
+ state_long_name
33
+ state_short_name
34
+
35
+ == Contributing to google_maps_geocoder
36
+
37
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
38
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
39
+ * Fork the project
40
+ * Start a feature/bugfix branch
41
+ * Commit and push until you are happy with your contribution
42
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
43
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
44
+
45
+ == Copyright
46
+
47
+ Copyright (c) 2011 Roderick Monje. See LICENSE.txt for further details.
data/Rakefile ADDED
@@ -0,0 +1,53 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'rake'
11
+
12
+ require 'jeweler'
13
+ Jeweler::Tasks.new do |gem|
14
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
15
+ gem.name = "google_maps_geocoder"
16
+ gem.homepage = "http://github.com/ivanoblomov/google_maps_geocoder"
17
+ gem.license = "MIT"
18
+ gem.summary = %Q{A simple PORO wrapper for geocoding with Google Maps.}
19
+ gem.description = %Q{Geocode a location without worrying about parsing Google Maps' response. GoogleMapsGeocoder wraps it in a plain-old Ruby object.}
20
+ gem.email = "rod@seologic.com"
21
+ gem.authors = ["Roderick Monje"]
22
+ # Include your dependencies below. Runtime dependencies are required when using your gem,
23
+ # and development dependencies are only needed for development (ie running rake tasks, tests, etc)
24
+ # gem.add_runtime_dependency 'jabber4r', '> 0.1'
25
+ # gem.add_development_dependency 'rspec', '> 1.2.3'
26
+ end
27
+ Jeweler::RubygemsDotOrgTasks.new
28
+
29
+ require 'rake/testtask'
30
+ Rake::TestTask.new(:test) do |test|
31
+ test.libs << 'lib' << 'test'
32
+ test.pattern = 'test/**/test_*.rb'
33
+ test.verbose = true
34
+ end
35
+
36
+ require 'rcov/rcovtask'
37
+ Rcov::RcovTask.new do |test|
38
+ test.libs << 'test'
39
+ test.pattern = 'test/**/test_*.rb'
40
+ test.verbose = true
41
+ end
42
+
43
+ task :default => :test
44
+
45
+ require 'rake/rdoctask'
46
+ Rake::RDocTask.new do |rdoc|
47
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
48
+
49
+ rdoc.rdoc_dir = 'rdoc'
50
+ rdoc.title = "google_maps_geocoder #{version}"
51
+ rdoc.rdoc_files.include('README*')
52
+ rdoc.rdoc_files.include('lib/**/*.rb')
53
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,65 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{google_maps_geocoder}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Roderick Monje"]
12
+ s.date = %q{2011-04-13}
13
+ s.description = %q{Geocode with Google Maps without worrying about JSON.}
14
+ s.email = %q{rod@seologic.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ "Gemfile",
22
+ "LICENSE.txt",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "google_maps_geocoder.gemspec",
27
+ "lib/google_maps_geocoder.rb",
28
+ "spec/lib/google_maps_geocoder_spec.rb",
29
+ "spec/spec_helper.rb"
30
+ ]
31
+ s.homepage = %q{http://github.com/ivanoblomov/google_maps_geocoder}
32
+ s.licenses = ["MIT"]
33
+ s.require_paths = ["lib"]
34
+ s.rubygems_version = %q{1.5.2}
35
+ s.summary = %q{Simple PORO wrapper for a Google-Maps geocoding call.}
36
+ s.test_files = [
37
+ "spec/lib/google_maps_geocoder_spec.rb",
38
+ "spec/spec_helper.rb"
39
+ ]
40
+
41
+ if s.respond_to? :specification_version then
42
+ s.specification_version = 3
43
+
44
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
45
+ s.add_development_dependency(%q<activesupport>, [">= 0"])
46
+ s.add_development_dependency(%q<jeweler>, [">= 0"])
47
+ s.add_development_dependency(%q<rack>, [">= 0"])
48
+ s.add_development_dependency(%q<rcov>, [">= 0"])
49
+ s.add_development_dependency(%q<rspec>, [">= 0"])
50
+ else
51
+ s.add_dependency(%q<activesupport>, [">= 0"])
52
+ s.add_dependency(%q<jeweler>, [">= 0"])
53
+ s.add_dependency(%q<rack>, [">= 0"])
54
+ s.add_dependency(%q<rcov>, [">= 0"])
55
+ s.add_dependency(%q<rspec>, [">= 0"])
56
+ end
57
+ else
58
+ s.add_dependency(%q<activesupport>, [">= 0"])
59
+ s.add_dependency(%q<jeweler>, [">= 0"])
60
+ s.add_dependency(%q<rack>, [">= 0"])
61
+ s.add_dependency(%q<rcov>, [">= 0"])
62
+ s.add_dependency(%q<rspec>, [">= 0"])
63
+ end
64
+ end
65
+
@@ -0,0 +1,112 @@
1
+ require 'active_support'
2
+ require 'logger'
3
+ require 'net/http'
4
+ require 'rack'
5
+
6
+ class GoogleMapsGeocoder
7
+ # Returns the complete formatted address with standardized abbreviations.
8
+ attr_reader :formatted_address
9
+ # Returns the formatted street address with standardized abbreviations.
10
+ attr_reader :formatted_street_address
11
+ # Self-explanatory
12
+ attr_reader :city, :country_long_name, :country_short_name, :county, :lat, :lng, :postal_code, :state_long_name, :state_short_name
13
+
14
+ # Instance Methods: Overrides ====================================================================
15
+
16
+ # Geocodes the specified address and wraps the results in a geocoder object.
17
+ #
18
+ # ==== Attributes
19
+ #
20
+ # * +address+ - a geocodable address
21
+ #
22
+ # ==== Examples
23
+ #
24
+ # white_house = GoogleMapsGeocoder.new('1600 Pennsylvania Washington')
25
+ # white_house.formatted_address
26
+ # => "1600 Pennsylvania Ave NW, Washington D.C., DC 20500, USA"
27
+ def initialize(address)
28
+ response = Net::HTTP.get_response(URI.parse("http://maps.googleapis.com/maps/api/geocode/json?address=#{Rack::Utils.escape(address)}&sensor=false"))
29
+ @json = ActiveSupport::JSON.decode(response.body)
30
+ raise "Geocoding \"#{address}\" exceeded query limit! Google returned...\n#{@json.inspect}" if @json.blank? || @json['status'] != 'OK'
31
+
32
+ logger = Logger.new(STDERR)
33
+ logger.info('GoogleMapsGeocoder') { "Geocoded \"#{address}\" and Google returned...\n#{@json.inspect}" }
34
+
35
+ @city, @country_short_name, @country_long_name, @county, @formatted_address, @formatted_street_address, @lat, @lng, @postal_code, @state_long_name, @state_short_name = parse_city, parse_country_short_name, parse_country_long_name, parse_county, parse_formatted_address, parse_formatted_street_address, parse_lat, parse_lng, parse_postal_code, parse_state_long_name, parse_state_short_name
36
+ end
37
+
38
+ # Instance Methods ===============================================================================
39
+
40
+ # Returns true if the address Google returns is an exact match.
41
+ #
42
+ # ==== Examples
43
+ #
44
+ # white_house = GoogleMapsGeocoder.new('1600 Pennsylvania Ave')
45
+ # white_house.exact_match?
46
+ # => true
47
+ def exact_match?
48
+ ! self.partial_match?
49
+ end
50
+
51
+ # Returns true if the address Google returns isn't an exact match.
52
+ #
53
+ # ==== Examples
54
+ #
55
+ # white_house = GoogleMapsGeocoder.new('1600 Pennsylvania Washington')
56
+ # white_house.exact_match?
57
+ # => false
58
+ def partial_match?
59
+ @json['results'][0]['partial_match'] == true
60
+ end
61
+
62
+ private
63
+
64
+ def parse_address_component_type(type, name='long_name')
65
+ _address_component = @json['results'][0]['address_components'].detect{ |ac| ac['types'] && ac['types'].include?(type) }
66
+ _address_component && _address_component[name]
67
+ end
68
+
69
+ def parse_city
70
+ parse_address_component_type('sublocality') || parse_address_component_type('locality')
71
+ end
72
+
73
+ def parse_country_long_name
74
+ parse_address_component_type('country')
75
+ end
76
+
77
+ def parse_country_short_name
78
+ parse_address_component_type('country', 'short_name')
79
+ end
80
+
81
+ def parse_county
82
+ parse_address_component_type('administrative_area_level_2')
83
+ end
84
+
85
+ def parse_formatted_address
86
+ @json['results'][0]['formatted_address']
87
+ end
88
+
89
+ def parse_formatted_street_address
90
+ "#{parse_address_component_type('street_number')} #{parse_address_component_type('route')}"
91
+ end
92
+
93
+ def parse_lat
94
+ @json['results'][0]['geometry']['location']['lat']
95
+ end
96
+
97
+ def parse_lng
98
+ @json['results'][0]['geometry']['location']['lng']
99
+ end
100
+
101
+ def parse_postal_code
102
+ parse_address_component_type('postal_code')
103
+ end
104
+
105
+ def parse_state_long_name
106
+ parse_address_component_type('administrative_area_level_1')
107
+ end
108
+
109
+ def parse_state_short_name
110
+ parse_address_component_type('administrative_area_level_1', 'short_name')
111
+ end
112
+ end
@@ -0,0 +1,62 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe GoogleMapsGeocoder do
4
+ before(:all) do
5
+ begin
6
+ @exact_match = GoogleMapsGeocoder.new('837 Union St Brooklyn NY')
7
+ @partial_match = GoogleMapsGeocoder.new('1600 Pennsylvania Washington')
8
+ rescue SocketError
9
+ @no_network = true
10
+ rescue RuntimeError
11
+ @query_limit = true
12
+ end
13
+ end
14
+
15
+ before(:each) do
16
+ pending 'waiting for a network connection', :if => @no_network
17
+ pending 'waiting for query limit to pass', :if => @query_limit
18
+ end
19
+
20
+ context 'with "837 Union St Brooklyn NY"' do
21
+ subject { @exact_match }
22
+ specify { should be_exact_match }
23
+
24
+ context 'address' do
25
+ specify { subject.formatted_street_address.should == '837 Union St' }
26
+ specify { subject.city.should == 'Brooklyn' }
27
+ specify { subject.county.should == 'Kings' }
28
+ specify { subject.state_long_name.should == 'New York' }
29
+ specify { subject.state_short_name.should == 'NY' }
30
+ specify { subject.postal_code.should == '11215' }
31
+ specify { subject.country_short_name.should == 'US' }
32
+ specify { subject.country_long_name.should == 'United States' }
33
+ specify { subject.formatted_address.should == '837 Union St, Brooklyn, NY 11215, USA' }
34
+ end
35
+
36
+ context 'coordinates' do
37
+ specify { subject.lat.should be_within(0.005).of(40.6748151) }
38
+ specify { subject.lng.should be_within(0.005).of(-73.9760302) }
39
+ end
40
+ end
41
+
42
+ context 'with "1600 Pennsylvania Washington"' do
43
+ subject { @partial_match }
44
+ specify { should be_partial_match }
45
+
46
+ context 'address' do
47
+ specify { subject.formatted_street_address.should == '1600 Pennsylvania Ave NW' }
48
+ specify { subject.city.should == 'Washington D.C.' }
49
+ specify { subject.state_long_name.should == 'District of Columbia' }
50
+ specify { subject.state_short_name.should == 'DC' }
51
+ specify { subject.postal_code.should == '20500' }
52
+ specify { subject.country_short_name.should == 'US' }
53
+ specify { subject.country_long_name.should == 'United States' }
54
+ specify { subject.formatted_address.should == '1600 Pennsylvania Ave NW, Washington D.C., DC 20500, USA' }
55
+ end
56
+
57
+ context 'coordinates' do
58
+ specify { subject.lat.should be_within(0.005).of(38.8976964) }
59
+ specify { subject.lng.should be_within(0.005).of(-77.0365191) }
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,13 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+
11
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
12
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
13
+ require 'google_maps_geocoder'
metadata ADDED
@@ -0,0 +1,147 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: google_maps_geocoder
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Roderick Monje
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-04-13 00:00:00 -05:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: activesupport
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :development
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: jeweler
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ hash: 3
44
+ segments:
45
+ - 0
46
+ version: "0"
47
+ type: :development
48
+ version_requirements: *id002
49
+ - !ruby/object:Gem::Dependency
50
+ name: rack
51
+ prerelease: false
52
+ requirement: &id003 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ hash: 3
58
+ segments:
59
+ - 0
60
+ version: "0"
61
+ type: :development
62
+ version_requirements: *id003
63
+ - !ruby/object:Gem::Dependency
64
+ name: rcov
65
+ prerelease: false
66
+ requirement: &id004 !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ hash: 3
72
+ segments:
73
+ - 0
74
+ version: "0"
75
+ type: :development
76
+ version_requirements: *id004
77
+ - !ruby/object:Gem::Dependency
78
+ name: rspec
79
+ prerelease: false
80
+ requirement: &id005 !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ hash: 3
86
+ segments:
87
+ - 0
88
+ version: "0"
89
+ type: :development
90
+ version_requirements: *id005
91
+ description: Geocode with Google Maps without worrying about JSON.
92
+ email: rod@seologic.com
93
+ executables: []
94
+
95
+ extensions: []
96
+
97
+ extra_rdoc_files:
98
+ - LICENSE.txt
99
+ - README.rdoc
100
+ files:
101
+ - .document
102
+ - Gemfile
103
+ - LICENSE.txt
104
+ - README.rdoc
105
+ - Rakefile
106
+ - VERSION
107
+ - google_maps_geocoder.gemspec
108
+ - lib/google_maps_geocoder.rb
109
+ - spec/lib/google_maps_geocoder_spec.rb
110
+ - spec/spec_helper.rb
111
+ has_rdoc: true
112
+ homepage: http://github.com/ivanoblomov/google_maps_geocoder
113
+ licenses:
114
+ - MIT
115
+ post_install_message:
116
+ rdoc_options: []
117
+
118
+ require_paths:
119
+ - lib
120
+ required_ruby_version: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ hash: 3
126
+ segments:
127
+ - 0
128
+ version: "0"
129
+ required_rubygems_version: !ruby/object:Gem::Requirement
130
+ none: false
131
+ requirements:
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ hash: 3
135
+ segments:
136
+ - 0
137
+ version: "0"
138
+ requirements: []
139
+
140
+ rubyforge_project:
141
+ rubygems_version: 1.5.2
142
+ signing_key:
143
+ specification_version: 3
144
+ summary: Simple PORO wrapper for a Google-Maps geocoding call.
145
+ test_files:
146
+ - spec/lib/google_maps_geocoder_spec.rb
147
+ - spec/spec_helper.rb