google_directions 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source :rubygems
2
+
3
+ gemspec
data/README.textile CHANGED
@@ -1,8 +1,5 @@
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
3
  <pre>directions = GoogleDirections.new(origin, destination)</pre>
7
4
  where _origin_ and _destination_ are strings of addresses or places that Google can find an address for. Example: "816 Meridian St., 37207"
8
5
 
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.4') do |p|
6
+ Echoe.new('google_directions', '0.1.3') 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,33 +2,23 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{google_directions}
5
- s.version = "0.1.4"
5
+ s.version = "0.1.5"
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"]
9
- s.date = %q{2010-07-21}
9
+ s.date = Time.now.utc.strftime("%Y-%m-%d")
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", "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"]
13
+ s.files = Dir["README.textile", "Gemfile", "Rakefile", "google_directions.gemspec", 'init.rb', "test/**/*", "lib/**/*.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
+ s.test_files = Dir.glob("test/**/*_test.rb")
21
21
 
22
- if s.respond_to? :specification_version then
23
- current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
24
- s.specification_version = 3
25
-
26
- if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
27
- s.add_development_dependency(%q<nokogiri>, [">= 1.4.1"])
28
- else
29
- s.add_dependency(%q<nokogiri>, [">= 1.4.1"])
30
- end
31
- else
32
- s.add_dependency(%q<nokogiri>, [">= 1.4.1"])
33
- end
22
+ s.add_dependency("nokogiri", ">=1.4.1")
23
+ s.add_dependency("extlib", ">=0.9.15")
34
24
  end
data/init.rb CHANGED
@@ -1 +1,2 @@
1
- require 'google_directions'
1
+ # encoding: UTF-8
2
+ require 'google_directions'
@@ -1,75 +1,99 @@
1
+ # encoding: UTF-8
2
+ require 'cgi'
1
3
  require 'net/http'
4
+ require 'open-uri'
2
5
  require 'nokogiri'
3
- require 'google_directions'
6
+ require 'extlib/hash'
4
7
 
5
8
  class GoogleDirections
6
-
7
- def initialize(location_1, location_2)
8
- @base_url = "http://maps.google.com/maps/api/directions/xml?key=#{GOOGLE_MAPS_API_KEY}&sensor=false&"
9
- @location_1 = location_1
10
- @location_2 = location_2
11
- options = "origin=#{transcribe(@location_1)}&destination=#{transcribe(@location_2)}"
12
- @xml_call = @base_url + options
13
- @status = find_status
14
- end
15
9
 
16
- # an example URL to be generated
17
- #http://maps.google.com/maps/api/directions/xml?origin=St.+Louis,+MO&destination=Nashville,+TN&sensor=false&key=ABQIAAAAINgf4OmAIbIdWblvypOUhxSQ8yY-fgrep0oj4uKpavE300Q6ExQlxB7SCyrAg2evsxwAsak4D0Liiv
18
-
19
- def find_status
20
- doc = Nokogiri::XML(xml)
21
- doc.css("status").text
22
- end
10
+ attr_reader :status, :doc, :xml, :origin, :destination, :options
23
11
 
24
- def xml
25
- unless @xml.nil?
26
- @xml
27
- else
28
- @xml ||= get_url(@xml_call)
29
- end
12
+ @@base_url = 'http://maps.googleapis.com/maps/api/directions/xml'
13
+
14
+ @@default_options = {
15
+ :language => :en,
16
+ :alternative => :true,
17
+ :sensor => :false,
18
+ :mode => :driving,
19
+ }
20
+
21
+ def initialize(origin, destination, opts=@@default_options)
22
+ @origin = origin
23
+ @destination = destination
24
+ @options = opts.merge({:origin => transcribe(@origin), :destination => transcribe(@destination)})
25
+
26
+ @url = @@base_url + '?' + @options.to_params
27
+ @xml = open(@url).read
28
+ @doc = Nokogiri::XML(@xml)
29
+ @status = @doc.css('status').text
30
30
  end
31
31
 
32
32
  def xml_call
33
- @xml_call
33
+ @url
34
34
  end
35
35
 
36
+ # an example URL to be generated
37
+ #http://maps.google.com/maps/api/directions/xml?origin=St.+Louis,+MO&destination=Nashville,+TN&sensor=false&key=ABQIAAAAINgf4OmAIbIdWblvypOUhxSQ8yY-fgrep0oj4uKpavE300Q6ExQlxB7SCyrAg2evsxwAsak4D0Liiv
38
+
36
39
  def drive_time_in_minutes
37
40
  if @status != "OK"
38
41
  drive_time = 0
39
42
  else
40
- doc = Nokogiri::XML(xml)
41
- drive_time = doc.css("duration value").last.text
43
+ drive_time = @doc.css("duration value").last.text
42
44
  convert_to_minutes(drive_time)
43
45
  end
44
46
  end
45
47
 
48
+ # the distance.value field always contains a value expressed in meters.
49
+ def distance
50
+ return @distance if @distance
51
+ unless @status == 'OK'
52
+ @distance = 0
53
+ else
54
+ @distance = @doc.css("distance value").last.text
55
+ end
56
+ end
57
+
58
+ def distance_text
59
+ return @distance_text if @distance_text
60
+ unless @status == 'OK'
61
+ @distance_text = "0 km"
62
+ else
63
+ @distance_text = @doc.css("distance text").last.text
64
+ end
65
+ end
66
+
46
67
  def distance_in_miles
47
68
  if @status != "OK"
48
69
  distance_in_miles = 0
49
70
  else
50
- doc = Nokogiri::XML(xml)
51
- meters = doc.css("distance value").last.text
71
+ meters = distance
52
72
  distance_in_miles = (meters.to_f / 1610.22).round
53
73
  distance_in_miles
54
74
  end
55
75
  end
56
-
57
- def status
58
- @status
76
+
77
+ def public_url
78
+ "http://maps.google.com/maps?saddr=#{transcribe(@origin)}&daddr=#{transcribe(@destination)}&hl=#{@options[:language]}&ie=UTF8"
79
+ end
80
+
81
+ def steps
82
+ if @status == 'OK'
83
+ @doc.css('html_instructions').map {|a| a.text }
84
+ else
85
+ []
86
+ end
59
87
  end
60
-
88
+
61
89
  private
62
-
90
+
63
91
  def convert_to_minutes(text)
64
- (text.to_i / 60).ceil
92
+ (text.to_f / 60).round
65
93
  end
66
-
94
+
67
95
  def transcribe(location)
68
- location.gsub(" ", "+")
96
+ CGI::escape(location)
69
97
  end
70
98
 
71
- def get_url(url)
72
- Net::HTTP.get(::URI.parse(url))
73
- end
74
-
75
- end
99
+ end
@@ -7,74 +7,60 @@
7
7
  <step>
8
8
  <travel_mode>DRIVING</travel_mode>
9
9
  <start_location>
10
- <lat>36.2264000</lat>
11
- <lng>-85.9490200</lng>
10
+ <lat>36.2263600</lat>
11
+ <lng>-85.9490400</lng>
12
12
  </start_location>
13
13
  <end_location>
14
- <lat>36.1929700</lat>
15
- <lng>-85.9550000</lng>
14
+ <lat>36.1772300</lat>
15
+ <lng>-85.9476400</lng>
16
16
  </end_location>
17
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>
18
+ <points>wmb|En|qkObGX~BGbBQhHwAnBUxB?fBZlCjAzA~At@jA|DnIbC~DnAfAfDjBpCl@zAJ~A?vC[hRmEnHmA`Z}@~ENfCTlDt@xG|BxGvDtFlEnExC`Dz@bCLlAAnBS~CgAbXgPlOyHrf@}TfAOpQHlBE</points>
19
+ <levels>B??????@????@???@???@??@????@??A????@??B</levels>
20
20
  </polyline>
21
21
  <duration>
22
- <value>222</value>
23
- <text>4 mins</text>
22
+ <value>354</value>
23
+ <text>6 mins</text>
24
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>
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;&lt;div style=&quot;font-size:0.9em&quot;&gt;Destination will be on the right&lt;/div&gt;</html_instructions>
26
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>
27
+ <value>5928</value>
28
+ <text>3.7 mi</text>
53
29
  </distance>
54
30
  </step>
55
31
  <duration>
56
- <value>363</value>
32
+ <value>354</value>
57
33
  <text>6 mins</text>
58
34
  </duration>
59
35
  <distance>
60
- <value>5862</value>
61
- <text>3.6 mi</text>
36
+ <value>5928</value>
37
+ <text>3.7 mi</text>
62
38
  </distance>
63
39
  <start_location>
64
- <lat>36.2264000</lat>
65
- <lng>-85.9490200</lng>
40
+ <lat>36.2263600</lat>
41
+ <lng>-85.9490400</lng>
66
42
  </start_location>
67
43
  <end_location>
68
- <lat>36.1773300</lat>
69
- <lng>-85.9475700</lng>
44
+ <lat>36.1772300</lat>
45
+ <lng>-85.9476400</lng>
70
46
  </end_location>
71
47
  <start_address>121 Gordonsville Hwy, Carthage, TN 37030, USA</start_address>
72
48
  <end_address>499 Gordonsville Hwy, Gordonsville, TN 38563, USA</end_address>
73
49
  </leg>
74
- <copyrights>Map data ©2010 Google</copyrights>
50
+ <copyrights>Map data ©2011 Google</copyrights>
75
51
  <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>
52
+ <points>wmb|En|qkObGX~BGbBQhHwAnBUxB?fBZlCjAzA~At@jA|DnIbC~DnAfAfDjBpCl@zAJ~A?vC[hRmEnHmA`Z}@~ENfCTlDt@xG|BxGvDtFlEnExC`Dz@bCLlAAnBS~CgAbXgPlOyHrf@}TfAOpQHlBE</points>
53
+ <levels>B??????@????@???@???@??@????@??A????@??B</levels>
78
54
  </overview_polyline>
55
+ <bounds>
56
+ <southwest>
57
+ <lat>36.1772300</lat>
58
+ <lng>-85.9560100</lng>
59
+ </southwest>
60
+ <northeast>
61
+ <lat>36.2263600</lat>
62
+ <lng>-85.9476200</lng>
63
+ </northeast>
64
+ </bounds>
79
65
  </route>
80
66
  </DirectionsResponse>
data/test/test_helper.rb CHANGED
@@ -1,10 +1,6 @@
1
+ # encoding: UTF-8
1
2
  require 'rubygems'
2
3
  require 'mocha'
3
4
  require 'test/unit'
4
- require 'ruby-debug'
5
5
 
6
- $:.unshift File.expand_path('../lib', __FILE__)
7
6
  require 'google_directions'
8
-
9
- GOOGLE_MAPS_API_KEY = "afakeapithatworksanyway"
10
-
@@ -1,32 +1,57 @@
1
- require 'test_helper'
1
+ # encoding: UTF-8
2
+ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'test_helper'))
2
3
 
