Class: SpreeCmCommissioner::Integrations::Larryta::Resources::SeatLayout

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

Overview

SeatLayout resource wraps Larryta bus template API response

This class provides methods to extract and transform seat layout data from the Larryta API into a format suitable for BookMe+ internal models.

Larryta API Structure:

data: {
  bus_template: {
    bus_type_id: 3,
    bus_name: "Minivan 15 Seats",
    seat_num: 15,
    seat_map: [
      [  # Row 0
        {label: "DR_IMG", class: "available", booking: []},
        {label: "", class: "available", booking: []},
        {label: 1, class: "available", booking: []},
        {label: 2, class: "available", booking: []}
      ],
      [  # Row 1
        {label: 3, class: "available", booking: []},
        ...
      ]
    ]
  }
}

Visual Layout Example (5 rows × 4 columns with gaps):

Row 0: [DR_IMG] []    [1]   [2]
Row 1: [3]      [4]   [5]   []
Row 2: [6]      [7]   []    [8]
Row 3: [9]      [10]  []    [11]
Row 4: [12]     [13]  [14]  [15]

Constant Summary collapse

BLOCK_WIDTH =

Layout rendering constants

50
BLOCK_HEIGHT =
50
BLOCK_GAP =
10
DEFAULT_CANVAS_WIDTH =
400
DEFAULT_CANVAS_HEIGHT =
400

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

from_api_response, #to_h

Constructor Details

#initialize(data) ⇒ SeatLayout

Initialize seat layout from Larryta API response

Example:

data = {
  'schedule_id' => 626511,
  'sche_date' => '2026-04-02',
  'sche_time' => '15:30',
  'bus_template' => {
    'bus_type_id' => 3,
    'bus_name' => 'Minivan 15 Seats',
    'seat_num' => 15,
    'seat_map' => [[...], [...]]
  }
}
seat_layout = SeatLayout.new(data)

Parameters:

  • data (Hash)

    API response data



64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'app/services/spree_cm_commissioner/integrations/larryta/resources/seat_layout.rb', line 64

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']
  @is_bind = data['is_bind']

  bus_template = data['bus_template'] || {}
  @bus_name = bus_template['bus_name']
  @seat_num = bus_template['seat_num']
  @seat_map = bus_template['seat_map'] || []
end

Instance Attribute Details

#bus_idObject

Returns the value of attribute bus_id.



43
44
45
# File 'app/services/spree_cm_commissioner/integrations/larryta/resources/seat_layout.rb', line 43

def bus_id
  @bus_id
end

#bus_nameObject

Returns the value of attribute bus_name.



43
44
45
# File 'app/services/spree_cm_commissioner/integrations/larryta/resources/seat_layout.rb', line 43

def bus_name
  @bus_name
end

#bus_type_idObject

Returns the value of attribute bus_type_id.



43
44
45
# File 'app/services/spree_cm_commissioner/integrations/larryta/resources/seat_layout.rb', line 43

def bus_type_id
  @bus_type_id
end

#direction_idObject

Returns the value of attribute direction_id.



43
44
45
# File 'app/services/spree_cm_commissioner/integrations/larryta/resources/seat_layout.rb', line 43

def direction_id
  @direction_id
end

#is_bindObject

Returns the value of attribute is_bind.



43
44
45
# File 'app/services/spree_cm_commissioner/integrations/larryta/resources/seat_layout.rb', line 43

def is_bind
  @is_bind
end

#sche_dateObject

Returns the value of attribute sche_date.



43
44
45
# File 'app/services/spree_cm_commissioner/integrations/larryta/resources/seat_layout.rb', line 43

def sche_date
  @sche_date
end

#sche_timeObject

Returns the value of attribute sche_time.



43
44
45
# File 'app/services/spree_cm_commissioner/integrations/larryta/resources/seat_layout.rb', line 43

def sche_time
  @sche_time
end

#schedule_idObject

Returns the value of attribute schedule_id.



43
44
45
# File 'app/services/spree_cm_commissioner/integrations/larryta/resources/seat_layout.rb', line 43

def schedule_id
  @schedule_id
end

#seat_mapObject

Returns the value of attribute seat_map.



43
44
45
# File 'app/services/spree_cm_commissioner/integrations/larryta/resources/seat_layout.rb', line 43

def seat_map
  @seat_map
end

#seat_numObject

Returns the value of attribute seat_num.



43
44
45
# File 'app/services/spree_cm_commissioner/integrations/larryta/resources/seat_layout.rb', line 43

def seat_num
  @seat_num
end

Instance Method Details

#available_seats_countInteger

Get count of available seats (class = ‘available’ and no bookings)

Returns:

  • (Integer)

    Number of available seats



110
111
112
# File 'app/services/spree_cm_commissioner/integrations/larryta/resources/seat_layout.rb', line 110

def available_seats_count
  seats.count(&:available?)
end

#booked_seats_countInteger

Get count of booked seats (has bookings in the booking array)

Returns:

  • (Integer)

    Number of booked seats



118
119
120
# File 'app/services/spree_cm_commissioner/integrations/larryta/resources/seat_layout.rb', line 118

def booked_seats_count
  seats.count(&:booked?)
end

#column_countInteger

Get the number of columns in the layout

Returns:

  • (Integer)

    Maximum number of columns across all rows



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

def column_count
  seat_map.map(&:size).max || 0
end

#row_countInteger

Get the number of rows in the layout

Returns:

  • (Integer)

    Number of rows in the seat map



94
95
96
# File 'app/services/spree_cm_commissioner/integrations/larryta/resources/seat_layout.rb', line 94

def row_count
  seat_map.size
end

#seatsArray<Seat>

Get all seats from the seat map as a flat array

Extracts all seat objects from the 2D seat_map, skipping empty cells. Each seat is enriched with row and column position.

Returns:

  • (Array<Seat>)

    Array of seat objects with position data



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

def seats
  @seats ||= extract_seats_from_map
end

#skip_seat?(seat_data) ⇒ Boolean

Check if a seat map cell should be skipped (aisle/empty space)

Parameters:

  • seat_data (Hash)

    Raw seat data from API

Returns:

  • (Boolean)

    true if this cell should be skipped



156
157
158
159
160
161
# File 'app/services/spree_cm_commissioner/integrations/larryta/resources/seat_layout.rb', line 156

def skip_seat?(seat_data)
  return true if seat_data.nil?
  return true if seat_data['label'].nil? || seat_data['label'] == ''

  false
end

#to_internal_blocks_attributes(seat_layout) ⇒ Array<Hash>

Convert Larryta seat layout to BookMe+ Block attributes

Transforms the 2D seat layout into an array of block hashes ready for creating SpreeCmCommissioner::Block records. Each seat becomes a block with calculated x,y coordinates based on its row/column position.

Coordinate Calculation:

x = column_index * (block_width + gap)
y = row_index * (block_height + gap)

Parameters:

Returns:

  • (Array<Hash>)

    Array of block attribute hashes



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'app/services/spree_cm_commissioner/integrations/larryta/resources/seat_layout.rb', line 135

def to_internal_blocks_attributes(seat_layout)
  blocks = []
  offsets = calculate_centering_offsets(seat_layout)

  seat_map.each_with_index do |row, row_idx|
    row.each_with_index do |seat_data, col_idx|
      next if skip_seat?(seat_data)

      seat = Seat.new(seat_data.merge('row' => row_idx, 'column' => col_idx))
      blocks << build_block_attributes(seat, row_idx, col_idx, offsets, seat_layout)
    end
  end

  blocks
end