Permalink
Browse files

initial commit

  • Loading branch information...
0 parents commit 5fddeb4189de6f0a4e770becb505fce7765d070a @YenTheFirst YenTheFirst committed Apr 15, 2011
Showing with 43 additions and 0 deletions.
  1. +3 −0 Gemfile
  2. +35 −0 choices.rb
  3. +5 −0 config.ru
  4. 0 public/index.html
@@ -0,0 +1,3 @@
+source :gemcutter
+gem "sinatra"
+gem "json"
@@ -0,0 +1,35 @@
+require 'net/http'
+
+get "/info_for_route" do
+ base_url="http://maps.googleapis.com/maps/api/directions/json?origin=#{params[:origin]}&destination=#{params[:destination]}&sensor=false"
+ modes=%w{driving walking bicycling}
+ results =modes.map do |mode|
+ response = JSON.parse(Net::HTTP.get(URI.parse(base_url+"&mode=#{mode}")))
+ legs=response["routes"][0]["legs"]
+ info={
+ :distance=>{:unit=>"meters",:value=>legs.inject(0) {|s,x| s+x["distance"]["value"]}},
+ :duration=>{:unit=>"seconds",:value=>legs.inject(0) {|s,x| s+x["duration"]["value"]}}
+ }
+ [mode,info]
+ end
+ results = Hash[*results.flatten]
+
+ results["driving"].merge!({
+ :emmisions=>{:unit=>"bunnies killed",:value=>1},
+ :cost=>{:unit=>"USD",:value=>1}
+ })
+ results["walking"].merge!({
+ :emmisions=>{:unit=>"bunnies killed",:value=>3},
+ :cost=>{:unit=>"USD",:value=>2}
+ })
+ results["bicycling"].merge!({
+ :emmisions=>{:unit=>"bunnies killed",:value=>5},
+ :cost=>{:unit=>"USD",:value=>4}
+ })
+
+ [200,{},results.to_json]
+end
+
+get "/" do
+ File.read(File.join('public', 'index.html'))
+end
@@ -0,0 +1,5 @@
+require 'bundler'
+Bundler.require
+
+require './choices.rb'
+run Sinatra::Application
No changes.

0 comments on commit 5fddeb4

Please sign in to comment.