4
+ # TODO: mocks
3
5
  class GoogleDirectionsTest < Test::Unit::TestCase
4
6
 
5
- def test_the_truth
6
- assert true
7
- end
8
-
9
7
  def test_happy_case
10
- directions = GoogleDirections.new("121 Gordonsville Highway, 37030", "499 Gordonsville Highway, 38563")
8
+ orig = "121 Gordonsville Highway, 37030"
9
+ dest = "499 Gordonsville Highway, 38563"
10
+ directions = GoogleDirections.new(orig, dest)
11
11
  assert_equal(4, directions.distance_in_miles)
12
12
  assert_equal(6, directions.drive_time_in_minutes)
13
- assert_equal("http://maps.google.com/maps/api/directions/xml?key=afakeapithatworksanyway&sensor=false&origin=121+Gordonsville+Highway,+37030&destination=499+Gordonsville+Highway,+38563", directions.xml_call)
14
- assert_not_nil(directions.xml =~ /36\.1773300/)
13
+ assert_equal('http://maps.googleapis.com/maps/api/directions/xml?language=en&alternative=true&sensor=false&mode=driving&origin=121+Gordonsville+Highway%2C+37030&destination=499+Gordonsville+Highway%2C+38563', directions.xml_call)
14
+ # end_location > lat
15
+ assert_not_nil(directions.xml =~ /36\.1772300/)
16
+ assert_equal orig, directions.origin
17
+ assert_equal dest, directions.destination
15
18
  end
