simple_geocoder 0.0.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/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Ying Tsen Hong
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,17 @@
1
+ = simple_geocoder
2
+
3
+ Simple interface to the Google Geocoding Service API V3 http://code.google.com/apis/maps/documentation/geocoding/
4
+
5
+ * API configuration in config/api.yml
6
+ * returns results in parsed JSON format
7
+
8
+ == Examples
9
+ require 'simple_geocoder'
10
+ result = SimpleGeocoder::Geocoder.new.geocode('2000 28th St, Boulder, CO')
11
+ puts result['results'][0]['geometry']['location'] # => {"lat"=> 40.0185510,"lng"=> -105.2582644}
12
+
13
+ == Requirements
14
+ * JSON http://flori.github.com/json/doc/index.html
15
+
16
+ == Copyright
17
+ Copyright (c) 2010 Ying Tsen Hong. See LICENSE for details
data/config/api.yml ADDED
@@ -0,0 +1,6 @@
1
+ google_v3:
2
+ # output: json # only JSON output format supported
3
+ sensor: false
4
+ region: us
5
+ url_root: "http://maps.google.com/maps/api/geocode"
6
+
@@ -0,0 +1,15 @@
1
+ module SimpleGeocoder
2
+ # contains the Net::HTTP response object accessible via the {#response} method.
3
+ class ResponseError < StandardError
4
+ # response of the last request
5
+ # @return [Net::HTTPResponse] A subclass of Net::HTTPResponse, e.g.
6
+ # Net::HTTPOK
7
+ attr_reader :response
8
+
9
+ # Instantiate an instance of ResponseError with a Net::HTTPResponse object
10
+ # @param [Net::HTTPResponse]
11
+ def initialize(response)
12
+ @response = response
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,60 @@
1
+ require 'net/http'
2
+ require 'cgi'
3
+ require 'timeout'
4
+ require 'json'
5
+
6
+ # Geocoding is a time and resource intensive task.
7
+ # Whenever possible, pre-geocode known addresses
8
+ # (using the Geocoding API described here or another geocoding service),
9
+ # and store your results in a temporary cache of your own design. (from Google Geocoding API docs)
10
+ module SimpleGeocoder
11
+ class Geocoder
12
+ @@api_config = YAML.load_file(File.join(File.dirname(__FILE__), '../../config/api.yml'))
13
+ def initialize
14
+
15
+ end
16
+
17
+ # swallow exceptions and return nil on error
18
+ def geocode(address)
19
+ geocode!(address)
20
+ rescue ResponseError
21
+ nil
22
+ end
23
+
24
+ # raise ResponseError exception on error
25
+ def geocode!(address)
26
+ response = call_geocoder_service(address)
27
+ if response.is_a?(Net::HTTPOK)
28
+ return JSON.parse response.body
29
+ else
30
+ raise ResponseError.new response
31
+ end
32
+ end
33
+
34
+ # if geocoding fails, then look for lat,lng string in address
35
+ def find_location(address)
36
+ result = geocode(address)
37
+ if result['status'] == 'OK'
38
+ return result['results'][0]['geometry']['location']
39
+ else
40
+ latlon_regexp = /(-?([1-8]?[0-9]\.{1}\d{1,6}|90\.{1}0{1,6})),(-?((([1]?[0-7][0-9]|[1-9]?[0-9])\.{1}\d{1,6})|[1]?[1-8][0]\.{1}0{1,6}))/
41
+ if address =~ latlon_regexp
42
+ location = $&.split(',').map {|e| e.to_f}
43
+ return { "lat" => location[0], "lng" => location[1] }
44
+ else
45
+ return nil
46
+ end
47
+ end
48
+ end
49
+
50
+ private
51
+ def call_geocoder_service(address)
52
+ format = 'json'
53
+ address = CGI.escape address
54
+ parameters = "address=#{address}&region=#{@@api_config['google_v3']['region']}&sensor=#{@@api_config['google_v3']['sensor']}"
55
+ url = "#{@@api_config['google_v3']['url_root']}/#{format}?#{parameters}"
56
+
57
+ Net::HTTP.get_response(URI.parse(url))
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,3 @@
1
+ module SimpleGeocoder
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,3 @@
1
+ require 'simple_geocoder/version'
2
+ require 'simple_geocoder/geocoder'
3
+ require 'simple_geocoder/exceptions'
@@ -0,0 +1,27 @@
1
+ require File.expand_path("../lib/simple_geocoder/version", __FILE__)
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = 'simple_geocoder'
5
+ spec.summary = 'Simple interface to Google Geocoding API V3'
6
+ spec.version = ::SimpleGeocoder::VERSION
7
+ spec.platform = Gem::Platform::RUBY
8
+ spec.email = 'tsenying gmail'
9
+ spec.authors = ['Ying Tsen Hong']
10
+ spec.homepage = 'http://github.com/tsenying/simple_geocoder'
11
+
12
+ # silence a gem build warning.
13
+ spec.rubyforge_project = 'N/A'
14
+
15
+ spec.extra_rdoc_files = ['MIT-LICENSE', 'README.rdoc']
16
+
17
+ # everything except the git files
18
+ spec.files = Dir['**/*'].reject{ |f| f.include?('.git') }
19
+ # spec.files += ['.gitignore']
20
+ spec.test_files = Dir['test/*.rb']
21
+
22
+ spec.description = %q{Simple Geocoder}
23
+ # spec.require_paths = ["lib"] # default
24
+ spec.has_rdoc = "true"
25
+
26
+ spec.add_dependency('json', '~>1.1.9')
27
+ end
@@ -0,0 +1,17 @@
1
+ require "spec_helper"
2
+
3
+ describe SimpleGeocoder::Geocoder do
4
+ it "can geocode" do
5
+ address = '2000 28th St, Boulder, CO'
6
+ result = SimpleGeocoder::Geocoder.new.geocode(address)
7
+ result['status'].should == "OK" # at least one geocode returned
8
+ result['results'][0]['geometry']['location'].should == {"lat"=> 40.0185510,"lng"=> -105.2582644}
9
+ end
10
+
11
+ it "can find lat/lng in string" do
12
+ address = "ÜT: 34.044817,-118.311893"
13
+ result = SimpleGeocoder::Geocoder.new.find_location(address)
14
+ result.should_not be_nil
15
+ result.should == {"lat"=> 34.044817,"lng"=> -118.311893}
16
+ end
17
+ end
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
4
+ require 'simple_geocoder'
5
+ require 'spec'
6
+ require 'spec/autorun'
7
+
8
+ Spec::Runner.configure do |config|
9
+
10
+ end
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple_geocoder
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Ying Tsen Hong
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-10-25 00:00:00 -06:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: json
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ hash: 1
30
+ segments:
31
+ - 1
32
+ - 1
33
+ - 9
34
+ version: 1.1.9
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ description: Simple Geocoder
38
+ email: tsenying gmail
39
+ executables: []
40
+
41
+ extensions: []
42
+
43
+ extra_rdoc_files:
44
+ - MIT-LICENSE
45
+ - README.rdoc
46
+ files:
47
+ - config/api.yml
48
+ - lib/simple_geocoder/exceptions.rb
49
+ - lib/simple_geocoder/geocoder.rb
50
+ - lib/simple_geocoder/version.rb
51
+ - lib/simple_geocoder.rb
52
+ - MIT-LICENSE
53
+ - README.rdoc
54
+ - simple_geocoder-0.0.1.gem
55
+ - simple_geocoder.gemspec
56
+ - spec/geocoder_spec.rb
57
+ - spec/spec_helper.rb
58
+ has_rdoc: true
59
+ homepage: http://github.com/tsenying/simple_geocoder
60
+ licenses: []
61
+
62
+ post_install_message:
63
+ rdoc_options: []
64
+
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ hash: 3
73
+ segments:
74
+ - 0
75
+ version: "0"
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ none: false
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ hash: 3
82
+ segments:
83
+ - 0
84
+ version: "0"
85
+ requirements: []
86
+
87
+ rubyforge_project: N/A
88
+ rubygems_version: 1.3.7
89
+ signing_key:
90
+ specification_version: 3
91
+ summary: Simple interface to Google Geocoding API V3
92
+ test_files: []
93
+