Class: Flycal::SlotFormatter
- Inherits:
-
Object
- Object
- Flycal::SlotFormatter
- Defined in:
- lib/flycal/slot_formatter.rb
Class Method Summary collapse
- .format_header(from:, to:, duration:, calendars:, count:, template: nil) ⇒ Object
-
.format_json(**kwargs) ⇒ Object
Pretty JSON for --format json (EmCP / API friendly).
- .format_output(slots_by_day) ⇒ Object
-
.format_text(slots_by_day:, time_min:, time_max:, duration:, calendars:, template: nil, empty_message: nil) ⇒ Object
Full shell text for slots (header + body).
-
.payload_hash(slots_by_day:, time_min:, time_max:, duration:, template:, calendars:, locale: nil, timezone: nil, calendar_option: nil, from_option: nil, to_option: nil, in_option: nil, free_before: nil, free_after: nil, empty_message: nil) ⇒ Object
Structured hash used by CLI JSON output and HTTP API.
- .strip_ansi(text) ⇒ Object
Class Method Details
.format_header(from:, to:, duration:, calendars:, count:, template: nil) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/flycal/slot_formatter.rb', line 8 def format_header(from:, to:, duration:, calendars:, count:, template: nil) lines = [] lines << Locale.t( "slots.header", count: underline(count), from: underline(Locale.format_long_date(from)), to: underline(Locale.format_long_date(to)), duration: underline(duration), template: underline(template.to_s) ) calendars.each do |cal| lines << "- #{cal[:name]}" end lines << "link: #{google_calendar_day_url(from)}" lines.join("\n") end |
.format_json(**kwargs) ⇒ Object
Pretty JSON for --format json (EmCP / API friendly).
67 68 69 |
# File 'lib/flycal/slot_formatter.rb', line 67 def format_json(**kwargs) JSON.pretty_generate(payload_hash(**kwargs)) + "\n" end |
.format_output(slots_by_day) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/flycal/slot_formatter.rb', line 25 def format_output(slots_by_day) lines = [] slots_by_day.sort_by(&:first).each do |date, slots| lines << "" unless lines.empty? lines << underline(day_header(date)) slots.each do |start_at, end_at| lines << slot_range(start_at, end_at) end end lines.join("\n") end |
.format_text(slots_by_day:, time_min:, time_max:, duration:, calendars:, template: nil, empty_message: nil) ⇒ Object
Full shell text for slots (header + body). Used by CLI and JSON text key.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/flycal/slot_formatter.rb', line 40 def format_text( slots_by_day:, time_min:, time_max:, duration:, calendars:, template: nil, empty_message: nil ) count = slots_by_day.values.sum(&:size) header = format_header( from: time_min, to: time_max, duration: duration, calendars: calendars, count: count, template: template ) body = format_output(slots_by_day) body = .to_s if body.empty? && parts = [ header, "" ] parts << body unless body.to_s.empty? "#{parts.join("\n").rstrip}\n" end |
.payload_hash(slots_by_day:, time_min:, time_max:, duration:, template:, calendars:, locale: nil, timezone: nil, calendar_option: nil, from_option: nil, to_option: nil, in_option: nil, free_before: nil, free_after: nil, empty_message: nil) ⇒ Object
Structured hash used by CLI JSON output and HTTP API.
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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/flycal/slot_formatter.rb', line 72 def payload_hash( slots_by_day:, time_min:, time_max:, duration:, template:, calendars:, locale: nil, timezone: nil, calendar_option: nil, from_option: nil, to_option: nil, in_option: nil, free_before: nil, free_after: nil, empty_message: nil ) items = [] groups = slots_by_day.sort_by(&:first).map do |date, slots| slot_items = slots.map { |start_at, end_at| serialize_slot(start_at, end_at, date) } items.concat(slot_items) { "type" => "day", "key" => date.iso8601, "date" => date.iso8601, "from" => format_iso(slots.map(&:first).min), "to" => format_iso(slots.map(&:last).max), "slots_found" => slot_items.size, "items" => slot_items } end text = format_text( slots_by_day: slots_by_day, time_min: time_min, time_max: time_max, duration: duration, calendars: calendars, template: template, empty_message: ) { "params" => { "command" => "slots", "from" => format_iso(time_min), "to" => format_iso(time_max), "duration" => duration, "template" => template, "calendar" => blank_to_nil(calendar_option), "calendar_ids" => Array(calendars).map { |c| c[:id] }, "format" => "json", "locale" => locale || Locale.current_locale, "timezone" => timezone || TimeFormat.zone_name, "from_option" => blank_to_nil(from_option), "to_option" => blank_to_nil(to_option), "in_option" => blank_to_nil(in_option), "free_before" => free_before, "free_after" => free_after }.compact, "info" => { "slots_found" => items.size, "from" => format_iso(time_min), "to" => format_iso(time_max), "duration" => duration, "template" => template, "timezone" => timezone || TimeFormat.zone_name, "calendars" => Array(calendars).map { |c| { "id" => c[:id], "name" => c[:name] } }, "link" => google_calendar_day_url(time_min) }, "text" => strip_ansi(text).split("\n"), "items" => items, "groups" => groups } end |
.strip_ansi(text) ⇒ Object
148 149 150 |
# File 'lib/flycal/slot_formatter.rb', line 148 def strip_ansi(text) text.to_s.gsub(/\e\[[0-9;]*m/, "") end |