Class: TimeEstimationService

Inherits:
ApplicationService show all
Defined in:
app/services/time_estimation_service.rb

Overview

Service class that estimates the time required to deliver an order from a restaurant to a destination.

Instance Attribute Summary

Attributes inherited from ApplicationService

#object

Instance Method Summary collapse

Methods inherited from ApplicationService

#execute, #execute!

Constructor Details

#initialize(order_type, restaurant:, destination:) ⇒ TimeEstimationService

Initializes a new TimeEstimationService object.

Parameters:

  • order_type (Symbol)

    the type of order (:delivery or :pickup)

  • restaurant (DriverData, Restaurant)

    the location of the restaurant or driver, passed as either a DriverData or Restaurant instance

  • destination (Hash)

    a hash with keys :lat and :lng representing the latitude and longitude of the destination



9
10
11
12
13
14
# File 'app/services/time_estimation_service.rb', line 9

def initialize(order_type, restaurant:, destination:)
  @destination = destination
  @order_type = order_type.to_sym
  @restaurant = restaurant
  @cooking_estimation = 0
end

Instance Method Details

#resultHash

Calculates the time required to deliver the order from the restaurant to the destination.

Returns:

  • (Hash)

    a hash with keys :success, :data, and :message containing the result of - the time estimation calculation



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/services/time_estimation_service.rb', line 27

def result
  return non_delivery_result unless @order_type == :delivery

  calculator = DeliveryChannel::DistanceCalculator.new
  calculator_result = calculator.find_distance_to_restaurant(@destination, @restaurant)
  if calculator_result[:success]
    {
      success: calculator_result[:success],
      data: {
        time_estimation: @cooking_estimation + calculator_result[:duration].to_i,
      },
      message: '',
    }
  else
    {
      success: false,
      data: {},
      message: calculator_result[:message],
    }
  end
end

#set_cooking_estimation(cooking_estimation) ⇒ Object

Sets the estimated time required to cook the order.

Parameters:

  • cooking_estimation (Integer)

    the estimated time in seconds



19
20
21
# File 'app/services/time_estimation_service.rb', line 19

def set_cooking_estimation(cooking_estimation)
  @cooking_estimation = cooking_estimation
end