Class: PgReports::Report
- Inherits:
-
Object
- Object
- PgReports::Report
- Includes:
- Enumerable
- Defined in:
- lib/pg_reports/report.rb
Overview
Report class that wraps query results and provides display/send methods Every module method returns a Report instance for chaining
Instance Attribute Summary collapse
-
#columns ⇒ Object
readonly
Returns the value of attribute columns.
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#generated_at ⇒ Object
readonly
Returns the value of attribute generated_at.
-
#title ⇒ Object
readonly
Returns the value of attribute title.
Instance Method Summary collapse
-
#display ⇒ Object
Display report to STDOUT.
-
#each(&block) ⇒ Object
Iterate over rows.
-
#empty? ⇒ Boolean
Check if report is empty.
-
#initialize(title:, data:, columns: nil) ⇒ Report
constructor
A new instance of Report.
-
#send_to_telegram ⇒ Object
Send report to configured Telegram channel as a message.
-
#send_to_telegram_as_file(filename: nil) ⇒ Object
Send report to configured Telegram channel as a file.
-
#size ⇒ Object
(also: #length, #count)
Get row count.
-
#to_a ⇒ Object
Get raw data as array of hashes.
-
#to_csv ⇒ Object
Return CSV representation.
-
#to_html ⇒ Object
Return HTML representation.
-
#to_markdown ⇒ Object
Return Markdown representation.
-
#to_text ⇒ Object
Return plain text representation.
Constructor Details
#initialize(title:, data:, columns: nil) ⇒ Report
Returns a new instance of Report.
11 12 13 14 15 16 |
# File 'lib/pg_reports/report.rb', line 11 def initialize(title:, data:, columns: nil) @title = title @data = data @columns = columns || detect_columns @generated_at = Time.current end |
Instance Attribute Details
#columns ⇒ Object (readonly)
Returns the value of attribute columns.
9 10 11 |
# File 'lib/pg_reports/report.rb', line 9 def columns @columns end |
#data ⇒ Object (readonly)
Returns the value of attribute data.
9 10 11 |
# File 'lib/pg_reports/report.rb', line 9 def data @data end |
#generated_at ⇒ Object (readonly)
Returns the value of attribute generated_at.
9 10 11 |
# File 'lib/pg_reports/report.rb', line 9 def generated_at @generated_at end |
#title ⇒ Object (readonly)
Returns the value of attribute title.
9 10 11 |
# File 'lib/pg_reports/report.rb', line 9 def title @title end |
Instance Method Details
#display ⇒ Object
Display report to STDOUT
19 20 21 |
# File 'lib/pg_reports/report.rb', line 19 def display puts to_text end |
#each(&block) ⇒ Object
Iterate over rows
111 112 113 |
# File 'lib/pg_reports/report.rb', line 111 def each(&block) data.each(&block) end |
#empty? ⇒ Boolean
Check if report is empty
98 99 100 |
# File 'lib/pg_reports/report.rb', line 98 def empty? data.empty? end |
#send_to_telegram ⇒ Object
Send report to configured Telegram channel as a message
24 25 26 27 |
# File 'lib/pg_reports/report.rb', line 24 def send_to_telegram TelegramSender.(to_markdown) self end |
#send_to_telegram_as_file(filename: nil) ⇒ Object
Send report to configured Telegram channel as a file
30 31 32 33 34 |
# File 'lib/pg_reports/report.rb', line 30 def send_to_telegram_as_file(filename: nil) filename ||= "#{title.parameterize}-#{generated_at.strftime("%Y%m%d-%H%M%S")}.txt" TelegramSender.send_file(to_text, filename: filename, caption: title) self end |
#size ⇒ Object Also known as: length, count
Get row count
103 104 105 |
# File 'lib/pg_reports/report.rb', line 103 def size data.size end |
#to_a ⇒ Object
Get raw data as array of hashes
93 94 95 |
# File 'lib/pg_reports/report.rb', line 93 def to_a data end |
#to_csv ⇒ Object
Return CSV representation
81 82 83 84 85 86 87 88 89 90 |
# File 'lib/pg_reports/report.rb', line 81 def to_csv require "csv" CSV.generate do |csv| csv << columns data.each do |row| csv << columns.map { |col| row[col] } end end end |
#to_html ⇒ Object
Return HTML representation
68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/pg_reports/report.rb', line 68 def to_html return empty_report_html if data.empty? lines = [] lines << "<h2>#{CGI.escapeHTML(title)}</h2>" lines << "<p><em>Generated: #{generated_at.strftime("%Y-%m-%d %H:%M:%S")}</em></p>" lines << format_table_html lines << "<p><em>Total: #{data.size} rows</em></p>" lines.join("\n") end |
#to_markdown ⇒ Object
Return Markdown representation
53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/pg_reports/report.rb', line 53 def to_markdown return empty_report_markdown if data.empty? lines = [] lines << "**#{title}**" lines << "_Generated: #{generated_at.strftime("%Y-%m-%d %H:%M:%S")}_" lines << "" lines << format_table_markdown lines << "" lines << "_Total: #{data.size} rows_" lines.join("\n") end |
#to_text ⇒ Object
Return plain text representation
37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/pg_reports/report.rb', line 37 def to_text return empty_report_text if data.empty? lines = [] lines << title lines << "=" * title.length lines << "Generated: #{generated_at.strftime("%Y-%m-%d %H:%M:%S")}" lines << "" lines << format_table_text lines << "" lines << "Total: #{data.size} rows" lines.join("\n") end |