Class: LanguageOperator::CLI::Helpers::ScheduleBuilder
- Inherits:
-
Object
- Object
- LanguageOperator::CLI::Helpers::ScheduleBuilder
- Defined in:
- lib/language_operator/cli/helpers/schedule_builder.rb
Overview
Helper for building schedules from natural language inputs
Class Method Summary collapse
-
.cron_to_human(cron_expr) ⇒ Object
Convert cron expression to human-readable format.
-
.daily_cron(time_string) ⇒ Object
Build a cron expression for daily execution at a specific time.
-
.interval_cron(interval, unit) ⇒ Object
Build a cron expression for interval-based execution.
-
.parse_time(input) ⇒ Object
Parse natural language time input and return 24-hour format Examples: "4pm" -> "16:00", "9:30am" -> "09:30", "16:00" -> "16:00".
Class Method Details
.cron_to_human(cron_expr) ⇒ Object
Convert cron expression to human-readable format
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/language_operator/cli/helpers/schedule_builder.rb', line 65 def cron_to_human(cron_expr) parts = cron_expr.split return cron_expr if parts.length != 5 minute, hour, day, month, weekday = parts # Daily at specific time if minute =~ /^\d+$/ && hour =~ /^\d+$/ && day == '*' && month == '*' && weekday == '*' time_str = format_time(hour.to_i, minute.to_i) return "Daily at #{time_str}" end # Every N minutes if minute.start_with?('*/') && hour == '*' interval = minute[2..].to_i return "Every #{interval} minute#{'s' if interval > 1}" end # Every N hours if minute == '0' && hour.start_with?('*/') interval = hour[2..].to_i return "Every #{interval} hour#{'s' if interval > 1}" end # Every N days if minute == '0' && hour == '0' && day.start_with?('*/') interval = day[2..].to_i return "Every #{interval} day#{'s' if interval > 1}" end # Fallback to cron expression cron_expr end |
.daily_cron(time_string) ⇒ Object
Build a cron expression for daily execution at a specific time
43 44 45 46 |
# File 'lib/language_operator/cli/helpers/schedule_builder.rb', line 43 def daily_cron(time_string) hours, minutes = time_string.split(':').map(&:to_i) "#{minutes} #{hours} * * *" end |
.interval_cron(interval, unit) ⇒ Object
Build a cron expression for interval-based execution
49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/language_operator/cli/helpers/schedule_builder.rb', line 49 def interval_cron(interval, unit) validate_interval(interval, unit) case unit.downcase when 'minutes', 'minute' "*/#{interval} * * * *" when 'hours', 'hour' "0 */#{interval} * * *" when 'days', 'day' "0 0 */#{interval} * *" else raise ArgumentError, "Invalid unit: #{unit}" end end |
.parse_time(input) ⇒ Object
Parse natural language time input and return 24-hour format Examples: "4pm" -> "16:00", "9:30am" -> "09:30", "16:00" -> "16:00"
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/language_operator/cli/helpers/schedule_builder.rb', line 13 def parse_time(input) input = input.strip.downcase # Handle 24-hour format (e.g., "16:00", "9:30") if input.match?(/^\d{1,2}:\d{2}$/) hours, minutes = input.split(':').map(&:to_i) return format_time(hours, minutes) if valid_time?(hours, minutes) raise ArgumentError, "Invalid time: #{input}" end # Handle 12-hour format with am/pm (e.g., "4pm", "9:30am") match = input.match(/^(\d{1,2})(?::(\d{2}))?\s*(am|pm)$/i) raise ArgumentError, "Invalid time format: #{input}" unless match hours = match[1].to_i minutes = match[2].to_i period = match[3].downcase # Convert to 24-hour format hours = 0 if hours == 12 && period == 'am' hours = 12 if hours == 12 && period == 'pm' hours += 12 if period == 'pm' && hours != 12 raise ArgumentError, "Invalid time: #{input}" unless valid_time?(hours, minutes) format_time(hours, minutes) end |