diff --git a/app/decorators/api/v01/destinations_decorator.rb b/app/decorators/api/v01/destinations_decorator.rb deleted file mode 100644 index 32a3fb2..0000000 --- a/app/decorators/api/v01/destinations_decorator.rb +++ /dev/null @@ -1,43 +0,0 @@ -# Copyright © Mapotempo, 2015-2016 -# -# This file is part of Mapotempo. -# -# Mapotempo is free software. You can redistribute it and/or -# modify since you respect the terms of the GNU Affero General -# Public License as published by the Free Software Foundation, -# either version 3 of the License, or (at your option) any later version. -# -# Mapotempo is distributed in the hope that it will be useful, but WITHOUT -# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY -# or FITNESS FOR A PARTICULAR PURPOSE. See the Licenses for more details. -# -# You should have received a copy of the GNU Affero General Public License -# along with Mapotempo. If not, see: -# -# -require_relative '../../../api/v01/entities/destination_id' - -V01::Destinations.class_eval do - desc 'Fetch customer\'s destinations inside time/distance.', - nickname: 'getDestinationsInsideTimeAndDistance', - is_array: true, - entity: V01::Entities::DestinationId - params do - requires :lat, type: Float, desc: 'Point latitude.' - requires :lng, type: Float, desc: 'Point longitude.' - optional :vehicle_usage_id, type: Integer, desc: 'Vehicle Usage uses in place of default router and speed multiplicator.' - optional :distance, type: Integer, desc: 'Maximum distance in meter.' - optional :time, type: Integer, desc: 'Maximum time in seconds.' - at_least_one_of :time, :distance - end - get :destinations_by_time_and_distance do - position = OpenStruct.new(lat: Float(params[:lat]), lng: Float(params[:lng])) - vehicle_usage = VehicleUsage.joins(:vehicle_usage_set).where(vehicle_usage_sets: {customer_id: current_customer.id}, id: params[:vehicle_usage_id]).first - if params.key?(:vehicle_usage_id) && vehicle_usage.nil? - error! 'VehicleUsage not found', 404 - else - destinations = current_customer.destinations_inside_time_distance(position, params[:distance], params[:time], vehicle_usage) || [] - present destinations, with: V01::Entities::DestinationId - end - end -end diff --git a/app/decorators/api/v01/stores_decorator.rb b/app/decorators/api/v01/stores_decorator.rb deleted file mode 100644 index 7dde807..0000000 --- a/app/decorators/api/v01/stores_decorator.rb +++ /dev/null @@ -1,39 +0,0 @@ -# Copyright © Mapotempo, 2015-2016 -# -# This file is part of Mapotempo. -# -# Mapotempo is free software. You can redistribute it and/or -# modify since you respect the terms of the GNU Affero General -# Public License as published by the Free Software Foundation, -# either version 3 of the License, or (at your option) any later version. -# -# Mapotempo is distributed in the hope that it will be useful, but WITHOUT -# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY -# or FITNESS FOR A PARTICULAR PURPOSE. See the Licenses for more details. -# -# You should have received a copy of the GNU Affero General Public License -# along with Mapotempo. If not, see: -# -# -V01::Stores.class_eval do - desc 'Fetch customer\'s stores by distance.', - nickname: 'getStoresByDistance', - is_array: true, - entity: V01::Entities::Store - params do - requires :lat, type: Float, desc: 'Point latitude.' - requires :lng, type: Float, desc: 'Point longitude.' - requires :n, type: Integer, desc: 'Number of results.' - optional :vehicle_usage_id, type: Integer, desc: 'Vehicle Usage uses in place of default router and speed multiplicator.' - end - get :stores_by_distance do - position = OpenStruct.new(lat: Float(params[:lat]), lng: Float(params[:lng])) - vehicle_usage = VehicleUsage.joins(:vehicle_usage_set).where(vehicle_usage_sets: {customer_id: current_customer.id}, id: params[:vehicle_usage_id]).first - if params.key?(:vehicle_usage_id) && vehicle_usage.nil? - error! 'VehicleUsage not found', 404 - else - stores = current_customer.stores_by_distance(position, Integer(params[:n]), vehicle_usage) - present stores, with: V01::Entities::Store - end - end -end diff --git a/app/decorators/controllers/.keep b/app/decorators/controllers/.keep deleted file mode 100644 index e69de29..0000000 diff --git a/app/decorators/models/.keep b/app/decorators/models/.keep deleted file mode 100644 index e69de29..0000000 diff --git a/app/decorators/models/customer_decorator.rb b/app/decorators/models/customer_decorator.rb deleted file mode 100644 index ab9461b..0000000 --- a/app/decorators/models/customer_decorator.rb +++ /dev/null @@ -1,50 +0,0 @@ -# Copyright © Mapotempo, 2015-2016 -# -# This file is part of Mapotempo. -# -# Mapotempo is free software. You can redistribute it and/or -# modify since you respect the terms of the GNU Affero General -# Public License as published by the Free Software Foundation, -# either version 3 of the License, or (at your option) any later version. -# -# Mapotempo is distributed in the hope that it will be useful, but WITHOUT -# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY -# or FITNESS FOR A PARTICULAR PURPOSE. See the Licenses for more details. -# -# You should have received a copy of the GNU Affero General Public License -# along with Mapotempo. If not, see: -# -# -Customer.class_eval do - def stores_by_distance(position, n, vehicle_usage = nil, &matrix_progress) - starts = [[position.lat, position.lng]] - dests = self.stores.select{ |store| !store.lat.nil? && !store.lng.nil? }.collect{ |store| [store.lat, store.lng] } - r = (vehicle_usage && vehicle_usage.vehicle.default_router) || router - d = (vehicle_usage && vehicle_usage.vehicle.default_router_dimension) || router_dimension - options = (vehicle_usage && vehicle_usage.vehicle.default_router_options || router_options).symbolize_keys - options[:geometry] = false - options[:speed_multiplier] = (vehicle_usage && vehicle_usage.vehicle.default_speed_multiplier) || speed_multiplier || 1 - - distances = r.matrix(starts, dests, :distance, options, &matrix_progress)[0] - stores.select{ |store, distance| !distance.nil? }.zip(distances).sort_by{ |store, distance| - distance - }[0..[n, stores.size].min - 1].collect{ |store, distance| store } - end - - def destinations_inside_time_distance(position, distance, time, vehicle_usage = nil, &matrix_progress) - starts = [[position.lat, position.lng]] - dest_with_pos = self.destinations.select{ |d| !d.lat.nil? && !d.lng.nil? } - dests = dest_with_pos.collect{ |d| [d.lat, d.lng] } - r = (vehicle_usage && vehicle_usage.vehicle.default_router) || router - d = (vehicle_usage && vehicle_usage.vehicle.default_router_dimension) || router_dimension - options = (vehicle_usage && vehicle_usage.vehicle.default_router_options || router_options).symbolize_keys - options[:geometry] = false - options[:speed_multiplier] = (vehicle_usage && vehicle_usage.vehicle.default_speed_multiplier) || speed_multiplier || 1 - - distances = !distance.nil? && r.distance? ? r.matrix(starts, dests, :distance, options, &matrix_progress)[0] : [] - times = !time.nil? && r.time? ? r.matrix(starts, dests, :time, options, &matrix_progress)[0] : [] - dest_with_pos.zip(distances, times).select{ |dest, dist, t| - (!dist || dist[0] <= distance) && (!t || t[0] <= time) - }.collect{ |dest, d, t| dest } - end -end diff --git a/config/initializers/api/v01/destinations_decorator.rb b/config/initializers/api/v01/destinations_decorator.rb new file mode 100644 index 0000000..1accadb --- /dev/null +++ b/config/initializers/api/v01/destinations_decorator.rb @@ -0,0 +1,44 @@ +# Copyright © Mapotempo, 2015-2016 +# +# This file is part of Mapotempo. +# +# Mapotempo is free software. You can redistribute it and/or +# modify since you respect the terms of the GNU Affero General +# Public License as published by the Free Software Foundation, +# either version 3 of the License, or (at your option) any later version. +# +# Mapotempo is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY +# or FITNESS FOR A PARTICULAR PURPOSE. See the Licenses for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with Mapotempo. If not, see: +# +# + +Rails.application.config.to_prepare do + V01::Destinations.class_eval do + desc 'Fetch customer\'s destinations inside time/distance.', + nickname: 'getDestinationsInsideTimeAndDistance', + is_array: true, + entity: V01::Entities::DestinationId + params do + requires :lat, type: Float, desc: 'Point latitude.' + requires :lng, type: Float, desc: 'Point longitude.' + optional :vehicle_usage_id, type: Integer, desc: 'Vehicle Usage uses in place of default router and speed multiplicator.' + optional :distance, type: Integer, desc: 'Maximum distance in meter.' + optional :time, type: Integer, desc: 'Maximum time in seconds.' + at_least_one_of :time, :distance + end + get :destinations_by_time_and_distance do + position = OpenStruct.new(lat: Float(params[:lat]), lng: Float(params[:lng])) + vehicle_usage = VehicleUsage.joins(:vehicle_usage_set).where(vehicle_usage_sets: {customer_id: current_customer.id}, id: params[:vehicle_usage_id]).first + if params.key?(:vehicle_usage_id) && vehicle_usage.nil? + error! 'VehicleUsage not found', 404 + else + destinations = current_customer.destinations_inside_time_distance(position, params[:distance], params[:time], vehicle_usage) || [] + present destinations, with: V01::Entities::DestinationId + end + end + end +end diff --git a/config/initializers/api/v01/stores_decorator.rb b/config/initializers/api/v01/stores_decorator.rb new file mode 100644 index 0000000..a01e70d --- /dev/null +++ b/config/initializers/api/v01/stores_decorator.rb @@ -0,0 +1,42 @@ +# Copyright © Mapotempo, 2015-2016 +# +# This file is part of Mapotempo. +# +# Mapotempo is free software. You can redistribute it and/or +# modify since you respect the terms of the GNU Affero General +# Public License as published by the Free Software Foundation, +# either version 3 of the License, or (at your option) any later version. +# +# Mapotempo is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY +# or FITNESS FOR A PARTICULAR PURPOSE. See the Licenses for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with Mapotempo. If not, see: +# +# + +Rails.application.config.to_prepare do + V01::Stores.class_eval do + desc 'Fetch customer\'s stores by distance.', + nickname: 'getStoresByDistance', + is_array: true, + entity: V01::Entities::Store + params do + requires :lat, type: Float, desc: 'Point latitude.' + requires :lng, type: Float, desc: 'Point longitude.' + requires :n, type: Integer, desc: 'Number of results.' + optional :vehicle_usage_id, type: Integer, desc: 'Vehicle Usage uses in place of default router and speed multiplicator.' + end + get :stores_by_distance do + position = OpenStruct.new(lat: Float(params[:lat]), lng: Float(params[:lng])) + vehicle_usage = VehicleUsage.joins(:vehicle_usage_set).where(vehicle_usage_sets: {customer_id: current_customer.id}, id: params[:vehicle_usage_id]).first + if params.key?(:vehicle_usage_id) && vehicle_usage.nil? + error! 'VehicleUsage not found', 404 + else + stores = current_customer.stores_by_distance(position, Integer(params[:n]), vehicle_usage) + present stores, with: V01::Entities::Store + end + end + end +end diff --git a/app/decorators/controllers/api_web/v01/stores_controller_decorator.rb b/config/initializers/controllers/api_web/v01/stores_controller_decorator.rb similarity index 70% rename from app/decorators/controllers/api_web/v01/stores_controller_decorator.rb rename to config/initializers/controllers/api_web/v01/stores_controller_decorator.rb index 5955863..f253677 100644 --- a/app/decorators/controllers/api_web/v01/stores_controller_decorator.rb +++ b/config/initializers/controllers/api_web/v01/stores_controller_decorator.rb @@ -17,10 +17,12 @@ # require 'ostruct' -ApiWeb::V01::StoresController.class_eval do - def by_distance - @customer = current_user.customer - @position = OpenStruct.new(lat: Float(params[:lat]), lng: Float(params[:lng])) - @stores = @customer.stores_by_distance(@position, Integer(params[:n])) +Rails.application.config.to_prepare do + ApiWeb::V01::StoresController.class_eval do + def by_distance + @customer = current_user.customer + @position = OpenStruct.new(lat: Float(params[:lat]), lng: Float(params[:lng])) + @stores = @customer.stores_by_distance(@position, Integer(params[:n])) + end end end diff --git a/config/initializers/models/customer_decorator.rb b/config/initializers/models/customer_decorator.rb new file mode 100644 index 0000000..a1ee789 --- /dev/null +++ b/config/initializers/models/customer_decorator.rb @@ -0,0 +1,53 @@ +# Copyright © Mapotempo, 2015-2016 +# +# This file is part of Mapotempo. +# +# Mapotempo is free software. You can redistribute it and/or +# modify since you respect the terms of the GNU Affero General +# Public License as published by the Free Software Foundation, +# either version 3 of the License, or (at your option) any later version. +# +# Mapotempo is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY +# or FITNESS FOR A PARTICULAR PURPOSE. See the Licenses for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with Mapotempo. If not, see: +# +# + +Rails.application.config.to_prepare do + Customer.class_eval do + def stores_by_distance(position, n, vehicle_usage = nil, &matrix_progress) + starts = [[position.lat, position.lng]] + dests = self.stores.select{ |store| !store.lat.nil? && !store.lng.nil? }.collect{ |store| [store.lat, store.lng] } + r = (vehicle_usage && vehicle_usage.vehicle.default_router) || router + d = (vehicle_usage && vehicle_usage.vehicle.default_router_dimension) || router_dimension + options = (vehicle_usage && vehicle_usage.vehicle.default_router_options || router_options).symbolize_keys + options[:geometry] = false + options[:speed_multiplier] = (vehicle_usage && vehicle_usage.vehicle.default_speed_multiplier) || speed_multiplier || 1 + + distances = r.matrix(starts, dests, :distance, options, &matrix_progress)[0] + stores.select{ |store, distance| !distance.nil? }.zip(distances).sort_by{ |store, distance| + distance + }[0..[n, stores.size].min - 1].collect{ |store, distance| store } + end + + def destinations_inside_time_distance(position, distance, time, vehicle_usage = nil, &matrix_progress) + starts = [[position.lat, position.lng]] + dest_with_pos = self.destinations.select{ |d| !d.lat.nil? && !d.lng.nil? } + dests = dest_with_pos.collect{ |d| [d.lat, d.lng] } + r = (vehicle_usage && vehicle_usage.vehicle.default_router) || router + d = (vehicle_usage && vehicle_usage.vehicle.default_router_dimension) || router_dimension + options = (vehicle_usage && vehicle_usage.vehicle.default_router_options || router_options).symbolize_keys + options[:geometry] = false + options[:speed_multiplier] = (vehicle_usage && vehicle_usage.vehicle.default_speed_multiplier) || speed_multiplier || 1 + + distances = !distance.nil? && r.distance? ? r.matrix(starts, dests, :distance, options, &matrix_progress)[0] : [] + times = !time.nil? && r.time? ? r.matrix(starts, dests, :time, options, &matrix_progress)[0] : [] + dest_with_pos.zip(distances, times).select{ |dest, dist, t| + (!dist || dist[0] <= distance) && (!t || t[0] <= time) + }.collect{ |dest, d, t| dest } + end + end +end