simple_geocoder 0.0.2 → 0.0.3

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/README.rdoc CHANGED
@@ -1,17 +1,31 @@
1
1
  = simple_geocoder
2
2
 
3
- Simple interface to the Google Geocoding Service API V3 http://code.google.com/apis/maps/documentation/geocoding/
3
+ Simple Ruby interface to the Google Geocoding Service API V3 http://code.google.com/apis/maps/documentation/geocoding/
4
4
 
5
- * API configuration in config/api.yml
6
5
  * returns results in parsed JSON format
6
+ * API configuration in config/api.yml
7
7
 
8
8
  == Examples
9
+ require 'rubygems'
9
10
  require 'simple_geocoder'
10
11
  result = SimpleGeocoder::Geocoder.new.geocode('2000 28th St, Boulder, CO')
11
- puts result['results'][0]['geometry']['location'] # => {"lat"=> 40.0185510,"lng"=> -105.2582644}
12
+ puts result['results'][0]['geometry']['location'].inspect # => {"lat"=> 40.0185510,"lng"=> -105.2582644}
13
+
14
+ Accepts optional parameters as a hash, keyed by parameter name symbols.
15
+ Optional parameters include: bounds, region, language, sensor.
16
+ Default *region* is 'us'.
17
+ Default *sensor* value is 'false'
18
+
19
+ Example for region parameter:
20
+ result = SimpleGeocoder::Geocoder.new.geocode("Toledo",{:region=>'es'})
21
+ returns location for Toledo, Spain.
22
+
23
+ Example for bounds (viewport biasing):
24
+ result = SimpleGeocoder::Geocoder.new.geocode("Winnetka", { :bounds => "34.172684,-118.604794|34.236144,-118.500938" } )
25
+ Returns location for Winnetka in California instead of Illinois.
12
26
 
13
27
  == Requirements
14
28
  * JSON http://flori.github.com/json/doc/index.html
15
29
 
16
30
  == Copyright
17
- Copyright (c) 2010 Ying Tsen Hong. See LICENSE for details
31
+ Copyright (c) 2010, 2011, 2012 Ying Tsen Hong. See LICENSE for details
@@ -16,15 +16,15 @@ module SimpleGeocoder
16
16
  end
17
17
 
18
18
  # swallow exceptions and return nil on error
19
- def geocode(address)
20
- geocode!(address)
19
+ def geocode(address, options = {})
20
+ geocode!(address, options)
21
21
  rescue ResponseError
22
22
  nil
23
23
  end
24
24
 
25
25
  # raise ResponseError exception on error
26
- def geocode!(address)
27
- response = call_geocoder_service(address)
26
+ def geocode!(address, options = {})
27
+ response = call_geocoder_service(address, options)
28
28
  if response.is_a?(Net::HTTPOK)
29
29
  return JSON.parse response.body
30
30
  else
@@ -49,10 +49,15 @@ module SimpleGeocoder
49
49
  end
50
50
 
51
51
  private
52
- def call_geocoder_service(address)
52
+ def call_geocoder_service(address, options)
53
53
  format = 'json'
54
54
  address = CGI.escape address
55
- parameters = "address=#{address}&region=#{@@api_config['google_v3']['region']}&sensor=#{@@api_config['google_v3']['sensor']}"
55
+ parameters = "address=#{address}"
56
+ parameters += options[:region] ? "&region=#{options[:region]}" : "&region=#{@@api_config['google_v3']['region']}"
57
+ parameters += options[:sensor] ? "&sensor=#{options[:sensor]}" : "&sensor=#{@@api_config['google_v3']['sensor']}"
58
+ parameters += "&bounds=#{CGI.escape options[:bounds]}" if options[:bounds]
59
+ parameters += "&language=#{options[:language]}" if options[:language]
60
+ # parameters = "address=#{address}&region=#{@@api_config['google_v3']['region']}&sensor=#{@@api_config['google_v3']['sensor']}"
56
61
  url = "#{@@api_config['google_v3']['url_root']}/#{format}?#{parameters}"
57
62
 
58
63
  Net::HTTP.get_response(URI.parse(url))
@@ -1,3 +1,3 @@
1
1
  module SimpleGeocoder
2
- VERSION = '0.0.2'
2
+ VERSION = '0.0.3'
3
3
  end
@@ -9,6 +9,41 @@ describe SimpleGeocoder::Geocoder do
9
9
  result['results'][0]['geometry']['location']['lng'].should be_within(0.0001).of(-105.2582644)
10
10
  end
11
11
 
12
+ it "works outside us region" do
13
+ result = SimpleGeocoder::Geocoder.new.geocode("Moto'otua, Samoa")
14
+ result['status'].should == "OK" # at least one geocode returned
15
+ result['results'][0]['geometry']['location']['lat'].should be_within(0.0001).of(-13.8460556)
16
+ result['results'][0]['geometry']['location']['lng'].should be_within(0.0001).of(-171.7629788)
17
+ end
18
+
19
+ it "accepts region parameter" do
20
+ # returns Toledo, Ohio location
21
+ result = SimpleGeocoder::Geocoder.new.geocode("Toledo")
22
+ result['status'].should == "OK" # at least one geocode returned
23
+ result['results'][0]['geometry']['location']['lat'].should be_within(0.0001).of(41.6639383)
24
+ result['results'][0]['geometry']['location']['lng'].should be_within(0.0001).of(-83.555212)
25
+
26
+ # returns Toledo, Spain location
27
+ result = SimpleGeocoder::Geocoder.new.geocode("Toledo",{:region=>'es'})
28
+ result['status'].should == "OK" # at least one geocode returned
29
+ result['results'][0]['geometry']['location']['lat'].should be_within(0.0001).of(39.8567775)
30
+ result['results'][0]['geometry']['location']['lng'].should be_within(0.0001).of(-4.0244759)
31
+ end
32
+
33
+ it "accepts bounds parameter" do
34
+ # returns Winnetka, IL location
35
+ result = SimpleGeocoder::Geocoder.new.geocode("Winnetka")
36
+ result['status'].should == "OK" # at least one geocode returned
37
+ result['results'][0]['geometry']['location']['lat'].should be_within(0.0001).of(42.1080834)
38
+ result['results'][0]['geometry']['location']['lng'].should be_within(0.0001).of(-87.735895)
39
+
40
+ # returns Winnetka, CA location
41
+ result = SimpleGeocoder::Geocoder.new.geocode("Winnetka", { :bounds => "34.172684,-118.604794|34.236144,-118.500938" } )
42
+ result['status'].should == "OK" # at least one geocode returned
43
+ result['results'][0]['geometry']['location']['lat'].should be_within(0.0001).of(34.20485860)
44
+ result['results'][0]['geometry']['location']['lng'].should be_within(0.0001).of(-118.57396210)
45
+ end
46
+
12
47
  it "can find lat/lng in string" do
13
48
  address = "ÜT: 34.044817,-118.311893"
14
49
  result = SimpleGeocoder::Geocoder.new.find_location(address)
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_geocoder
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 2
10
- version: 0.0.2
9
+ - 3
10
+ version: 0.0.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Ying Tsen Hong
@@ -50,7 +50,7 @@ files:
50
50
  - lib/simple_geocoder.rb
51
51
  - MIT-LICENSE
52
52
  - README.rdoc
53
- - simple_geocoder-0.0.2.gem
53
+ - simple_geocoder-0.0.3.gem
54
54
  - simple_geocoder.gemspec
55
55
  - spec/geocoder_spec.rb
56
56
  - spec/spec_helper.rb