Skip to content
This repository has been archived by the owner on Jun 8, 2021. It is now read-only.

Commit

Permalink
requests now contain origin and destination point, price for ExpressT…
Browse files Browse the repository at this point in the history
…axi is being calculated now
  • Loading branch information
DanielVartanov committed Jan 14, 2012
1 parent 12a5cbf commit 7ce24b7
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 20 deletions.
20 changes: 6 additions & 14 deletions application.rb
Expand Up @@ -15,9 +15,7 @@
Slim::Engine.default_options[:disable_escape] = true

ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Base.establish_connection :adapter => 'sqlite3', :database => 'zones.db', :pool => 25

zones = Zone.all(:include => :vertices)
ActiveRecord::Base.establish_connection :adapter => 'sqlite3', :database => 'express-taxi.db', :pool => 25

get '/' do
slim :index
Expand All @@ -27,16 +25,10 @@
slim :mobile, :layout => false
end

get '/:lat,:lng' do
point = Geometry::Point.new(params[:lat].to_f, params[:lng].to_f)

matching_zone = zones.find { |zone|
zone.to_polygon.contains?(point)
}
matching_zone.name
end

get '/:distance' do
get '/prices' do
distance = params[:distance].to_f
prices(distance).to_json
origin = Geometry::Point.new(params[:origin][:lat].to_f, params[:origin][:lng].to_f)
destination = Geometry::Point.new(params[:destination][:lat].to_f, params[:destination][:lng].to_f)

prices(distance, origin, destination).to_json
end
1 change: 1 addition & 0 deletions kaisy_taxi.rb
@@ -1,3 +1,4 @@
require_relative "lib/prices"
require_relative "lib/zone"
require_relative "lib/vertex"
require_relative "lib/price"
17 changes: 14 additions & 3 deletions lib/prices.rb
Expand Up @@ -5,12 +5,23 @@ def alpha_price(distance)
end

def express_price(origin, destination)
100
zones = Zone.all(:include => :vertices)

origin_zone = zones.find { |zone|
zone.to_polygon.contains?(origin)
}

destination_zone = zones.find { |zone|
zone.to_polygon.contains?(destination)
}

puts "Price.from(#{origin_zone.name}).to(#{destination_zone.name})"
Price.between(origin_zone, destination_zone).value
end

def prices(distance)
def prices(distance, origin, destination)
{
:alpha => alpha_price(distance),
:express => express_price(nil, nil)
:express => express_price(origin, destination)
}
end
7 changes: 4 additions & 3 deletions public/index.js
Expand Up @@ -32,14 +32,15 @@ function drawRoute() {
if (status == google.maps.DirectionsStatus.OK) {
var leg = response.routes[0].legs[0];
directionsDisplay.setDirections(response);
calculateRouteCost(leg.distance.value);
calculateRouteCost(leg.distance.value, route[0], route[1]);
}
});
}

function calculateRouteCost(distance) {
function calculateRouteCost(distance, origin, destination) {
var request = new XMLHttpRequest();
request.open("GET", distance / 1000, false);
url_string = "prices?distance=" + distance / 1000 + "&origin[lat]=" + origin.lat() + "&origin[lng]=" + origin.lng() + "&destination[lat]=" + destination.lat() + "&destination[lng]=" + destination.lng()
request.open("GET", url_string, false);
request.send(null);
alert(request.responseText);
return distance;
Expand Down
6 changes: 6 additions & 0 deletions schema.rb
Expand Up @@ -8,4 +8,10 @@
create_table "zones", :force => true do |t|
t.string "name"
end

create_table "prices", :forct => true do |t|
t.integer "from_id"
t.integer "to_id"
t.integer "value"
end
end

0 comments on commit 7ce24b7

Please sign in to comment.