-
Notifications
You must be signed in to change notification settings - Fork 2
/
emitter.rb
170 lines (152 loc) · 4.12 KB
/
emitter.rb
1
2
3
4
5
6
7
8
9
10
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
require 'uri'
require 'net/http'
require 'conversions'
require 'cache_method'
module Carbon
class Shell
# @private
class Emitter < Bombshell::Environment
class << self
# @private
def characteristics(emitter)
::MultiJson.load ::Net::HTTP.get(::URI.parse("http://impact.brighterplanet.com/#{emitter.underscore.pluralize}/options.json"))
rescue
# oops
end
cache_method :characteristics, 300
end
include Bombshell::Shell
include Carbon
# @private
def initialize(name, input = {})
@emitter = name.to_s.singularize.camelcase
@input = input
if characteristics = Emitter.characteristics(@emitter)
characteristics.each do |characteristic|
instance_eval <<-meth
def #{characteristic}(arg = nil)
if arg
@input[:#{characteristic}] = arg.to_s.strip
emission
else
@input[:#{characteristic}]
end
end
meth
end
provisions = characteristics.map { |k| "provide :#{k}"}.join('; ')
emit_as_block = "emit_as(:#{name}) { #{provisions} }"
self.class.class_eval emit_as_block
emission
else
puts " => Sorry, characteristics couldn't be retrieved for #{@emitter.underscore.pluralize}. Please try again later."
done
end
end
# @private
def timeframe(t = nil)
if t
@timeframe = t
emission
elsif @timeframe
puts ' => ' + @timeframe
else
puts ' => (defaults to current year)'
end
end
# @private
def emission
puts " => #{emission_in_kilograms} kg CO2e"
end
# @private
def emission_in_kilograms
impact(:timeframe => @timeframe).decisions.carbon.object.value
end
# @private
def lbs
puts " => #{emission_in_kilograms.kilograms.to :pounds} lbs CO2e"
end
alias :pounds :lbs
# @private
def tons
puts " => #{emission_in_kilograms.kilograms.to :tons} lbs CO2e"
end
# @private
def characteristics
if @input.empty?
puts " => (none)"
else
first = true
@input.each_pair do |key, value|
if first
puts " => #{key}: #{value}"
first = false
else
puts " #{key}: #{value}"
end
end
end
end
# @private
def url
puts " => #{impact(:timeframe => @timeframe).methodology}"
end
# @private
def methodology
first = true
impact(:timeframe => @timeframe).decisions.each do |name, report|
if first
w = ' => '
first = false
else
w = ' '
end
puts w + "#{name}: #{report.methodology}"
end
end
# @private
def reports
first = true
impact(:timeframe => @timeframe).decisions.each do |name, report|
if first
w = ' => '
first = false
else
w = ' '
end
puts w + "#{name}: #{report.object.inspect}"
end
end
# @private
def help
puts " => #{Emitter.characteristics(@emitter).join ', '}"
end
prompt_with do |emitter|
if emitter._timeframe
"#{emitter._name}[#{emitter._timeframe}]*"
else
"#{emitter._name}*"
end
end
# @private
def _name
@emitter
end
# @private
def _timeframe
@timeframe
end
# @private
def inspect
"#<Emitter[#{@emitter}]: #{@input.inspect}>"
end
# @private
def done
$emitters[@emitter] ||= []
$emitters[@emitter] << @input
puts " => Saved as #{@emitter} ##{$emitters[@emitter].length - 1}"
quit
end
end
end
end