Permalink
Browse files

prep for github

  • Loading branch information...
0 parents commit c618e3eed481952c4f0f3cfef141e1736818d1a1 Josh Crews committed Jul 14, 2010
Showing with 156 additions and 0 deletions.
  1. +5 −0 Manifest
  2. +42 −0 README.textile
  3. +15 −0 Rakefile
  4. BIN google_directions-0.1.0.gem
  5. +33 −0 google_directions.gemspec
  6. +1 −0 init.rb
  7. +60 −0 lib/google_directions.rb
@@ -0,0 +1,5 @@
+Manifest
+README.textile
+Rakefile
+init.rb
+lib/google_directions.rb
@@ -0,0 +1,42 @@
+h2. Usage
+
+Get a google maps API key, and store in your app under GOOGLE_MAPS_API_KEY
+<pre>
+ GOOGLE_MAPS_API_KEY = 'your_api_key'
+</pre>
+
+<pre>
+directions = GoogleDirections.new(origin, destination)
+</pre>
+where _origin_ and _destination_ are strings of addresses or places that Google can find an address for. Example: "816 Meridian St., 37207"
+
+<pre>
+drive_time_in_minutes = directions.drive_time_in_minutes
+distance_in_miles = directions.distance_in_miles
+</pre>
+Call these two methods to get integers back for the entire drive time of the trip in minutes and distance in miles
+
+<pre>
+xml = directions.xml
+xml_call = directions.xml_call
+</pre>
+Call these if you want the XML Google returns containing all the driving steps or you want to see the URL
+
+h2. Installation
+
+h3. gem
+# gem install google_directions
+# add config.gem "google_directions" to your environment.rb file
+
+Or do Bundler's Gemfile
+
+h3. Rails 2.3.8 compatible
+Not yet tested on Rails 3. It probably is Rails 3 compatible, because it's just a single class with a few methods. It's probably compatible with every ruby project ever.
+
+h3. Google maps API key
+
+You'll need a Google Map API key
+
+http://code.google.com/apis/maps/signup.html
+
+Include it as the constant GOOGLE_MAPS_API_KEY in an app configuration file (environment.rb, config/initializers/api_keys.rb)
@@ -0,0 +1,15 @@
+# Rakefile
+require 'rubygems'
+require 'rake'
+require 'echoe'
+
+Echoe.new('google_directions', '0.1.0') do |p|
+ p.description = "Ruby-wrapper for Google Directions API. Can return the drive time and driving distance between to places"
+ p.url = "http://github.com/joshcrews/Google-Directions-Ruby"
+ p.author = "Josh Crews"
+ p.email = "josh@joshcrews.com"
+ p.ignore_pattern = ["tmp/*", "script/*"]
+ p.development_dependencies = ['nokogiri >=1.4.1']
+end
+
+Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
Binary file not shown.
@@ -0,0 +1,33 @@
+# -*- encoding: utf-8 -*-
+
+Gem::Specification.new do |s|
+ s.name = %q{google_directions}
+ s.version = "0.1.0"
+
+ s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
+ s.authors = ["Josh Crews"]
+ s.date = %q{2010-07-14}
+ s.description = %q{Ruby-wrapper for Google Directions API. Can return the drive time and driving distance between to places}
+ s.email = %q{josh@joshcrews.com}
+ s.extra_rdoc_files = ["README.textile", "lib/google_directions.rb"]
+ s.files = ["Manifest", "README.textile", "Rakefile", "init.rb", "lib/google_directions.rb", "google_directions.gemspec"]
+ s.homepage = %q{http://github.com/joshcrews/Google-Directions-Ruby}
+ s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Google_directions", "--main", "README.textile"]
+ s.require_paths = ["lib"]
+ s.rubyforge_project = %q{google_directions}
+ s.rubygems_version = %q{1.3.7}
+ s.summary = %q{Ruby-wrapper for Google Directions API. Can return the drive time and driving distance between to places}
+
+ if s.respond_to? :specification_version then
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
+ s.specification_version = 3
+
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
+ s.add_development_dependency(%q<nokogiri>, [">= 1.4.1"])
+ else
+ s.add_dependency(%q<nokogiri>, [">= 1.4.1"])
+ end
+ else
+ s.add_dependency(%q<nokogiri>, [">= 1.4.1"])
+ end
+end
@@ -0,0 +1 @@
+require 'google_directions'
@@ -0,0 +1,60 @@
+require 'net/http'
+require 'nokogiri'
+require 'google_directions'
+
+class GoogleDirections
+
+ def initialize(location_1, location_2)
+ @base_url = "http://maps.google.com/maps/api/directions/xml?key=#{GOOGLE_MAPS_API_KEY}&sensor=false&"
+ @location_1 = location_1
+ @location_2 = location_2
+ end
+
+ # an example URL to be generated
+ #http://maps.google.com/maps/api/directions/xml?origin=St.+Louis,+MO&destination=Nashville,+TN&sensor=false&key=ABQIAAAAINgf4OmAIbIdWblvypOUhxSQ8yY-fgrep0oj4uKpavE300Q6ExQlxB7SCyrAg2evsxwAsak4D0Liiv
+
+ def xml
+ full_url = xml_call
+ Rails.logger.info "Hit Google maps: #{full_url}"
+ @xml = get_url(full_url)
+ end
+
+ def xml_call
+ options = "origin=#{transcribe(@location_1)}&destination=#{transcribe(@location_2)}"
+ full_url = @base_url + options
+ end
+
+ def drive_time_in_minutes
+ @xml ||= xml
+ doc = Nokogiri::XML(@xml)
+ raise "Google returned nothing" if doc.nil?
+ debugger if drive_time = doc.css("duration value").last.nil?
+ drive_time = doc.css("duration value").last.text
+ Rails.logger.info "Hit Google maps: drive time: #{drive_time}"
+ convert_to_minutes(drive_time)
+ end
+
+ def distance_in_miles
+ @xml ||= xml
+ doc = Nokogiri::XML(xml)
+ meters = doc.css("distance value").last.text
+ miles = (meters.to_f / 1610.22).round
+ Rails.logger.info "Hit Google maps: #{miles} miles"
+ miles
+ end
+
+ private
+
+ def convert_to_minutes(text)
+ (text.to_i / 60).ceil
+ end
+
+ def transcribe(location)
+ location.gsub(" ", "+")
+ end
+
+ def get_url(url)
+ Net::HTTP.get(::URI.parse(url))
+ end
+
+end

0 comments on commit c618e3e

Please sign in to comment.