Module: Tricle::EmailHelper

Includes:
ActiveSupport::Inflector
Defined in:
lib/tricle/email_helper.rb

Instance Method Summary collapse

Instance Method Details

#add_periods(time, n) ⇒ Object



41
42
43
44
45
46
47
48
49
50
# File 'lib/tricle/email_helper.rb', line 41

def add_periods(time, n)
  case period
  when :day
    time.advance(days: n)
  when :week
    time.advance(weeks: n)
  when :month
    time.advance(months: n)
  end
end

#current_dates_cellObject



143
144
145
146
147
148
# File 'lib/tricle/email_helper.rb', line 143

def current_dates_cell
  dates_cell(
    periods_ago(1),
    if period == :day then nil else end_of_period(periods_ago(1)) end
  )
end

#current_dates_cell_headerObject



172
173
174
175
176
177
178
179
180
181
# File 'lib/tricle/email_helper.rb', line 172

def current_dates_cell_header
  case period
  when :day
    'Yesterday'
  when :week
    'Last week'
  when :month
    'Last month'
  end
end

#current_number(metric) ⇒ Object



197
198
199
200
# File 'lib/tricle/email_helper.rb', line 197

def current_number(metric)
  format_number metric.periods_ago(period, 1),
                metric.unit
end

#dates_cell(start_at, end_at) ⇒ Object



111
112
113
114
# File 'lib/tricle/email_helper.rb', line 111

