Class: AvailableTime::TimeSlot

Inherits:
Object
  • Object
show all
Defined in:
lib/available_time.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(start_time, end_time, interval: 30.minutes, rest: 0.minutes) ⇒ TimeSlot

Returns a new instance of TimeSlot.



8
9
10
11
12
13
# File 'lib/available_time.rb', line 8

def initialize(start_time,end_time, interval: 30.minutes,rest: 0.minutes)
	@start_time = parse(start_time).to_i
	@end_time   = parse(end_time).to_i
	@interval = interval.to_i
	@rest = rest.to_i
end

Instance Attribute Details

#end_timeObject

Returns the value of attribute end_time.



6
7
8
# File 'lib/available_time.rb', line 6

def end_time
  @end_time
end

#intervalObject

Returns the value of attribute interval.



6
7
8
# File 'lib/available_time.rb', line 6

def interval
  @interval
end

#restObject

Returns the value of attribute rest.



6
7
8
# File 'lib/available_time.rb', line 6

def rest
  @rest
end

#start_timeObject

Returns the value of attribute start_time.



6
7
8
# File 'lib/available_time.rb', line 6

def start_time
  @start_time
end

Instance Method Details

#free_slots(appointments = []) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/available_time.rb', line 15

def free_slots(appointments=[])
   free_slots = []
   curr_time = start_time 

   while curr_time <= end_time - @interval ## Assuming last available time slot should not start after 4:30Pm
     free_start_time = curr_time
     free_end_time = free_start_time + @interval
     status, conflict_end_time = check_availbility(free_start_time, free_end_time, appointments)
     if status
       free_slots << [Time.at(free_start_time).strftime("%I:%M %p"), Time.at(free_end_time).strftime("%I:%M %p")]
       curr_time = free_end_time + @rest
     else
       curr_time = conflict_end_time[1] + @rest
     end
   end
   return free_slots
end