google_map_directions 0.0.2 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,6 +1,7 @@
1
- # GoogleMapDirections
1
+ # Google Map Directions
2
2
 
3
- TODO: Write a gem description
3
+ A wrapper gem for the google directions API.
4
+ https://developers.google.com/maps/documentation/directions/
4
5
 
5
6
  ## Installation
6
7
 
@@ -18,8 +19,63 @@ Or install it yourself as:
18
19
 
19
20
  ## Usage
20
21
 
21
- TODO: Write usage instructions here
22
+ Currently, this gem only pulls a single route from the google directions api. The ability to search through multiple routes will be added at a later date.
22
23
 
24
+ Setup:
25
+ ```Ruby
26
+ directions = GoogleMapDirections::Directions.new('Soda Hall Berkeley CA', 'Foothill Berkeley CA')
27
+ ```
28
+ You can check the status code to see if the request to the API was sucessful.
29
+ ```Ruby
30
+ directions.status
31
+ ```
32
+ If you want to parse the data yourself, you can grab the ruby representation of the JSON object
33
+ ```Ruby
34
+ directions.json
35
+ ```
36
+ There is a lot of basic information you can just get from the Directions object
37
+ ```Ruby
38
+ directions.distance_as_string
39
+ directions.distance_in_meters
40
+
41
+ directions.destination_coordinates
42
+ directions.origin_coordinates
43
+
44
+ #The two coordinates methods return hashes that contain the latitude and longitude
45
+ directions.destination_coordinates["lat"]
46
+ directions.destination_coordinates["lng"]
47
+
48
+
49
+ directions.origin_address
50
+ directions.destination_address
51
+ directions.duration_as_string
52
+ directions.duration_in_seconds
53
+ ```
54
+ The steps of the route can be pulled from the step method
55
+ ```Ruby
56
+ directions.path_length
57
+ #Returns the number of steps in the routes
58
+ step1 = directions.step(0)
59
+ #Returns the first step of the path
60
+
61
+ step1.distance_as_string
62
+ step1.distance_in_meters
63
+ step1.duration_as_string
64
+ step1.duration_in_seconds
65
+
66
+
67
+ step1.end_location
68
+ step1.start_location
69
+
70
+ #The two coordinates methods return hashes that contain the latitude and longitude
71
+ step1.end_location["lat"]
72
+ step1.end_location["lng"]
73
+
74
+ step1.number
75
+ step1.HTML_instructions
76
+ ```
77
+
78
+ Let me know of any bugs/additional features wanted.
23
79
  ## Contributing
24
80
 
25
81
  1. Fork it
@@ -1,5 +1,5 @@
1
1
  module GoogleMapDirections
2
- class Path
2
+ class Step
3
3
  attr_reader :distance, :duration, :end_location, :start_location, :number, :HTML_instructions
4
4
 
5
5
  def initialize(distance, duration, end_location, start_location, number, html_instructions)
@@ -10,5 +10,19 @@ module GoogleMapDirections
10
10
  @number = number
11
11
  @HTML_instructions = html_instructions
12
12
  end
13
+
14
+ def distance_as_string
15
+ return @distance["text"]
16
+ end
17
+ def distance_in_meters
18
+ return @distance["value"]
19
+ end
20
+ def duration_as_string
21
+ return @duration["text"]
22
+ end
23
+ def duration_in_seconds
24
+ return @duration["value"]
25
+ end
13
26
  end
27
+
14
28
  end
@@ -1,3 +1,3 @@
1
1
  module GoogleMapDirections
2
- VERSION = "0.0.2"
2
+ VERSION = "1.0.0"
3
3
  end
@@ -1,24 +1,26 @@
1
1
  require "google_map_directions/version"
2
- require "google_map_directions/path"
2
+ require "google_map_directions/step"
3
3
  require "open-uri"
4
4
  require "json"
5
5
 
6
6
  module GoogleMapDirections
7
7
  class Directions
8
-
9
- attr_accessor :origin, :destination, :sensor
10
- attr_reader :json
8
+ attr_accessor :origin, :destination, :sensor, :json, :paths
11
9
  @@base_google_url = 'http://maps.googleapis.com/maps/api/directions/json?'
12
10
 
