class Turbovax::Handlers::LocationHandler

Given a list of locations, tweet appointment info for each location

Public Class Methods

new(locations) click to toggle source
# File lib/turbovax/handlers/location_handler.rb, line 9
def initialize(locations)
  @locations = locations
end

Public Instance Methods

appointment_time_format() click to toggle source

Format of each individual appointment time. See APIdoc for format apidock.com/ruby/DateTime/strftime @example Datetime to default time format

Wed, 21 Apr 2021 09:23:15 -0400 => 9:23AM
# File lib/turbovax/handlers/location_handler.rb, line 49
def appointment_time_format
  "%-l:%M%p"
end
daily_appointment_limit() click to toggle source

Max number of appointment times included per day

# File lib/turbovax/handlers/location_handler.rb, line 33
def daily_appointment_limit
  3
end
date_format() click to toggle source

Format of each individual date. See APIdoc for format apidock.com/ruby/DateTime/strftime @example Datetime to default time format

Wed, 21 Apr 2021 09:23:15 -0400 => Apr 21
# File lib/turbovax/handlers/location_handler.rb, line 41
def date_format
  "%b %-e"
end
day_limit() click to toggle source

Max number of days included in a tweet

# File lib/turbovax/handlers/location_handler.rb, line 28
def day_limit
  3
end
execute!() click to toggle source
# File lib/turbovax/handlers/location_handler.rb, line 13
def execute!
  count = 0
  @locations.each do |location|
    next if count >= max_location_limit

    count += 1 if handle_location(location)
  end
end
max_location_limit() click to toggle source

Max locations to tweet at a given time

# File lib/turbovax/handlers/location_handler.rb, line 23
def max_location_limit
  2
end
should_tweet_for_location(location) click to toggle source

@return [Boolean] Override this method to to add caching logic

# File lib/turbovax/handlers/location_handler.rb, line 55
def should_tweet_for_location(location)
  location.available
end

Private Instance Methods

format_appointments(location) click to toggle source
# File lib/turbovax/handlers/location_handler.rb, line 90
def format_appointments(location)
  to_join = []

  appointments_by_day =
    group_appointments_by_day(location.appointments.sort)

  appointments_by_day.each.with_index do |(day, appointments), index|
    next if index >= day_limit

    to_join << format_appointments_for_day(day, appointments)
  end

  to_join.join("\n")
end
format_appointments_for_day(day_string, appointments) click to toggle source
# File lib/turbovax/handlers/location_handler.rb, line 114
def format_appointments_for_day(day_string, appointments)
  use_extra_appointment_count = appointments.size > daily_appointment_limit
  extra_appointment_count = appointments.size - daily_appointment_limit

  sorted_times = appointments.first(daily_appointment_limit).sort.map do |appointment|
    appointment.time_in_time_zone.strftime(appointment_time_format)
  end

  time_string = sorted_times.join(", ")
  time_string += " + #{extra_appointment_count}" if use_extra_appointment_count

  "#{day_string}: #{time_string}"
end
format_tweet(location) click to toggle source
# File lib/turbovax/handlers/location_handler.rb, line 74
def format_tweet(location)
  to_join = []

  portal = location.portal
  appointment_count = location.appointment_count ? "#{location.appointment_count} appts" : nil

  summary_string = "[#{join(portal.name, location.area, delimiter: " ยท ")}] "
  summary_string += join(location.name, appointment_count, delimiter: ": ")
  to_join << summary_string

  to_join << format_appointments(location)
  to_join << portal.public_url

  to_join.join("\n\n")
end
group_appointments_by_day(appointments) click to toggle source
# File lib/turbovax/handlers/location_handler.rb, line 105
def group_appointments_by_day(appointments)
  appointments.each_with_object({}) do |appointment, memo|
    day = appointment.time_in_time_zone.strftime(date_format)

    memo[day] ||= []
    memo[day] << appointment
  end
end
handle_location(location) click to toggle source
# File lib/turbovax/handlers/location_handler.rb, line 61
def handle_location(location)
  return false unless should_tweet_for_location(location)

  text = format_tweet(location)

  send_tweet(text)
  true
end
join(*args, delimiter:) click to toggle source
# File lib/turbovax/handlers/location_handler.rb, line 128
def join(*args, delimiter:)
  args.compact.join(delimiter)
end
send_tweet(text) click to toggle source
# File lib/turbovax/handlers/location_handler.rb, line 70
def send_tweet(text)
  Turbovax::TwitterClient.send_tweet(text)
end