google_directions 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Manifest +5 -0
- data/README.textile +21 -0
- data/Rakefile +15 -0
- data/google_directions.gemspec +33 -0
- data/init.rb +1 -0
- data/lib/google_directions.rb +60 -0
- metadata +94 -0
data/Manifest
ADDED
data/README.textile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
h2. Usage
|
2
|
+
|
3
|
+
|
4
|
+
h2. Installation
|
5
|
+
|
6
|
+
h3. gem
|
7
|
+
# gem install google_directions
|
8
|
+
# add config.gem "google_directions" to your environment.rb file
|
9
|
+
|
10
|
+
Or do Bundler's Gemfile
|
11
|
+
|
12
|
+
h3. Rails 2.3.8 compatible
|
13
|
+
Not yet tested on Rails 3
|
14
|
+
|
15
|
+
h3. Google maps API key
|
16
|
+
|
17
|
+
You'll need a Google Map API key
|
18
|
+
|
19
|
+
http://code.google.com/apis/maps/signup.html
|
20
|
+
|
21
|
+
Include it as the constant GOOGLE_MAPS_API_KEY in an app configuration file (environment.rb, config/initializers/api_keys.rb)
|
data/Rakefile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
# Rakefile
|
2
|
+
require 'rubygems'
|
3
|
+
require 'rake'
|
4
|
+
require 'echoe'
|
5
|
+
|
6
|
+
Echoe.new('google_directions', '0.1.0') do |p|
|
7
|
+
p.description = "Ruby-wrapper for Google Directions API. Can return the drive time and driving distance between to places"
|
8
|
+
p.url = "http://github.com/joshcrews/Google-Directions-Ruby"
|
9
|
+
p.author = "Josh Crews"
|
10
|
+
p.email = "josh@joshcrews.com"
|
11
|
+
p.ignore_pattern = ["tmp/*", "script/*"]
|
12
|
+
p.development_dependencies = ['nokogiri >=1.4.1']
|
13
|
+
end
|
14
|
+
|
15
|
+
Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{google_directions}
|
5
|
+
s.version = "0.1.0"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["Josh Crews"]
|
9
|
+
s.date = %q{2010-07-14}
|
10
|
+
s.description = %q{Ruby-wrapper for Google Directions API. Can return the drive time and driving distance between to places}
|
11
|
+
s.email = %q{josh@joshcrews.com}
|
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"]
|
14
|
+
s.homepage = %q{http://github.com/joshcrews/Google-Directions-Ruby}
|
15
|
+
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Google_directions", "--main", "README.textile"]
|
16
|
+
s.require_paths = ["lib"]
|
17
|
+
s.rubyforge_project = %q{google_directions}
|
18
|
+
s.rubygems_version = %q{1.3.7}
|
19
|
+
s.summary = %q{Ruby-wrapper for Google Directions API. Can return the drive time and driving distance between to places}
|
20
|
+
|
21
|
+
if s.respond_to? :specification_version then
|
22
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
23
|
+
s.specification_version = 3
|
24
|
+
|
25
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
26
|
+
s.add_development_dependency(%q<nokogiri>, [">= 1.4.1"])
|
27
|
+
else
|
28
|
+
s.add_dependency(%q<nokogiri>, [">= 1.4.1"])
|
29
|
+
end
|
30
|
+
else
|
31
|
+
s.add_dependency(%q<nokogiri>, [">= 1.4.1"])
|
32
|
+
end
|
33
|
+
end
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'google_directions'
|
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'nokogiri'
|
3
|
+
require 'google_directions'
|
4
|
+
|
5
|
+
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
|
+
end
|
12
|
+
|
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
|
+
|
16
|
+
def xml
|
17
|
+
full_url = xml_call
|
18
|
+
Rails.logger.info "Hit Google maps: #{full_url}"
|
19
|
+
@xml = get_url(full_url)
|
20
|
+
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
|
29
|
+
doc = Nokogiri::XML(@xml)
|
30
|
+
raise "Google returned nothing" if doc.nil?
|
31
|
+
debugger if drive_time = doc.css("duration value").last.nil?
|
32
|
+
drive_time = doc.css("duration value").last.text
|
33
|
+
Rails.logger.info "Hit Google maps: drive time: #{drive_time}"
|
34
|
+
convert_to_minutes(drive_time)
|
35
|
+
end
|
36
|
+
|
37
|
+
def distance_in_miles
|
38
|
+
@xml ||= xml
|
39
|
+
doc = Nokogiri::XML(xml)
|
40
|
+
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
|
44
|
+
end
|
45
|
+
|
46
|
+
private
|
47
|
+
|
48
|
+
def convert_to_minutes(text)
|
49
|
+
(text.to_i / 60).ceil
|
50
|
+
end
|
51
|
+
|
52
|
+
def transcribe(location)
|
53
|
+
location.gsub(" ", "+")
|
54
|
+
end
|
55
|
+
|
56
|
+
def get_url(url)
|
57
|
+
Net::HTTP.get(::URI.parse(url))
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
metadata
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: google_directions
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Josh Crews
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-07-14 00:00:00 -05:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: nokogiri
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 5
|
30
|
+
segments:
|
31
|
+
- 1
|
32
|
+
- 4
|
33
|
+
- 1
|
34
|
+
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
|
38
|
+
email: josh@joshcrews.com
|
39
|
+
executables: []
|
40
|
+
|
41
|
+
extensions: []
|
42
|
+
|
43
|
+
extra_rdoc_files:
|
44
|
+
- README.textile
|
45
|
+
- lib/google_directions.rb
|
46
|
+
files:
|
47
|
+
- Manifest
|
48
|
+
- README.textile
|
49
|
+
- Rakefile
|
50
|
+
- init.rb
|
51
|
+
- lib/google_directions.rb
|
52
|
+
- google_directions.gemspec
|
53
|
+
has_rdoc: true
|
54
|
+
homepage: http://github.com/joshcrews/Google-Directions-Ruby
|
55
|
+
licenses: []
|
56
|
+
|
57
|
+
post_install_message:
|
58
|
+
rdoc_options:
|
59
|
+
- --line-numbers
|
60
|
+
- --inline-source
|
61
|
+
- --title
|
62
|
+
- Google_directions
|
63
|
+
- --main
|
64
|
+
- README.textile
|
65
|
+
require_paths:
|
66
|
+
- lib
|
67
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
68
|
+
none: false
|
69
|
+
requirements:
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
hash: 3
|
73
|
+
segments:
|
74
|
+
- 0
|
75
|
+
version: "0"
|
76
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
77
|
+
none: false
|
78
|
+
requirements:
|
79
|
+
- - ">="
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
hash: 11
|
82
|
+
segments:
|
83
|
+
- 1
|
84
|
+
- 2
|
85
|
+
version: "1.2"
|
86
|
+
requirements: []
|
87
|
+
|
88
|
+
rubyforge_project: google_directions
|
89
|
+
rubygems_version: 1.3.7
|
90
|
+
signing_key:
|
91
|
+
specification_version: 3
|
92
|
+
summary: Ruby-wrapper for Google Directions API. Can return the drive time and driving distance between to places
|
93
|
+
test_files: []
|
94
|
+
|