Class: JekyllStats::Formatter
- Inherits:
-
Object
- Object
- JekyllStats::Formatter
- Defined in:
- lib/jekyll-stats/formatter.rb
Instance Attribute Summary collapse
-
#stats ⇒ Object
readonly
Returns the value of attribute stats.
Instance Method Summary collapse
- #averages_line ⇒ Object
- #categories_line ⇒ Object
- #date_range_line ⇒ Object
- #format_number(n) ⇒ Object
- #format_reading_time(minutes) ⇒ Object
- #frequency_line ⇒ Object
-
#initialize(stats) ⇒ Formatter
constructor
A new instance of Formatter.
- #post_summary ⇒ Object
- #posts_by_year_chart ⇒ Object
- #to_terminal ⇒ Object
- #top_tags ⇒ Object
- #truncate(str, length) ⇒ Object
Constructor Details
#initialize(stats) ⇒ Formatter
Returns a new instance of Formatter.
7 8 9 |
# File 'lib/jekyll-stats/formatter.rb', line 7 def initialize(stats) @stats = stats end |
Instance Attribute Details
#stats ⇒ Object (readonly)
Returns the value of attribute stats.
5 6 7 |
# File 'lib/jekyll-stats/formatter.rb', line 5 def stats @stats end |
Instance Method Details
#averages_line ⇒ Object
55 56 57 58 59 |
# File 'lib/jekyll-stats/formatter.rb', line 55 def averages_line avg = stats[:average_words] longest = stats[:longest_post] "Avg: #{avg} words | Longest: \"#{truncate(longest[:title], 30)}\" (#{format_number(longest[:words])} words)" end |
#categories_line ⇒ Object
96 97 98 99 100 |
# File 'lib/jekyll-stats/formatter.rb', line 96 def categories_line cats = stats[:categories] cat_strs = cats.map { |c| "#{c[:name]} (#{c[:count]})" } "Categories:\n #{cat_strs.join(" | ")}" end |
#date_range_line ⇒ Object
61 62 63 64 65 66 |
# File 'lib/jekyll-stats/formatter.rb', line 61 def date_range_line first = stats[:first_post][:date] last = stats[:last_post][:date] years = stats[:years_active] "First: #{first} | Last: #{last} (#{years} years)" end |
#format_number(n) ⇒ Object
102 103 104 |
# File 'lib/jekyll-stats/formatter.rb', line 102 def format_number(n) n.to_s.reverse.gsub(/(\d{3})(?=\d)/, '\\1,').reverse end |
#format_reading_time(minutes) ⇒ Object
106 107 108 109 110 111 112 113 114 |
# File 'lib/jekyll-stats/formatter.rb', line 106 def format_reading_time(minutes) if minutes >= 60 hours = minutes / 60 mins = minutes % 60 "#{hours}h #{mins}m" else "#{minutes}m" end end |
#frequency_line ⇒ Object
68 69 70 |
# File 'lib/jekyll-stats/formatter.rb', line 68 def frequency_line "Frequency: #{stats[:posts_per_month]} posts/month" end |
#post_summary ⇒ Object
48 49 50 51 52 53 |
# File 'lib/jekyll-stats/formatter.rb', line 48 def post_summary total = stats[:total_posts] words = format_number(stats[:total_words]) time = format_reading_time(stats[:reading_minutes]) "Posts: #{total} (#{words} words, ~#{time} read time)" end |
#posts_by_year_chart ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/jekyll-stats/formatter.rb', line 72 def posts_by_year_chart years = stats[:posts_by_year] return "" if years.empty? max_count = years.map { |y| y[:count] }.max = 20 lines = ["Posts by Year:"] years.first(10).each do |year_data| year = year_data[:year] count = year_data[:count] = ((count.to_f / max_count) * ).round = "\u2588" * lines << " #{year}: #{bar} #{count}" end lines.join("\n") end |
#to_terminal ⇒ Object
11 12 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 41 42 43 44 45 46 |
# File 'lib/jekyll-stats/formatter.rb', line 11 def to_terminal return "No posts found." if stats[:total_posts].zero? lines = [] lines << "" lines << "\u{1F4CA} Site Statistics" lines << "\u2500" * 35 lines << post_summary lines << averages_line lines << date_range_line lines << frequency_line lines << "" lines << posts_by_year_chart if stats[:tags].any? lines << "" lines << end if stats[:categories].any? lines << "" lines << categories_line end if stats[:drafts_count].positive? lines << "" lines << "Drafts: #{stats[:drafts_count]}" end lines << "\u2500" * 35 lines << "" lines.join("\n") end |
#top_tags ⇒ Object
90 91 92 93 94 |
# File 'lib/jekyll-stats/formatter.rb', line 90 def = stats[:tags].first(10) tag_strs = .map { |t| "#{t[:name]} (#{t[:count]})" } "Top #{[10, stats[:tags].size].min} Tags:\n #{tag_strs.join(" | ")}" end |
#truncate(str, length) ⇒ Object
116 117 118 119 120 |
# File 'lib/jekyll-stats/formatter.rb', line 116 def truncate(str, length) return str if str.length <= length "#{str[0, length]}..." end |