Class: SpreeCmCommissioner::Integrations::Larryta::Resources::Schedule

Inherits:
Base
  • Object
show all
Defined in:
app/services/spree_cm_commissioner/integrations/larryta/resources/schedule.rb

Overview

Represents a trip schedule from Larryta API

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

from_api_response

Constructor Details

#initialize(data) ⇒ Schedule

rubocop:disable Lint/MissingSuper



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/services/spree_cm_commissioner/integrations/larryta/resources/schedule.rb', line 9

def initialize(data) # rubocop:disable Lint/MissingSuper
  @schedule_id = data['schedule_id']
  @sche_date = data['sche_date']
  @sche_time = data['sche_time']
  @bus_type_id = data['bus_type_id']
  @direction_id = data['direction_id']
  @bus_id = data['bus_id']

  prices = data['prices'] || {}
  @unit_price = prices['unit_price']
  @discount = prices['discount']
  @commission = prices['commission']
  @net_price = prices['net_price']
  @currency = prices['currency'] || 'KHR'

  @direction_data = data['direction'] || {}
  @total_unavailable = data['total_unavailable']
  @bus_type_data = data['bus_type'] || {}
  @total_available = data['total_available']
end

Instance Attribute Details

#bus_idObject

Returns the value of attribute bus_id.



4
5
6
# File 'app/services/spree_cm_commissioner/integrations/larryta/resources/schedule.rb', line 4

def bus_id
  @bus_id
end

#bus_type_dataObject (readonly)

Returns the value of attribute bus_type_data.



7
8
9
# File 'app/services/spree_cm_commissioner/integrations/larryta/resources/schedule.rb', line 7

def bus_type_data
  @bus_type_data
end

#bus_type_idObject

Returns the value of attribute bus_type_id.



4
5
6
# File 'app/services/spree_cm_commissioner/integrations/larryta/resources/schedule.rb', line 4

def bus_type_id
  @bus_type_id
end

#commissionObject

Returns the value of attribute commission.



4
5
6
# File 'app/services/spree_cm_commissioner/integrations/larryta/resources/schedule.rb', line 4

def commission
  @commission
end

#currencyObject

Returns the value of attribute currency.



4
5
6
# File 'app/services/spree_cm_commissioner/integrations/larryta/resources/schedule.rb', line 4

def currency
  @currency
end

#direction_dataObject (readonly)

Returns the value of attribute direction_data.



7
8
9
# File 'app/services/spree_cm_commissioner/integrations/larryta/resources/schedule.rb', line 7

def direction_data
  @direction_data
end

#direction_idObject

Returns the value of attribute direction_id.



4
5
6
# File 'app/services/spree_cm_commissioner/integrations/larryta/resources/schedule.rb', line 4

def direction_id
  @direction_id
end

#discountObject

Returns the value of attribute discount.



4
5
6
# File 'app/services/spree_cm_commissioner/integrations/larryta/resources/schedule.rb', line 4

def discount
  @discount
end

#net_priceObject

Returns the value of attribute net_price.



4
5
6
# File 'app/services/spree_cm_commissioner/integrations/larryta/resources/schedule.rb', line 4

def net_price
  @net_price
end

#sche_dateObject

Returns the value of attribute sche_date.



4
5
6
# File 'app/services/spree_cm_commissioner/integrations/larryta/resources/schedule.rb', line 4

def sche_date
  @sche_date
end

#sche_timeObject

Returns the value of attribute sche_time.



4
5
6
# File 'app/services/spree_cm_commissioner/integrations/larryta/resources/schedule.rb', line 4

def sche_time
  @sche_time
end

#schedule_idObject

Returns the value of attribute schedule_id.



4
5
6
# File 'app/services/spree_cm_commissioner/integrations/larryta/resources/schedule.rb', line 4

def schedule_id
  @schedule_id
end

#total_availableObject

Returns the value of attribute total_available.



4
5
6
# File 'app/services/spree_cm_commissioner/integrations/larryta/resources/schedule.rb', line 4

def total_available
  @total_available
end

#total_unavailableObject

Returns the value of attribute total_unavailable.



4
5
6
# File 'app/services/spree_cm_commissioner/integrations/larryta/resources/schedule.rb', line 4

def total_unavailable
  @total_unavailable
end

#unit_priceObject

Returns the value of attribute unit_price.



4
5
6
# File 'app/services/spree_cm_commissioner/integrations/larryta/resources/schedule.rb', line 4

def unit_price
  @unit_price
end

Instance Method Details

#arrival_datetimeObject

Returns the arrival datetime



56
57
58
59
60
61
62
63
# File 'app/services/spree_cm_commissioner/integrations/larryta/resources/schedule.rb', line 56

