google_directions 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/Manifest CHANGED
@@ -1,5 +1,9 @@
1
1
  Manifest
2
2
  README.textile
3
3
  Rakefile
4
+ google_directions.gemspec
4
5
  init.rb
5
6
  lib/google_directions.rb
7
+ test/mocks/google_directions_samle_xml.xml
8
+ test/test_helper.rb
9
+ test/unit/google_directions_test.rb
data/README.textile CHANGED
@@ -1,16 +1,54 @@
1
1
  h2. Usage
2
2
 
3
+ Get a google maps API key, and store in your app under GOOGLE_MAPS_API_KEY
4
+ <pre>GOOGLE_MAPS_API_KEY = 'your_api_key'</pre>
5
+
6
+ <pre>directions = GoogleDirections.new(origin, destination)</pre>
7
+ where _origin_ and _destination_ are strings of addresses or places that Google can find an address for. Example: "816 Meridian St., 37207"
8
+
9
+ Get drive time or distance of whole trip
10
+ <pre>
11
+ drive_time_in_minutes = directions.drive_time_in_minutes
12
+ distance_in_miles = directions.distance_in_miles
13
+ </pre>
14
+
15
+ Get the XML Google returns with every turn, or the API call URL
16
+ <pre>
17
+ xml = directions.xml
18
+ xml_call = directions.xml_call
19
+ </pre>
20
+
21
+ h3. Error situations
22
+
23
+ If Google can't recognize your places, the distance_in_miles and drive_time_in_minutes will each return 0. You can call <pre>directions.status</pre> and it should return "NOT_FOUND" (directions.status will also return any other Google API call statuses)
24
+
25
+ TODO: handle QUERY_OVER_LIMIT
3
26
 
4
27
  h2. Installation
5
28
 
6
29
  h3. gem
30
+
31
+ rails 2.3
7
32
  # gem install google_directions
8
33
  # add config.gem "google_directions" to your environment.rb file
9
34
 
10
- Or do Bundler's Gemfile
35
+ rails 3.0
36
+ # gem 'google_directions' in your Gemfile
37
+ # <pre>bundle install</pre> from command line
11
38
 
12
- h3. Rails 2.3.8 compatible
13
- Not yet tested on Rails 3
39
+ h3. Rails plugin
40
+
41
+ Rails 2.3
42
+ <pre>script/plugin install git://github.com/joshcrews/google-directions-ruby.git</pre>
43
+
44
+ Rails 3.0
45
+ <pre>rails plugin install git://github.com/joshcrews/google-directions-ruby.git</pre>
46
+
47
+ h3. Compatibility
48
+
49
+ Tested on Rails 2.3.8
50
+
51
+ Not yet tested on Rails 3. It probably is Rails 3 compatible, because it's just a single class with a few methods. It's probably compatible with every ruby project ever.
14
52
 
15
53
  h3. Google maps API key
16
54
 
@@ -18,4 +56,5 @@ You'll need a Google Map API key
18
56
 
19
57
  http://code.google.com/apis/maps/signup.html
20
58
 
21
- Include it as the constant GOOGLE_MAPS_API_KEY in an app configuration file (environment.rb, config/initializers/api_keys.rb)
59
+ Include it as the constant GOOGLE_MAPS_API_KEY in an app configuration file (environment.rb, config/initializers/api_keys.rb)
60
+
data/Rakefile CHANGED
@@ -3,7 +3,7 @@ require 'rubygems'
3
3
  require 'rake'
4
4
  require 'echoe'
5
5
 
6
- Echoe.new('google_directions', '0.1.0') do |p|
6
+ Echoe.new('google_directions', '0.1.1') do |p|
7
7
  p.description = "Ruby-wrapper for Google Directions API. Can return the drive time and driving distance between to places"
8
8
  p.url = "http://github.com/joshcrews/Google-Directions-Ruby"
