google_map_directions 0.0.0 → 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/.gitignore +19 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +9 -0
- data/google_map_directions.gemspec +24 -0
- data/lib/google_map_directions/path.rb +14 -0
- data/lib/google_map_directions/version.rb +3 -0
- data/lib/google_map_directions.rb +81 -0
- data/spec/direction_spec.rb +97 -0
- data/spec/spec_helper.rb +4 -0
- data/test/google_map_directions/google_map_direction_test.rb +26 -0
- data/test/helper.rb +3 -0
- metadata +75 -9
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 TODO: Write your name
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# GoogleMapDirections
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'google_map_directions'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install google_map_directions
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'google_map_directions/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "google_map_directions"
|
8
|
+
spec.version = GoogleMapDirections::VERSION
|
9
|
+
spec.authors = ["Chris Hsu"]
|
10
|
+
spec.email = ["chris_h2312@yahoo.com"]
|
11
|
+
spec.description = %q{A wrapper gem for the google directions API}
|
12
|
+
spec.summary = %q{I'll update this once I have some working code.}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module GoogleMapDirections
|
2
|
+
class Path
|
3
|
+
attr_reader :distance, :duration, :end_location, :start_location, :number, :HTML_instructions
|
4
|
+
|
5
|
+
def initialize(distance, duration, end_location, start_location, number, html_instructions)
|
6
|
+
@distance = distance
|
7
|
+
@duration = duration
|
8
|
+
@end_location = end_location
|
9
|
+
@start_location = start_location
|
10
|
+
@number = number
|
11
|
+
@HTML_instructions = html_instructions
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,81 @@
|
|
1
|
+
require "google_map_directions/version"
|
2
|
+
require "google_map_directions/path"
|
3
|
+
require "open-uri"
|
4
|
+
require "json"
|
5
|
+
|
6
|
+
module GoogleMapDirections
|
7
|
+
class Directions
|
8
|
+
|
9
|
+
attr_accessor :origin, :destination, :sensor
|
10
|
+
attr_reader :json
|
11
|
+
@@base_google_url = 'http://maps.googleapis.com/maps/api/directions/json?'
|
12
|
+
|
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}"
|
18
|
+
@json = JSON.parse(open(url).read)
|
19
|
+
@legs = (status_check ? @json["routes"][0]["legs"][0] : nil)
|
20
|
+
@paths = nil
|
21
|
+
set_up_paths
|
22
|
+
end
|
23
|
+
|
24
|
+
def status
|
25
|
+
return @json["status"]
|
26
|
+
end
|
27
|
+
|
28
|
+
def distance_as_string
|
29
|
+
if status_check then return @legs["distance"]["text"] else return nil end
|
30
|
+
end
|
31
|
+
|
32
|
+
def distance_in_meters
|
33
|
+
if status_check then return @legs["distance"]["value"] else return nil end
|
34
|
+
end
|
35
|
+
|
36
|
+
def destination_coordinates
|
37
|
+
if status_check then return @legs['end_location'] else return nil end
|
38
|
+
end
|
39
|
+
|
40
|
+
def origin_address
|
41
|
+
if status_check then return @legs['start_address'] else return nil end
|
42
|
+
end
|
43
|
+
|
44
|
+
def destination_address
|
45
|
+
if status_check then return @legs['end_address'] else return nil end
|
46
|
+
end
|
47
|
+
|
48
|
+
def origin_coordinates
|
49
|
+
if status_check then return @legs['start_location'] else return nil end
|
50
|
+
end
|
51
|
+
|
52
|
+
|
53
|
+
def duration_as_string
|
54
|
+
if status_check then return @legs['duration']['text'] else return nil end
|
55
|
+
end
|
56
|
+
|
57
|
+
def duration_in_minutes
|
58
|
+
if status_check then return @legs['duration']["value"] else return nil end
|
59
|
+
end
|
60
|
+
|
61
|
+
private
|
62
|
+
def status_check
|
63
|
+
if status != 'OK'
|
64
|
+
return false
|
65
|
+
else
|
66
|
+
return true
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
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
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
@@ -0,0 +1,97 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
describe GoogleMapDirections do
|
3
|
+
|
4
|
+
describe "connecting to the google directions api successfully" do
|
5
|
+
before(:each) do
|
6
|
+
@test = GoogleMapDirections::Directions.new('UC Berkeley, Berkeley, CA', '1600 Amphitheatre Pkwy, Mountain View')
|
7
|
+
sleep(0.1) #make sure you don't spam the api too quickly
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'should successfully create itself' do
|
11
|
+
@test.should_not == nil
|
12
|
+
end
|
13
|
+
it 'should have success code of OK' do
|
14
|
+
@test.status.should == 'OK'
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe "possible bad statuses" do
|
19
|
+
it 'should indicate a start or end point that is not found' do
|
20
|
+
sleep(0.1) #make sure you don't spam the api too quickly
|
21
|
+
GoogleMapDirections::Directions.new('herpderprawrs31415', '1600 Amphitheatre Pkwy, Mountain View').status.should == "NOT_FOUND"
|
22
|
+
end
|
23
|
+
it 'should indicate that a path does not exist' do
|
24
|
+
sleep(0.1) #make sure you don't spam the api too quickly
|
25
|
+
GoogleMapDirections::Directions.new('New York, New York', 'Beijing, China').status.should == "ZERO_RESULTS"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
describe "pulling correct information from api" do
|
30
|
+
before(:all) do
|
31
|
+
@happy_path = GoogleMapDirections::Directions.new('UC Berkeley, Berkeley, CA', '1600 Amphitheatre Pkwy, Mountain View')
|
32
|
+
sleep(0.1) #make sure you don't spam the api too quickly
|
33
|
+
@sad_path = GoogleMapDirections::Directions.new('super nyan cat', 'flying cattle from mars 3125herpderp')
|
34
|
+
sleep(0.1) #make sure you don't spam the api too quickly
|
35
|
+
end
|
36
|
+
it 'should correctly give distances as a string' do
|
37
|
+
@happy_path.distance_as_string.should == "42.3 mi"
|
38
|
+
end
|
39
|
+
it 'should correctly give distances in meters' do
|
40
|
+
@happy_path.distance_in_meters.should == 68130
|
41
|
+
end
|
42
|
+
it 'should correctly give the coordinates of the destination' do
|
43
|
+
@happy_path.destination_coordinates.should == {
|
44
|
+
"lat" => 37.4212053,
|
45
|
+
"lng" => -122.0840357
|
46
|
+
}
|
47
|
+
end
|
48
|
+
it 'should correctly give the coordinates of the origin' do
|
49
|
+
@happy_path.origin_coordinates.should == {
|
50
|
+
"lat" => 37.8688419,
|
51
|
+
"lng" => -122.2582396
|
52
|
+
}
|
53
|
+
end
|
54
|
+
it 'should correctly give the address of the destination' do
|
55
|
+
@happy_path.origin_address.should == "University of California, Berkeley, Berkeley, CA"
|
56
|
+
end
|
57
|
+
it 'should correctly give the address of the origin' do
|
58
|
+
@happy_path.destination_address.should == "1600 Amphitheatre Parkway, Mountain View, CA 94043, USA"
|
59
|
+
|
60
|
+
end
|
61
|
+
it 'should correctly give the duration of the trip as a string' do
|
62
|
+
@happy_path.duration_as_string.should == "55 mins"
|
63
|
+
end
|
64
|
+
it 'should correctly give the duration of the trip in minutes' do
|
65
|
+
@happy_path.duration_in_minutes.should == 3322
|
66
|
+
end
|
67
|
+
it 'should correctly give distances as a string for a bad reply' do
|
68
|
+
@sad_path.distance_as_string.should == nil
|
69
|
+
end
|
70
|
+
it 'should correctly give distances in meters for a bad reply' do
|
71
|
+
@sad_path.distance_in_meters.should == nil
|
72
|
+
end
|
73
|
+
it 'should correctly give the coordinates of the destination for a bad reply' do
|
74
|
+
@sad_path.destination_coordinates.should == nil
|
75
|
+
end
|
76
|
+
it 'should correctly give the coordinates of the origin for a bad reply' do
|
77
|
+
@sad_path.origin_coordinates.should == nil
|
78
|
+
end
|
79
|
+
it 'should correctly give the address of the destination for a bad reply' do
|
80
|
+
@sad_path.origin_address.should == nil
|
81
|
+
end
|
82
|
+
it 'should correctly give the address of the origin for a bad reply' do
|
83
|
+
@sad_path.destination_address.should == nil
|
84
|
+
|
85
|
+
end
|
86
|
+
it 'should correctly give the duration of the trip as a string for a bad reply' do
|
87
|
+
@sad_path.duration_as_string.should == nil
|
88
|
+
end
|
89
|
+
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
|
92
|
+
end
|
93
|
+
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
describe GoogleMapDirections do
|
4
|
+
before(:each) do
|
5
|
+
@test = GoogleMapDirections::Directions.new('UC Berkeley, Berkeley, CA', '1600 Amphitheatre Pkwy, Mountain View')
|
6
|
+
end
|
7
|
+
|
8
|
+
it 'should have a version' do
|
9
|
+
GoogleMapDirections::VERSION.wont_be_nil
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'should successfully create itself' do
|
13
|
+
@test.wont_be_nil
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'should have success code of OK' do
|
17
|
+
@test.status.must_equal('OK')
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'should have success code of OK' do
|
21
|
+
@test.status.must_equal('OK')
|
22
|
+
end
|
23
|
+
|
24
|
+
|
25
|
+
|
26
|
+
end
|
data/test/helper.rb
ADDED
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.
|
4
|
+
version: 0.0.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,16 +9,78 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-12-
|
13
|
-
dependencies:
|
14
|
-
|
15
|
-
|
12
|
+
date: 2013-12-30 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.3'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.3'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rspec
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
description: A wrapper gem for the google directions API
|
63
|
+
email:
|
64
|
+
- chris_h2312@yahoo.com
|
16
65
|
executables: []
|
17
66
|
extensions: []
|
18
67
|
extra_rdoc_files: []
|
19
68
|
files:
|
69
|
+
- .gitignore
|
70
|
+
- .rspec
|
71
|
+
- Gemfile
|
72
|
+
- LICENSE.txt
|
73
|
+
- README.md
|
74
|
+
- Rakefile
|
75
|
+
- google_map_directions.gemspec
|
20
76
|
- lib/google_map_directions.rb
|
21
|
-
|
77
|
+
- lib/google_map_directions/path.rb
|
78
|
+
- lib/google_map_directions/version.rb
|
79
|
+
- spec/direction_spec.rb
|
80
|
+
- spec/spec_helper.rb
|
81
|
+
- test/google_map_directions/google_map_direction_test.rb
|
82
|
+
- test/helper.rb
|
83
|
+
homepage: ''
|
22
84
|
licenses:
|
23
85
|
- MIT
|
24
86
|
post_install_message:
|
@@ -39,8 +101,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
39
101
|
version: '0'
|
40
102
|
requirements: []
|
41
103
|
rubyforge_project:
|
42
|
-
rubygems_version: 1.8.
|
104
|
+
rubygems_version: 1.8.25
|
43
105
|
signing_key:
|
44
106
|
specification_version: 3
|
45
|
-
summary:
|
46
|
-
test_files:
|
107
|
+
summary: I'll update this once I have some working code.
|
108
|
+
test_files:
|
109
|
+
- spec/direction_spec.rb
|
110
|
+
- spec/spec_helper.rb
|
111
|
+
- test/google_map_directions/google_map_direction_test.rb
|
112
|
+
- test/helper.rb
|