13
- def initialize(origin, destination, sensor = false)
14
- @origin = origin.gsub(/\s/,'+')
15
- @destination = destination.gsub(/\s/,'+')
16
- @sensor = sensor.to_s
17
- url = "#{@@base_google_url}&origin=#{@origin}&destination=#{@destination}&sensor=#{@sensor}"
11
+ def initialize(origin, destination, sensor=false)
12
+ @origin = origin
13
+ @destination = destination
14
+ @sensor = sensor
15
+ url = "#{@@base_google_url}&origin=#{@origin.gsub(/\s/,'+')}&destination=#{@destination.gsub(/\s/,'+')}&sensor=#{@sensor.to_s}"
18
16
  @json = JSON.parse(open(url).read)
19
- @legs = (status_check ? @json["routes"][0]["legs"][0] : nil)
20
- @paths = nil
21
- set_up_paths
17
+ if status_check
18
+ @legs = @json["routes"][0]["legs"][0]
19
+ set_up_paths
20
+ else
21
+ @legs = nil
22
+ @path = nil
23
+ end
22
24
  end
23
25
 
24
26
  def status
@@ -26,55 +28,58 @@ module GoogleMapDirections
26
28
  end
27
29
 
28
30
  def distance_as_string
29
- if status_check then return @legs["distance"]["text"] else return nil end
31
+ return @legs["distance"]["text"] unless !status_check
30
32
  end
31
33
 
32
34
  def distance_in_meters
33
- if status_check then return @legs["distance"]["value"] else return nil end
35
+ return @legs["distance"]["value"] unless !status_check
34
36
  end
35
37
 
36
38
  def destination_coordinates
37
- if status_check then return @legs['end_location'] else return nil end
39
+ return @legs['end_location'] unless !status_check
38
40
  end
39
41
 
40
42
  def origin_address
41
- if status_check then return @legs['start_address'] else return nil end
43
+ return @legs['start_address'] unless !status_check
42
44
  end
43
45
 
44
46
  def destination_address
45
- if status_check then return @legs['end_address'] else return nil end
47
+ return @legs['end_address'] unless !status_check
46
48
  end
47
49
 
48
50
  def origin_coordinates
49
- if status_check then return @legs['start_location'] else return nil end
51
+ return @legs['start_location'] unless !status_check
50
52
  end
51
53
 
52
54
 
53
55
  def duration_as_string
54
- if status_check then return @legs['duration']['text'] else return nil end
56
+ return @legs['duration']['text'] unless !status_check
57
+ end
58
+
59
+ def duration_in_seconds
60
+ return @legs['duration']["value"] unless !status_check
55
61
  end
56
62
 
57
- def duration_in_minutes
58
- if status_check then return @legs['duration']["value"] else return nil end
63
+ def path_length
64
+ return @path.length unless !status_check
59
65
  end
60
66
 
67
+ def step(number)
68
+ return @path[number] unless !status_check
69
+ end
70
+
71
+
61
72
  private
62
73
  def status_check
63
- if status != 'OK'
64
- return false
65
- else
66
- return true
67
- end
74
+ status == 'OK'
68
75
  end
69
76
 
70
77
  def set_up_paths
71
- if status_check
72
- @path = Array.new(@legs["steps"].length)
73
- count = 0
74
- @legs["steps"].each do |segment|
75
- @path[count] = GoogleMapDirections::Path.new(segment["distance"], segment["duration"], segment["end_location"], segment["start_location"], count, segment["html_instructions"])
76
- count = count + 1
77
- end
78
+ @path = Array.new(@legs["steps"].length)
79
+ count = 0
80
+ @legs["steps"].each do |segment|
81
+ @path[count] = GoogleMapDirections::Step.new(segment["distance"], segment["duration"], segment["end_location"], segment["start_location"], count, segment["html_instructions"])
82
+ count = count + 1
78
83
  end
79
84
  end
80
85
  end
@@ -13,6 +13,10 @@ describe GoogleMapDirections do
13
13
  it 'should have success code of OK' do
14
14
  @test.status.should == 'OK'
15
15
  end
16
+ it 'should return a json object representing the reply from the api' do
17
+ @test.json.should_not == nil
18
+ end
19
+
16
20
  end
