Module: TronprintHelper

Includes:
Tronprint::StatisticsFormatter
Defined in:
lib/tronprint/rails/tronprint_helper.rb

Overview

Rails helper for displaying footprint data.

Instance Method Summary collapse

Methods included from Tronprint::StatisticsFormatter

#pounds_with_precision

Instance Method Details

#cm1_badgeObject

Let the world know that your app is powered by CM1



76
77
78
79
# File 'lib/tronprint/rails/tronprint_helper.rb', line 76

def cm1_badge
  %q{<script type="text/javascript" src="http://impact.brighterplanet.com/badge.js"></script>}.
    html_safe
end

#footprint_badge(options = {}) ⇒ Object

An informational badge displaying total energy, footprint, CO2/minute



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/tronprint/rails/tronprint_helper.rb', line 24

def footprint_badge(options = {})
  text = nil
  if Tronprint.connected?
    begin
      footprint = pounds_with_precision total_estimate

      two_hr_emissions = Tronprint.statistics.
        emission_estimate(Time.now - 7200, Time.now).to_f
      rate = two_hr_emissions / 120  # kgs CO2 per minute over last 2 hours
      rate = rate < 0.0001 ? "< 0.0001" : pounds_with_precision(rate)

      text = <<-HTML
        <p class="tronprint-footprint">
          <span class="tronprint-total-footprint">
            <span class="tronprint-label">Total app footprint:</span>
            <span class="tronprint-value">#{total_electricity.to_i}</span>
            <span class="tronprint-units">W</span>,
            <span class="tronprint-value">#{footprint}</span>
            <span class="tronprint-units">lbs. CO<sub>2</sub>e</span>
          </span>
          <span class="tronprint-separator">&middot;</span>
          <span class="tronprint-current-footprint">
            <span class="tronprint-label">Current footprint:</span>
            <span class="tronprint-value">#{rate}</span>
            <span class="tronprint-units">lbs. CO<sub>2</sub>e/min.</span>
          </span>
          <span class="tronprint-attribution">#{tronprint_attribution if options[:attribution]}</span>
        </p>
      HTML
    rescue Timeout::Error => e
    rescue => e
    end
  end

  text ||= <<-HTML
    <p class="tronprint-footprint">App footprint unavailable</p>
  HTML

  text.html_safe
end

#footprint_methodologyObject

A URL for the methodology statement of the emissions calculation.



19
20
21
# File 'lib/tronprint/rails/tronprint_helper.rb', line 19

def footprint_methodology
  Tronprint.statistics.total_footprint_methodology
end

#total_electricityObject

The total amount of electricity used by the application.



14
15
16
# File 'lib/tronprint/rails/tronprint_helper.rb', line 14

def total_electricity
  total_estimate.electricity_use.to_f
end

#total_estimateObject



81
82
83
# File 'lib/tronprint/rails/tronprint_helper.rb', line 81

def total_estimate
  @total_estimate ||= Tronprint.statistics.emission_estimate
end

#total_footprintObject

The total amount of CO2e generated by the application.



9
10
11
# File 'lib/tronprint/rails/tronprint_helper.rb', line 9

def total_footprint
  total_estimate.to_f
end

#tronprint_attributionObject

Tronprint attribution



66
67
68
# File 'lib/tronprint/rails/tronprint_helper.rb', line 66

def tronprint_attribution
  %q{App footprint calculated by <a href="http://brighterplanet.github.com/tronprint">Tronprint</a>}
end

#tronprint_badgeObject

A link to more information about Tronprint



71
72
73
# File 'lib/tronprint/rails/tronprint_helper.rb', line 71

def tronprint_badge
  %Q{<p class="tronprint-link">#{tronprint_attribution}</p>}.html_safe
end