9
9
  p.author = "Josh Crews"
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{google_directions}
5
- s.version = "0.1.0"
5
+ s.version = "0.1.1"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Josh Crews"]
@@ -10,13 +10,14 @@ Gem::Specification.new do |s|
10
10
  s.description = %q{Ruby-wrapper for Google Directions API. Can return the drive time and driving distance between to places}
11
11
  s.email = %q{josh@joshcrews.com}
12
12
  s.extra_rdoc_files = ["README.textile", "lib/google_directions.rb"]
13
- s.files = ["Manifest", "README.textile", "Rakefile", "init.rb", "lib/google_directions.rb", "google_directions.gemspec"]
13
+ s.files = ["Manifest", "README.textile", "Rakefile", "google_directions.gemspec", "init.rb", "lib/google_directions.rb", "test/mocks/google_directions_samle_xml.xml", "test/test_helper.rb", "test/unit/google_directions_test.rb"]
14
14
  s.homepage = %q{http://github.com/joshcrews/Google-Directions-Ruby}
15
15
  s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Google_directions", "--main", "README.textile"]
16
16
  s.require_paths = ["lib"]
17
17
  s.rubyforge_project = %q{google_directions}
18
18
  s.rubygems_version = %q{1.3.7}
19
19
  s.summary = %q{Ruby-wrapper for Google Directions API. Can return the drive time and driving distance between to places}
20
+ s.test_files = ["test/test_helper.rb", "test/unit/google_directions_test.rb"]
20
21
 
21
22
  if s.respond_to? :specification_version then
22
23
  current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
@@ -8,40 +8,52 @@ class GoogleDirections
8
8
  @base_url = "http://maps.google.com/maps/api/directions/xml?key=#{GOOGLE_MAPS_API_KEY}&sensor=false&"
9
9
  @location_1 = location_1
10
10
  @location_2 = location_2
11
+ options = "origin=#{transcribe(@location_1)}&destination=#{transcribe(@location_2)}"
12
+ @xml_call = @base_url + options
13
+ @xml = get_url(@xml_call)
14
+ @status = find_status
11
15
  end
16
+
17
+ # an example URL to be generated
18
+ #http://maps.google.com/maps/api/directions/xml?origin=St.+Louis,+MO&destination=Nashville,+TN&sensor=false&key=ABQIAAAAINgf4OmAIbIdWblvypOUhxSQ8yY-fgrep0oj4uKpavE300Q6ExQlxB7SCyrAg2evsxwAsak4D0Liiv
12
19
 
13
- # an example URL to be generated
14
- #http://maps.google.com/maps/api/directions/xml?origin=St.+Louis,+MO&destination=Nashville,+TN&sensor=false&key=ABQIAAAAINgf4OmAIbIdWblvypOUhxSQ8yY-fgrep0oj4uKpavE300Q6ExQlxB7SCyrAg2evsxwAsak4D0Liiv
15
-
20
+ def find_status
21
+ doc = Nokogiri::XML(@xml)
22
+ doc.css("status").text
23
+ end
24
+
16
25
  def xml
17
- full_url = xml_call
18
- Rails.logger.info "Hit Google maps: #{full_url}"
19
- @xml = get_url(full_url)
26
+ @xml
20
27
  end
21
-
22
- def xml_call
23
- options = "origin=#{transcribe(@location_1)}&destination=#{transcribe(@location_2)}"
24
- full_url = @base_url + options
25
- end
26
-
27
- def drive_time_in_minutes
28
- @xml ||= xml
28
+
29
+ def xml_call
30
+ @xml_call
31
+ end
32
+
33
+ def drive_time_in_minutes
34
+ if @status == "NOT_FOUND"
35
+ drive_time = 0
36
+ else
29
37
  doc = Nokogiri::XML(@xml)
30
- raise "Google returned nothing" if doc.nil?
31
- debugger if drive_time = doc.css("duration value").last.nil?
32
38
  drive_time = doc.css("duration value").last.text
33
- Rails.logger.info "Hit Google maps: drive time: #{drive_time}"
34
39
  convert_to_minutes(drive_time)
35
40
  end
41
+ end
36
42
 
37
- def distance_in_miles
38
- @xml ||= xml
39
- doc = Nokogiri::XML(xml)
43
+ def distance_in_miles
44
+ if @status == "NOT_FOUND"
45
+ distance_in_miles = 0
46
+ else
47
+ doc = Nokogiri::XML(@xml)
40
48
  meters = doc.css("distance value").last.text
41
- miles = (meters.to_f / 1610.22).round
42
- Rails.logger.info "Hit Google maps: #{miles} miles"
43
- miles
49
+ distance_in_miles = (meters.to_f / 1610.22).round
50
+ distance_in_miles
44
51
  end
52
+ end
53
+
54
+ def status
55
+ @status
56
+ end
45
57
 
46
58
  private
47
59
 
@@ -0,0 +1,80 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <DirectionsResponse>
3
+ <status>OK</status>
4
+ <route>
5
+ <summary>TN-53 S/Gordonsville Hwy</summary>
6
+ <leg>
7
+ <step>
8
+ <travel_mode>DRIVING</travel_mode>
9
+ <start_location>
10
+ <lat>36.2264000</lat>
11
+ <lng>-85.9490200</lng>
12
+ </start_location>
13
+ <end_location>
14
+ <lat>36.1929700</lat>
15
+ <lng>-85.9550000</lng>
16
+ </end_location>
17
+ <polyline>
18
+ <points>_nb|Ej|qkOfCFhH_@dEc@rE?nDb@vEjCjHxFzBtBpKnDlD|@bEPbGv@pFiArF?pHyDzDQHF~AExGz@dE?zE`@~@l@zDHbBf@lEf@pAZlG|FrC|@bGfFvBv@vLDfFoD`BO</points>
19
+ <levels>B????@??@???@???@??????@??@???@@?B</levels>
20
+ </polyline>
21
+ <duration>
22
+ <value>222</value>
23
+ <text>4 mins</text>
24
+ </duration>
25
+ <html_instructions>Head &lt;b&gt;south&lt;/b&gt; on &lt;b&gt;TN-53 S/Gordonsville Hwy&lt;/b&gt; toward &lt;b&gt;Riverwood Cir&lt;/b&gt;</html_instructions>
26
+ <distance>
27
+ <value>3950</value>
28
+ <text>2.5 mi</text>
29
+ </distance>
30
+ </step>
31
+ <step>
32
+ <travel_mode>DRIVING</travel_mode>
33
+ <start_location>
34
+ <lat>36.1929700</lat>
35
+ <lng>-85.9550000</lng>
36
+ </start_location>
37
+ <end_location>
38
+ <lat>36.1773300</lat>
39
+ <lng>-85.9475700</lng>
40
+ </end_location>
41
+ <polyline>
42
+ <points>a}{{EvaskOTw@zPiJPU~@QxCyA|C_BbBoAvCa@vI}F^q@xBg@~LmGbC{@|Be@rQFvAW</points>
43
+ <levels>B???????????@???B</levels>
44
+ </polyline>
45
+ <duration>
46
+ <value>141</value>
47
+ <text>2 mins</text>
48
+ </duration>
49
+ <html_instructions>Turn &lt;b&gt;left&lt;/b&gt; at &lt;b&gt;TN-53 S&lt;/b&gt; &lt;div style="font-size:0.9em"&gt;Destination will be on the right&lt;/div&gt;</html_instructions>
50
+ <distance>
51
+ <value>1912</value>
52
+ <text>1.2 mi</text>
53
+ </distance>
54
+ </step>
55
+ <duration>
56
+ <value>363</value>
57
+ <text>6 mins</text>
58
+ </duration>
59
+ <distance>
60
+ <value>5862</value>
61
+ <text>3.6 mi</text>
62
+ </distance>
63
+ <start_location>
64
+ <lat>36.2264000</lat>
65
+ <lng>-85.9490200</lng>
66
+ </start_location>
67
+ <end_location>
68
+ <lat>36.1773300</lat>
69
+ <lng>-85.9475700</lng>
70
+ </end_location>
71
+ <start_address>121 Gordonsville Hwy, Carthage, TN 37030, USA</start_address>
72
+ <end_address>499 Gordonsville Hwy, Gordonsville, TN 38563, USA</end_address>
73
+ </leg>
74
+ <copyrights>Map data ©2010 Google</copyrights>
75
+ <overview_polyline>
76
+ <points>_nb|Ej|qkOfCFhH_@dEc@rE?nDb@vEjCjHxFzBtBpKnDlD|@bEPbGv@pFiArF?pHyDzDQHF~AExGz@dE?zE`@~@l@zDHbBf@lEf@pAZlG|FrC|@bGfFvBv@vLDfFoD`BOTw@zPiJPU~@QxCyA|C_BbBoAvCa@vI}F^q@xBg@~LmGbC{@|Be@rQFvAW</points>
77
+ <levels>B????@??@???@??@?????@????@???@A?????????????@???B</levels>
78
+ </overview_polyline>
79
+ </route>
80
+ </DirectionsResponse>
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'mocha'
3
+ require 'test/unit'
4
+ require 'ruby-debug'
5
+
6
+ $:.unshift File.expand_path('../lib', __FILE__)
7
+ require 'google_directions'
8
+
9
+ GOOGLE_MAPS_API_KEY = "ABQIAAAAj_VbPHmedky6mQd2p37p9xQbL-Ke6g7EfeqhfwcS7ZnhWLsBVxQA3yWKTKM9MVa31hYIhxsKffu7Pw"
10
+
@@ -0,0 +1,25 @@
1
+ require 'test_helper'
2
+
3
+ class GoogleDirectionsTest < Test::Unit::TestCase
4
+
5
+ def test_the_truth
6
+ assert true
7
+ end
8
+
9
+ def test_happy_case
10
+ directions = GoogleDirections.new("121 Gordonsville Highway, 37030", "499 Gordonsville Highway, 38563")
11
+ assert_equal(4, directions.distance_in_miles)
12
+ assert_equal(6, directions.drive_time_in_minutes)
13
+ assert_equal("http://maps.google.com/maps/api/directions/xml?key=ABQIAAAAj_VbPHmedky6mQd2p37p9xQbL-Ke6g7EfeqhfwcS7ZnhWLsBVxQA3yWKTKM9MVa31hYIhxsKffu7Pw&sensor=false&origin=121+Gordonsville+Highway,+37030&destination=499+Gordonsville+Highway,+38563", directions.xml_call)
14
+ assert_not_nil(directions.xml =~ /36\.1773300/)
15
+ end
16
+
17
+ def test_directions_not_found
18
+ directions = GoogleDirections.new("fasfefasdfdfsd", "499 Gordonsville Highway, 38563")
19
+ assert_equal(0, directions.distance_in_miles)
20
+ assert_equal(0, directions.drive_time_in_minutes)
21
+ assert_equal("NOT_FOUND", directions.status)
22
+ end
23
+
24
+
25
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: google_directions
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 0
10
- version: 0.1.0
9
+ - 1
10
+ version: 0.1.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Josh Crews
@@ -47,9 +47,12 @@ files:
47
47
  - Manifest
48
48
  - README.textile
49
49
  - Rakefile
50
+ - google_directions.gemspec
50
51
  - init.rb
51
52
  - lib/google_directions.rb
52
- - google_directions.gemspec
53
+ - test/mocks/google_directions_samle_xml.xml
54
+ - test/test_helper.rb
55
+ - test/unit/google_directions_test.rb
53
56
  has_rdoc: true
54
57
  homepage: http://github.com/joshcrews/Google-Directions-Ruby
55
58
  licenses: []
@@ -90,5 +93,6 @@ rubygems_version: 1.3.7
90
93
  signing_key:
91
94
  specification_version: 3
92
95
  summary: Ruby-wrapper for Google Directions API. Can return the drive time and driving distance between to places
93
- test_files: []
94
-
96
+ test_files:
97
+ - test/test_helper.rb
98
+ - test/unit/google_directions_test.rb