def arrival_datetime
  return nil unless arrival_time

  # Parse arrival time and add to departure date
  arrival_hour, arrival_minute = arrival_time.split(':').map(&:to_i)
  departure = departure_datetime
  Time.zone.local(departure.year, departure.month, departure.day, arrival_hour, arrival_minute)
end

#arrival_timeObject

Returns the arrival time



51
52
53
# File 'app/services/spree_cm_commissioner/integrations/larryta/resources/schedule.rb', line 51

def arrival_time
  direction_data['arrival_time']
end

#availability_summaryObject

Returns availability summary



117
118
119
120
121
122
123
# File 'app/services/spree_cm_commissioner/integrations/larryta/resources/schedule.rb', line 117

def availability_summary
  {
    total_seats: bus_capacity,
    available: total_available,
    unavailable: total_unavailable
  }
end

#bus_capacityObject

Returns the bus seat capacity



86
87
88
# File 'app/services/spree_cm_commissioner/integrations/larryta/resources/schedule.rb', line 86

def bus_capacity
  bus_type_data['seat_num']
end

#bus_image_urlObject

Returns the bus type image URL



91
92
93
# File 'app/services/spree_cm_commissioner/integrations/larryta/resources/schedule.rb', line 91

def bus_image_url
  bus_type_data['image']
end

#bus_nameObject

Returns the bus name



81
82
83
# File 'app/services/spree_cm_commissioner/integrations/larryta/resources/schedule.rb', line 81

def bus_name
  bus_type_data['bus_name']
end

#dateObject

Returns the departure date



36
37
38
# File 'app/services/spree_cm_commissioner/integrations/larryta/resources/schedule.rb', line 36

def date
  sche_date
end

#departure_datetimeObject

Returns the departure datetime



46
47
48
# File 'app/services/spree_cm_commissioner/integrations/larryta/resources/schedule.rb', line 46

def departure_datetime
  Time.zone.parse("#{date} #{time}")
end

#direction_codeObject

Returns the direction code



66
67
68
# File 'app/services/spree_cm_commissioner/integrations/larryta/resources/schedule.rb', line 66

def direction_code
  direction_data['dire_code']
end

#display_nameObject

Returns the display name for this schedule



101
102
103
# File 'app/services/spree_cm_commissioner/integrations/larryta/resources/schedule.rb', line 101

def display_name
  "#{from_location_name}#{to_location_name} - #{date} #{time}"
end

#from_location_nameObject

Returns the from location name



71
72
73
# File 'app/services/spree_cm_commissioner/integrations/larryta/resources/schedule.rb', line 71

def from_location_name
  direction_data.dig('from_location', 'loca_name')
end

#idObject

Returns the schedule ID



31
32
33
# File 'app/services/spree_cm_commissioner/integrations/larryta/resources/schedule.rb', line 31

def id
  schedule_id
end

#night_bus?Boolean

Returns whether this is a night bus

Returns:

  • (Boolean)


96
97
98
# File 'app/services/spree_cm_commissioner/integrations/larryta/resources/schedule.rb', line 96

def night_bus?
  bus_type_data['is_night_bus'] == 1
end

#pricing_summaryObject

Returns pricing summary



106
107
108
109
110
111
112
113
114
# File 'app/services/spree_cm_commissioner/integrations/larryta/resources/schedule.rb', line 106

def pricing_summary
  {
    unit_price: unit_price,
    discount: discount,
    commission: commission,
    net_price: net_price,
    currency: currency
  }
end

#timeObject

Returns the departure time



41
42
43
# File 'app/services/spree_cm_commissioner/integrations/larryta/resources/schedule.rb', line 41

def time
  sche_time
end

#to_hObject

Returns a hash representation



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'app/services/spree_cm_commissioner/integrations/larryta/resources/schedule.rb', line 126

def to_h
  {
    id: id,
    date: date,
    time: time,
    departure_datetime: departure_datetime,
    arrival_time: arrival_time,
    arrival_datetime: arrival_datetime,
    bus_type_id: bus_type_id,
    direction_id: direction_id,
    bus_id: bus_id,
    direction_code: direction_code,
    from_location_name: from_location_name,
    to_location_name: to_location_name,
    bus_name: bus_name,
    bus_capacity: bus_capacity,
    bus_image_url: bus_image_url,
    night_bus: night_bus?,
    pricing: pricing_summary,
    availability: availability_summary,
    display_name: display_name
  }
end

#to_location_nameObject

Returns the to location name



76
77
78
# File 'app/services/spree_cm_commissioner/integrations/larryta/resources/schedule.rb', line 76

def to_location_name
  direction_data.dig('to_location', 'loca_name')
end