Permalink
Browse files

requests now contain origin and destination point, price for ExpressT…

…axi is being calculated now
  • Loading branch information...
1 parent 12a5cbf commit 7ce24b723ec15c27194ca6302a26676c680cb1d8 @DanielVartanov committed Jan 14, 2012
Showing with 31 additions and 20 deletions.
  1. +6 −14 application.rb
  2. +1 −0 kaisy_taxi.rb
  3. +14 −3 lib/prices.rb
  4. +4 −3 public/index.js
  5. +6 −0 schema.rb
View
@@ -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
@@ -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
View
@@ -1,3 +1,4 @@
require_relative "lib/prices"
require_relative "lib/zone"
require_relative "lib/vertex"
+require_relative "lib/price"
View
@@ -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
View
@@ -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;
View
@@ -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.