google_api_directions 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +23 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +46 -0
- data/Rakefile +10 -0
- data/google_api_directions.gemspec +24 -0
- data/lib/google_api_directions/google_directions.rb +48 -0
- data/lib/google_api_directions/version.rb +3 -0
- data/lib/google_api_directions.rb +6 -0
- data/test/google_api_directions_test.rb +38 -0
- data/test/test_helper.rb +2 -0
- metadata +99 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 6c2ce4804b03002331d072204d71546393d2dead
|
4
|
+
data.tar.gz: 12a5fd78c0aa72fe45f04451b027abce3000e318
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 1a13bfc073f3ad2dd05f1e34f873ccfc0e286a7febad5e3a8086fe2af1ed55122bcc2bba52851e49445cc3c0fa97b31dad28b36268513b662d543149ed1cd0a0
|
7
|
+
data.tar.gz: 7ed64741b2d1135777df6ba3060330c85ebd7ead13c00d5bcb32592987a976b5d341f7000df16c3bbd82fa9d19c12e3edfdf89e93d8a42c2128b53bb6f8e621d
|
data/.gitignore
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
test/tmp
|
16
|
+
test/version_tmp
|
17
|
+
tmp
|
18
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
23
|
+
.idea/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Jone Samra
|
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,46 @@
|
|
1
|
+
# GoogleApiDirections
|
2
|
+
|
3
|
+
A ruby wrapper class for the Google Directions API.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'google_api_directions'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install google_api_directions
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
require "google_api_directions"
|
22
|
+
|
23
|
+
Create a new instance of GoogleDirections.
|
24
|
+
**You can pass the APIKEY to the constructor if you own one (optional).**
|
25
|
+
|
26
|
+
g = GoogleApiDirections::GoogleDirections.new
|
27
|
+
|
28
|
+
Let's get the directions. The first paramameter is the origin, the second one is the destination
|
29
|
+
and the third one is the language to be used for the directions and it's optional.
|
30
|
+
**Watch out couse Exceptions can be thrown here.**
|
31
|
+
|
32
|
+
result = g.directions("Fittjavägen 1 Norsborg", "Slottsbacken 1 Stockholm", "sv")
|
33
|
+
|
34
|
+
Lets get the directions. An array of string will be returned.
|
35
|
+
|
36
|
+
directions_array = result[:directions]
|
37
|
+
|
38
|
+
How about the distance information!? A string will be returned.
|
39
|
+
|
40
|
+
distance = result[:distance]
|
41
|
+
|
42
|
+
And the duration of time? A string will be returned.
|
43
|
+
|
44
|
+
duration = result[:duration]
|
45
|
+
|
46
|
+
|
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_api_directions/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "google_api_directions"
|
8
|
+
spec.version = GoogleApiDirections::VERSION
|
9
|
+
spec.authors = ["Jone Samra"]
|
10
|
+
spec.email = ["jonemob@gmail.com"]
|
11
|
+
spec.summary = %q{A ruby wrapper class for the Google Directions API.}
|
12
|
+
spec.description = %q{A ruby wrapper class for the Google Directions API.}
|
13
|
+
spec.homepage = "https://github.com/phenomen2277/google_api_directions"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.6"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "json", ">= 1.8.1"
|
24
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'open-uri'
|
3
|
+
require 'json'
|
4
|
+
|
5
|
+
module GoogleApiDirections
|
6
|
+
|
7
|
+
class GoogleDirections
|
8
|
+
def initialize(apikey = "")
|
9
|
+
@apikey = ""
|
10
|
+
@apikey = "&key" + apikey unless apikey.empty?
|
11
|
+
|
12
|
+
|
13
|
+
@uri = "http://maps.googleapis.com/maps/api/directions/json?origin="
|
14
|
+
@directions = []
|
15
|
+
end
|
16
|
+
|
17
|
+
def directions(origin, destination, language = "en")
|
18
|
+
raise ArgumentError, "Argument(s) are missing or are empty" if origin.strip.empty? || destination.strip.empty?
|
19
|
+
|
20
|
+
@directions.clear unless @directions.empty?
|
21
|
+
|
22
|
+
@uri = @uri + URI::encode(origin) + "&destination=" + URI::encode(destination) + "&language=" + language + "&sensor=false" + @apikey
|
23
|
+
begin
|
24
|
+
resp = Net::HTTP.get_response(URI.parse(@uri))
|
25
|
+
data = resp.body
|
26
|
+
distance = JSON.parse(data)["routes"][0]["legs"][0]["distance"]["text"]
|
27
|
+
duration = JSON.parse(data)["routes"][0]["legs"][0]["duration"]["text"]
|
28
|
+
result = JSON.parse(data)["routes"][0]["legs"][0]["steps"]
|
29
|
+
rescue Exception => e
|
30
|
+
raise e.message
|
31
|
+
end
|
32
|
+
|
33
|
+
result.each do |direction|
|
34
|
+
text = remove_html_tags(direction["html_instructions"])
|
35
|
+
@directions << text.gsub(" ", " ")
|
36
|
+
end
|
37
|
+
|
38
|
+
{:directions => @directions, :distance => distance, :duration => duration}
|
39
|
+
end
|
40
|
+
|
41
|
+
private
|
42
|
+
def remove_html_tags(text)
|
43
|
+
text.gsub(%r{</?[^>]+?>}, " ")
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class GoogleApiDirectionsTest < Test::Unit::TestCase
|
4
|
+
def test_if_argument_error_exception_is_thrown_when_getting_directions
|
5
|
+
g = GoogleApiDirections::GoogleDirections.new
|
6
|
+
|
7
|
+
assert_raise ArgumentError do
|
8
|
+
g.directions("", "", "en")
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_if_runtime_error_exception_is_thrown_when_getting_directions
|
13
|
+
g = GoogleApiDirections::GoogleDirections.new
|
14
|
+
|
15
|
+
assert_raise RuntimeError do
|
16
|
+
g.directions("?", "2", "en")
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_if_directions_are_valid_after_successful_calling_of_directions_method
|
21
|
+
g = GoogleApiDirections::GoogleDirections.new
|
22
|
+
result = g.directions("Fittjavägen 1 Norsborg", "Slottsbacken 1 Stockholm", "sv")
|
23
|
+
assert result[:directions].any?
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_if_distance_is_present_after_successful_calling_of_directions_method
|
27
|
+
g = GoogleApiDirections::GoogleDirections.new
|
28
|
+
result = g.directions("Fittjavägen 1 Norsborg", "Slottsbacken 1 Stockholm", "sv")
|
29
|
+
assert !result[:directions].nil?
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_if_duration_is_present_after_successful_calling_of_directions_method
|
33
|
+
g = GoogleApiDirections::GoogleDirections.new
|
34
|
+
result = g.directions("Fittjavägen 1 Norsborg", "Slottsbacken 1 Stockholm", "sv")
|
35
|
+
assert !result[:duration].nil?
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: google_api_directions
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jone Samra
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-07-21 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.6'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.6'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: json
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 1.8.1
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 1.8.1
|
55
|
+
description: A ruby wrapper class for the Google Directions API.
|
56
|
+
email:
|
57
|
+
- jonemob@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- Gemfile
|
64
|
+
- LICENSE.txt
|
65
|
+
- README.md
|
66
|
+
- Rakefile
|
67
|
+
- google_api_directions.gemspec
|
68
|
+
- lib/google_api_directions.rb
|
69
|
+
- lib/google_api_directions/google_directions.rb
|
70
|
+
- lib/google_api_directions/version.rb
|
71
|
+
- test/google_api_directions_test.rb
|
72
|
+
- test/test_helper.rb
|
73
|
+
homepage: https://github.com/phenomen2277/google_api_directions
|
74
|
+
licenses:
|
75
|
+
- MIT
|
76
|
+
metadata: {}
|
77
|
+
post_install_message:
|
78
|
+
rdoc_options: []
|
79
|
+
require_paths:
|
80
|
+
- lib
|
81
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
82
|
+
requirements:
|
83
|
+
- - ">="
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
requirements: []
|
92
|
+
rubyforge_project:
|
93
|
+
rubygems_version: 2.4.1
|
94
|
+
signing_key:
|
95
|
+
specification_version: 4
|
96
|
+
summary: A ruby wrapper class for the Google Directions API.
|
97
|
+
test_files:
|
98
|
+
- test/google_api_directions_test.rb
|
99
|
+
- test/test_helper.rb
|