16
-
19
+
17
20
  def test_directions_not_found
18
21
  directions = GoogleDirections.new("fasfefasdfdfsd", "499 Gordonsville Highway, 38563")
19
22
  assert_equal(0, directions.distance_in_miles)
20
23
  assert_equal(0, directions.drive_time_in_minutes)
21
24
  assert_equal("NOT_FOUND", directions.status)
22
25
  end
23
-
26
+
24
27
  def test_zero_results
25
- directions = GoogleDirections.new("COMMUNITY SOUTH HOSPITAL 1402 EAST COUNTY LINE ROAD, 46227", "499 Gordonsville Highway, 38563")
28
+ directions = GoogleDirections.new("27 Beemdenlaan, 2550 Kontich", "499 Gordonsville Highway, 38563")
26
29
  assert_equal(0, directions.distance_in_miles)
27
30
  assert_equal(0, directions.drive_time_in_minutes)
28
31
  assert_equal("ZERO_RESULTS", directions.status)
29
32
  end
30
-
31
-
33
+
34
+ def test_french_direction
35
+ assert_nothing_raised do
36
+ # URI::InvalidURIError
37
+ directions = GoogleDirections.new("15 rue poissonnière, 75002 Paris", "169 11th Street CA 94103 San Francisco United States")
38
+ end
39
+ end
40
+
41
+ def test_steps
42
+ directions = GoogleDirections.new("rue poissonnière, 75002 Paris", "51 rue de Turbigo, 75003 Paris France")
43
+ assert_equal Array, directions.steps.class
44
+ assert_equal 3, directions.steps.size
45
+ end
46
+
47
+ def test_distance_text
48
+ directions = GoogleDirections.new("Place du Maquis du Vercors PARIS-19EME", "rue poissoniere 75002 paris")
49
+ assert_equal String, directions.distance_text.class
50
+ assert_equal "6.7 km", directions.distance_text
51
+ end
52
+
53
+ def test_zero_distance_text
54
+ directions = GoogleDirections.new("27 Beemdenlaan, 2550 Kontich", "499 Gordonsville Highway, 38563")
55
+ assert_equal "0 km", directions.distance_text
56
+ end
32
57
  end