def dates_cell(start_at, end_at)
  range = dates_range_str(start_at, end_at)
  %[<div class="date-range">(#{range})</div>].html_safe
end

#dates_range_str(start_at, end_at) ⇒ Object



103
104
105
106
107
108
109
# File 'lib/tricle/email_helper.rb', line 103

def dates_range_str(start_at, end_at)
  if end_at
    "#{ self.format_date(start_at) } - #{ self.format_date(end_at) }"
  else
    self.format_date(start_at)
  end
end

#end_of_period(beginning) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/tricle/email_helper.rb', line 30

def end_of_period(beginning)
  case period
  when :day
    beginning.end_of_day
  when :week
    beginning.end_of_week
  when :month
    beginning.end_of_month
  end
end

#format_date(date) ⇒ Object



52
53
54
# File 'lib/tricle/email_helper.rb', line 52

def format_date(date)
  date.strftime('%-m/%-d/%y')
end

#format_number(number, unit = nil) ⇒ Object



56
57
58
59
# File 'lib/tricle/email_helper.rb', line 56

def format_number(number, unit = nil)
  number_with_delimiter(if number.abs >= 100 then number.round else sig_figs(number) end) +
  (if unit then ' ' + unit.pluralize(number.abs) else '' end)
end

#list_markup(list) ⇒ Object



202
203
204
205
206
# File 'lib/tricle/email_helper.rb', line 202

def list_markup(list)
  start_at = self.periods_ago(1).to_time
  end_at = add_periods(start_at, 1)
  list.items_markup(start_at, end_at).html_safe
end

#num_durationsObject



8
9
10
11
12
13
14
15
16
17
# File 'lib/tricle/email_helper.rb', line 8

def num_durations
  case period
  when :day
    7 # week
  when :week
    13 # quarter
  when :month
    12 # year
  end
end

#number_with_delimiter(number) ⇒ Object



61
62
63
64
65
# File 'lib/tricle/email_helper.rb', line 61

def number_with_delimiter(number)
  # from http://stackoverflow.com/a/11466770/358804
  integer, decimal = number.to_s.split(".")
  [integer.to_s.reverse.gsub(/(\d{3})(?=\d)/, '\\1,').reverse, decimal].compact.join('.')
end

#old_data_cell(metric) ⇒ Object



183
184
185
186
187
188
# File 'lib/tricle/email_helper.rb', line 183

def old_data_cell(metric)
  percent_change_cell metric.periods_ago(period, 1),
                      metric.range_average(period, num_durations),
                      metric.better,
                      metric.unit
end

#old_dates_cellObject



132
133
134
# File 'lib/tricle/email_helper.rb', line 132

def old_dates_cell
  dates_cell periods_ago(num_durations), end_of_period(periods_ago(1))
end

#old_dates_cell_headerObject



150
151
152
153
154
155
156
157
158
159
# File 'lib/tricle/email_helper.rb', line 150

def old_dates_cell_header
  case period
  when :day
    'Weekly average'
  when :week
    'Quarterly average'
  when :month
    'Yearly average'
  end
end

#percent_change(new_val, old_val) ⇒ Object



74
75
76
77
78
79
80
81
82
83
# File 'lib/tricle/email_helper.rb', line 74

def percent_change(new_val, old_val)
  if old_val == new_val
    'No change'
  elsif old_val == 0
    new_val >= 0 ? '+' : '-'
  else
    fraction = (new_val - old_val) / old_val.to_f
    (fraction >= 0 ? '+' : '').concat("#{sig_figs(fraction * 100.0)}%")
  end
end

#percent_change_cell(new_val, old_val, better, unit) ⇒ Object



96
97
98
99
100
101
# File 'lib/tricle/email_helper.rb', line 96

def percent_change_cell(new_val, old_val, better, unit)
  cls = self.percent_change_class(new_val, old_val, better)
  pct_str = percent_change(new_val, old_val)
  old_val_str = format_number(old_val, unit)
  %[<td class="#{cls}"><div>#{pct_str}</div><div>#{old_val_str}</div></td>].html_safe
end

#percent_change_class(new_val, old_val, better) ⇒ Object



85
86
87
88
89
90
91
92
93
94
# File 'lib/tricle/email_helper.rb', line 85

def percent_change_class(new_val, old_val, better)
  case better
  when :higher
    (new_val >= old_val) ? 'good' : 'bad'
  when :lower
    (new_val > old_val) ? 'bad' : 'good'
  else
    ''
  end
end

#periodObject



128
129
130
# File 'lib/tricle/email_helper.rb', line 128

def period
  self.mailer.period
end

#periods_ago(n) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/tricle/email_helper.rb', line 19

def periods_ago(n)
  case period
  when :day
    Date.today.beginning_of_day.ago(n.days)
  when :week
    Date.today.beginning_of_week.ago(n.weeks)
  when :month
    Date.today.beginning_of_month.advance(months: -n) # for old ActiveSupport
  end
end

#previous_data_cell(metric) ⇒ Object



190
191
192
193
194
195
# File 'lib/tricle/email_helper.rb', line 190

def previous_data_cell(metric)
  percent_change_cell metric.periods_ago(period, 1),
                      metric.periods_ago(period, 2),
                      metric.better,
                      metric.unit
end

#previous_dates_cellObject



136
137
138
139
140
141
# File 'lib/tricle/email_helper.rb', line 136

def previous_dates_cell
  dates_cell(
    periods_ago(2),
    if period == :day then nil else end_of_period(periods_ago(2)) end
  )
end

#previous_dates_cell_headerObject



161
162
163
164
165
166
167
168
169
170
# File 'lib/tricle/email_helper.rb', line 161

def previous_dates_cell_header
  case period
  when :day
    '2 days ago'
  when :week
    'Previous week'
  when :month
    'Previous month'
  end
end

#sig_figs(number, num_sig_figs = 3) ⇒ Object



67
68
69
70
71
72
# File 'lib/tricle/email_helper.rb', line 67

def sig_figs(number, num_sig_figs = 3)
  # http://six-impossible.blogspot.com/2011/05/significant-digits-in-ruby-float.html
  f = sprintf("%.#{num_sig_figs - 1}e", number).to_f
  i = f.to_i # avoid
  i == f && i.to_s.size > num_sig_figs ? i : f
end

#single_day_dates_cell(start_at) ⇒ Object



116
117
118
# File 'lib/tricle/email_helper.rb', line 116

def single_day_dates_cell(start_at)
  dates_cell(start_at, nil)
end

#single_month_dates_cell(start_at) ⇒ Object



124
125
126
# File 'lib/tricle/email_helper.rb', line 124

def single_month_dates_cell(start_at)
  dates_cell(start_at, start_at.end_of_month)
end

#single_week_dates_cell(start_at) ⇒ Object



120
121
122
# File 'lib/tricle/email_helper.rb', line 120

def single_week_dates_cell(start_at)
  dates_cell(start_at, start_at.end_of_week)
end

#sparkline(metric) ⇒ Object



208
209
210
211
212
# File 'lib/tricle/email_helper.rb', line 208

def sparkline(metric)
  values = metric.range_values(period, num_durations)
  attachment_url = "https://sparklines.herokuapp.com/api/v1.png?values=#{values.join(',')}"
  image_tag(attachment_url, alt: 'sparkline').html_safe
end