17
21
 
18
22
  describe "possible bad statuses" do
@@ -62,7 +66,7 @@ describe GoogleMapDirections do
62
66
  @happy_path.duration_as_string.should == "55 mins"
63
67
  end
64
68
  it 'should correctly give the duration of the trip in minutes' do
65
- @happy_path.duration_in_minutes.should == 3322
69
+ @happy_path.duration_in_seconds.should == 3322
66
70
  end
67
71
  it 'should correctly give distances as a string for a bad reply' do
68
72
  @sad_path.distance_as_string.should == nil
@@ -87,11 +91,7 @@ describe GoogleMapDirections do
87
91
  @sad_path.duration_as_string.should == nil
88
92
  end
89
93
  it 'should correctly give the duration of the trip in minutes for a bad reply' do
90
- @sad_path.duration_in_minutes.should == nil
91
- end
94
+ @sad_path.duration_in_seconds.should == nil
95
+ end
92
96
  end
93
-
94
-
95
-
96
-
97
97
  end
data/spec/step_spec.rb ADDED
@@ -0,0 +1,53 @@
1
+ require 'spec_helper'
2
+ describe GoogleMapDirections do
3
+ describe "directions object should have a series of paths" do
4
+ before(:all) do
5
+ @test = GoogleMapDirections::Directions.new('Soda Hall Berkeley CA', 'Foothill Berkeley CA')
6
+ end
7
+
8
+ it "should have a certain number of steps in the route" do
9
+ @test.path_length.should == 4
10
+ end
11
+
12
+ it "should be able to return a step of a route" do
13
+ @test.step(0).class.should == GoogleMapDirections::Step
14
+ end
15
+ end
16
+
17
+ describe "path object should have information about the route" do
18
+ before(:all) do
19
+ @test = GoogleMapDirections::Directions.new('Soda Hall Berkeley CA', 'Foothill Berkeley CA').step(0)
20
+ end
21
+
22
+ it "should give the distance of the step as a string" do
23
+ @test.distance_as_string.should == "167 ft"
24
+ end
25
+
26
+ it "should give the duration of the step in seconds" do
27
+ @test.duration_in_seconds.should == 5
28
+ end
29
+ it "should give the distance of the step as a value" do
30
+ @test.distance_as_string.should == "167 ft"
31
+ end
32
+
33
+ it "should give the duration of the step as a vlue" do
34
+ @test.distance_in_meters.should == 51
35
+ end
36
+
37
+ it "should give the end_location of the step" do
38
+ @test.end_location.should == {"lat"=>37.87533519999999, "lng"=>-122.2583139}
39
+ end
40
+
41
+ it "should give the start_location of the step" do
42
+ @test.start_location.should == {"lat"=>37.8757857, "lng"=>-122.2584081}
43
+ end
44
+
45
+ it "should give the number of the step" do
46
+ @test.number.should == 0
47
+ end
48
+
49
+ it "should give the HTML_instructions of the step" do
50
+ @test.HTML_instructions.should == "Head <b>south</b> on <b>Le Roy Ave</b> toward <b>Hearst Ave</b>"
51
+ end
52
+ end
53
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: google_map_directions
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 1.0.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-12-30 00:00:00.000000000 Z
12
+ date: 2013-12-31 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -74,10 +74,11 @@ files:
74
74
  - Rakefile
75
75
  - google_map_directions.gemspec
76
76
  - lib/google_map_directions.rb
77
- - lib/google_map_directions/path.rb
77
+ - lib/google_map_directions/step.rb
78
78
  - lib/google_map_directions/version.rb
79
79
  - spec/direction_spec.rb
80
80
  - spec/spec_helper.rb
81
+ - spec/step_spec.rb
81
82
  - test/google_map_directions/google_map_direction_test.rb
82
83
  - test/helper.rb
83
84
  homepage: https://github.com/chrishsu2312/google-map-directions
@@ -108,5 +109,6 @@ summary: I'll update this once I have some working code.
108
109
  test_files:
109
110
  - spec/direction_spec.rb
110
111
  - spec/spec_helper.rb
112
+ - spec/step_spec.rb
111
113
  - test/google_map_directions/google_map_direction_test.rb
112
114
  - test/helper.rb