metadata CHANGED
@@ -1,98 +1,86 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: google_directions
3
- version: !ruby/object:Gem::Version
4
- hash: 19
5
- prerelease: false
6
- segments:
7
- - 0
8
- - 1
9
- - 4
10
- version: 0.1.4
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.5
5
+ prerelease:
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - Josh Crews
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2010-07-21 00:00:00 -05:00
19
- default_executable:
20
- dependencies:
21
- - !ruby/object:Gem::Dependency
12
+ date: 2013-01-25 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
22
15
  name: nokogiri
23
- prerelease: false
24
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: &70151553549100 !ruby/object:Gem::Requirement
25
17
  none: false
26
- requirements:
27
- - - ">="
28
- - !ruby/object:Gem::Version
29
- hash: 5
30
- segments:
31
- - 1
32
- - 4
33
- - 1
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
34
21
  version: 1.4.1
35
- type: :development
36
- version_requirements: *id001
37
- description: Ruby-wrapper for Google Directions API. Can return the drive time and driving distance between to places
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70151553549100
25
+ - !ruby/object:Gem::Dependency
26
+ name: extlib
27
+ requirement: &70151553548140 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: 0.9.15
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70151553548140
36
+ description: Ruby-wrapper for Google Directions API. Can return the drive time and
37
+ driving distance between to places
38
38
  email: josh@joshcrews.com
39
39
  executables: []
40
-
41
40
  extensions: []
42
-
43
- extra_rdoc_files:
41
+ extra_rdoc_files:
44
42
  - README.textile
45
43
  - lib/google_directions.rb
46
- files:
47
- - Manifest
44
+ files:
48
45
  - README.textile
46
+ - Gemfile
49
47
  - Rakefile
50
48
  - google_directions.gemspec
51
49
  - init.rb
52
- - lib/google_directions.rb
53
50
  - test/mocks/google_directions_samle_xml.xml
54
51
  - test/test_helper.rb
55
52
  - test/unit/google_directions_test.rb
56
- has_rdoc: true
53
+ - lib/google_directions.rb
57
54
  homepage: http://github.com/joshcrews/Google-Directions-Ruby
58
55
  licenses: []
59
-
60
56
  post_install_message:
61
- rdoc_options:
57
+ rdoc_options:
62
58
  - --line-numbers
63
59
  - --inline-source
64
60
  - --title
65
61
  - Google_directions
66
62
  - --main
67
63
  - README.textile
68
- require_paths:
64
+ require_paths:
69
65
  - lib
70
- required_ruby_version: !ruby/object:Gem::Requirement
66
+ required_ruby_version: !ruby/object:Gem::Requirement
71
67
  none: false
72
- requirements:
73
- - - ">="
74
- - !ruby/object:Gem::Version
75
- hash: 3
76
- segments:
77
- - 0
78
- version: "0"
79
- required_rubygems_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ! '>='
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
73
  none: false
81
- requirements:
82
- - - ">="
83
- - !ruby/object:Gem::Version
84
- hash: 11
85
- segments:
86
- - 1
87
- - 2
88
- version: "1.2"
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '1.2'
89
78
  requirements: []
90
-
91
79
  rubyforge_project: google_directions
92
- rubygems_version: 1.3.7
80
+ rubygems_version: 1.8.11
93
81
  signing_key:
94
82
  specification_version: 3
95
- summary: Ruby-wrapper for Google Directions API. Can return the drive time and driving distance between to places
96
- test_files:
97
- - test/test_helper.rb
83
+ summary: Ruby-wrapper for Google Directions API. Can return the drive time and driving
84
+ distance between to places
85
+ test_files:
98
86
  - test/unit/google_directions_test.rb
data/Manifest DELETED
@@ -1,9 +0,0 @@
1
- Manifest
2
- README.textile
3
- Rakefile
4
- google_directions.gemspec
5
- init.rb
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