carbon 0.2.7 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -16,6 +16,7 @@ begin
16
16
  # This is still only on Rubyforge
17
17
  gemspec.add_dependency 'SystemTimer', '>=1.2'
18
18
  gemspec.add_dependency 'blockenspiel', '>=0.3.2'
19
+ gemspec.add_dependency 'conversions', '~>1'
19
20
 
20
21
  gemspec.add_development_dependency 'fakeweb', '>=1.2.8'
21
22
  # sabshere 7/16/10 if you're having trouble running specs, try "rspec spec" and/or "sudo gem install rspec --pre"
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.7
1
+ 0.3.0
data/bin/carbon ADDED
@@ -0,0 +1,17 @@
1
+ #!/usr/bin/env ruby
2
+ $:.unshift(File.dirname(__FILE__) + '/../lib') unless $:.include?(File.dirname(__FILE__) + '/../lib')
3
+
4
+ require 'rubygems'
5
+ require 'carbon'
6
+ require 'carbon/cli'
7
+ begin
8
+ # The dup is to keep ARGV intact, so that tools like ruby-debug can respawn.
9
+ failure = Carbon::Cli.execute(ARGV.dup)
10
+ Kernel.exit(failure ? 1 : 0)
11
+ rescue SystemExit => e
12
+ Kernel.exit(e.status)
13
+ rescue Exception => e
14
+ STDERR.puts("#{e.message} (#{e.class})")
15
+ STDERR.puts(e.backtrace.join("\n"))
16
+ Kernel.exit(1)
17
+ end
data/carbon.gemspec CHANGED
@@ -5,13 +5,15 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{carbon}
8
- s.version = "0.2.7"
8
+ s.version = "0.3.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Derek Kastner", "Seamus Abshere", "Andy Rossmeissl"]
12
- s.date = %q{2010-08-18}
12
+ s.date = %q{2010-10-01}
13
+ s.default_executable = %q{carbon}
13
14
  s.description = %q{Carbon is a Ruby API wrapper for the Brighter Planet emission estimate web service (http://carbon.brighterplanet.com). By querying the web service, it can estimate the carbon emissions of many real-life objects, such as cars and houses, based on particular attributes that they may have.}
14
15
  s.email = %q{derek.kastner@brighterplanet.com}
16
+ s.executables = ["carbon"]
15
17
  s.extra_rdoc_files = [
16
18
  "README.rdoc"
17
19
  ]
@@ -21,14 +23,21 @@ Gem::Specification.new do |s|
21
23
  "README.rdoc",
22
24
  "Rakefile",
23
25
  "VERSION",
26
+ "bin/carbon",
24
27
  "carbon.gemspec",
25
28
  "doc/INTEGRATION_GUIDE.rdoc",
26
29
  "doc/examining-response-with-jsonview.png",
30
+ "doc/shell_example",
27
31
  "doc/timeout-error.png",
28
32
  "doc/with-committee-reports.png",
29
33
  "doc/without-committee-reports.png",
30
34
  "lib/carbon.rb",
31
35
  "lib/carbon/base.rb",
36
+ "lib/carbon/cli.rb",
37
+ "lib/carbon/cli/emitter.rb",
38
+ "lib/carbon/cli/environment.rb",
39
+ "lib/carbon/cli/irb.rb",
40
+ "lib/carbon/cli/shell.rb",
32
41
  "lib/carbon/emission_estimate.rb",
33
42
  "lib/carbon/emission_estimate/request.rb",
34
43
  "lib/carbon/emission_estimate/response.rb",
@@ -59,6 +68,7 @@ Gem::Specification.new do |s|
59
68
  s.add_runtime_dependency(%q<timeframe>, [">= 0.0.7"])
60
69
  s.add_runtime_dependency(%q<SystemTimer>, [">= 1.2"])
61
70
  s.add_runtime_dependency(%q<blockenspiel>, [">= 0.3.2"])
71
+ s.add_runtime_dependency(%q<conversions>, ["~> 1"])
62
72
  s.add_development_dependency(%q<fakeweb>, [">= 1.2.8"])
63
73
  s.add_development_dependency(%q<rspec>, [">= 2.0.0.beta.17"])
64
74
  else
@@ -67,6 +77,7 @@ Gem::Specification.new do |s|
67
77
  s.add_dependency(%q<timeframe>, [">= 0.0.7"])
68
78
  s.add_dependency(%q<SystemTimer>, [">= 1.2"])
69
79
  s.add_dependency(%q<blockenspiel>, [">= 0.3.2"])
80
+ s.add_dependency(%q<conversions>, ["~> 1"])
70
81
  s.add_dependency(%q<fakeweb>, [">= 1.2.8"])
71
82
  s.add_dependency(%q<rspec>, [">= 2.0.0.beta.17"])
72
83
  end
@@ -76,6 +87,7 @@ Gem::Specification.new do |s|
76
87
  s.add_dependency(%q<timeframe>, [">= 0.0.7"])
77
88
  s.add_dependency(%q<SystemTimer>, [">= 1.2"])
78
89
  s.add_dependency(%q<blockenspiel>, [">= 0.3.2"])
90
+ s.add_dependency(%q<conversions>, ["~> 1"])
79
91
  s.add_dependency(%q<fakeweb>, [">= 1.2.8"])
80
92
  s.add_dependency(%q<rspec>, [">= 2.0.0.beta.17"])
81
93
  end
data/doc/shell_example ADDED
@@ -0,0 +1,43 @@
1
+ carbon> key '123abc'
2
+ => Using key 123abc
3
+ carbon> help
4
+ => flight, automobile, ...
5
+ carbon> flight
6
+ => 974 kg CO2e
7
+ flight> lbs
8
+ => 123 lbs CO2e
9
+ flight> tons
10
+ => 1 ton CO2e
11
+ flight> help
12
+ => origin_airport, destination_airport . . .
13
+ flight> origin_airport 'lax'
14
+ => 1123 kg CO2e
15
+ flight> destination_airport 'jfk'
16
+ => 1375 kg CO2e
17
+ flight> characteristics
18
+ => Origin airport: JFK
19
+ Destination airport: JFK
20
+ flight> methodology
21
+ => Emission: from foo and bar
22
+ Foo: from baz
23
+ Bar: default
24
+ flight> url
25
+ => http://carbon.brighterplanet.com/flights?key=123abc&origin_airport[iata_code]=lax&destination_airport[iata_code]=jfk
26
+ flight> done
27
+ => Origin airport: JFK
28
+ Destination airport: JFK
29
+ 1375 kg CO2e
30
+ http://carbon.brighterplanet.com/flights?key=123abc&origin_airport[iata_code]=lax&destination_airport[iata_code]=jfk
31
+ Saved as flight #1
32
+ carbon> flight 1
33
+ => Origin airport: JFK
34
+ Destination airport: JFK
35
+ 1375 kg CO2e
36
+ flight> done
37
+ => Origin airport: JFK
38
+ Destination airport: JFK
39
+ 1375 kg CO2e
40
+ http://carbon.brighterplanet.com/flights?key=123abc&origin_airport[iata_code]=lax&destination_airport[iata_code]=jfk
41
+ Saved as flight #1
42
+ carbon> exit
43
+ $
data/lib/carbon.rb CHANGED
@@ -75,7 +75,7 @@ module Carbon
75
75
  # provide :make
76
76
  # end
77
77
  def emit_as(emitter_common_name, &block)
78
- self.carbon_base ||= ::Carbon::Base.new self, emitter_common_name
78
+ self.carbon_base = ::Carbon::Base.new self, emitter_common_name
79
79
  ::Blockenspiel.invoke block, carbon_base
80
80
  end
81
81
  # Third-person singular preferred.
data/lib/carbon/base.rb CHANGED
@@ -20,6 +20,10 @@ module Carbon
20
20
  def translation_table # :nodoc:
21
21
  @translation_table ||= Hash.new
22
22
  end
23
+
24
+ def reset_translation_table!
25
+ @translation_table = Hash.new
26
+ end
23
27
 
24
28
  # Indicate that you will send in a piece of data about the emitter.
25
29
  #
data/lib/carbon/cli.rb ADDED
@@ -0,0 +1,21 @@
1
+ module Carbon
2
+ module Cli
3
+ def execute(*)
4
+ $emitters = {}
5
+ IRB.start_session(Shell.new.get_binding)
6
+ end
7
+ module_function :execute
8
+ end
9
+ end
10
+
11
+ require 'carbon/cli/environment'
12
+ require 'carbon/cli/shell'
13
+ require 'carbon/cli/emitter'
14
+ require 'carbon/cli/irb'
15
+ require 'conversions'
16
+
17
+ if File.exist?(dotfile = File.join(ENV['HOME'], '.carbon_middleware'))
18
+ if (key = IO.read(dotfile).strip).present?
19
+ ::Carbon.key = key
20
+ end
21
+ end
@@ -0,0 +1,126 @@
1
+ module Carbon
2
+ module Cli
3
+ class Emitter < Environment
4
+ include Carbon
5
+ def initialize(name, input = {})
6
+ @emitter = name
7
+ @input = input
8
+ characteristics_url = "http://carbon.brighterplanet.com/#{@emitter.to_s.pluralize}/options.json"
9
+ response = REST.get(characteristics_url)
10
+ if response.ok?
11
+ @characteristics = JSON.parse(response.body)
12
+ @characteristics.keys.each do |characteristic|
13
+ instance_eval <<-meth
14
+ def #{characteristic}(arg = nil)
15
+ if arg
16
+ @input[:#{characteristic}] = arg
17
+ emission
18
+ else
19
+ @input[:#{characteristic}]
20
+ end
21
+ end
22
+ meth
23
+ end
24
+ if self.class.carbon_base && self.class.carbon_base.translation_table.any?
25
+ self.class.carbon_base.reset_translation_table!
26
+ end
27
+ provisions = @characteristics.keys.map { |k| "provide :#{k}"}.join('; ')
28
+ emit_as_block = "emit_as(:#{name}) { #{provisions} }"
29
+ self.class.class_eval emit_as_block
30
+ emission
31
+ else
32
+ puts " => Sorry, characteristics couldn't be retrieved for #{@emitter.to_s.pluralize} (via #{url})"
33
+ done
34
+ end
35
+ end
36
+
37
+ def emission
38
+ puts " => #{emission_in_kilograms} kg CO2e"
39
+ end
40
+
41
+ def emission_in_kilograms
42
+ ::Carbon::EmissionEstimate.new(self).to_f
43
+ end
44
+
45
+ def lbs
46
+ puts " => #{emission_in_kilograms.kilograms.to :pounds} lbs CO2e"
47
+ end
48
+ alias :pounds :lbs
49
+
50
+ def tons
51
+ puts " => #{emission_in_kilograms.kilograms.to :tons} lbs CO2e"
52
+ end
53
+
54
+ def characteristics
55
+ if @input.empty?
56
+ puts " => (none)"
57
+ else
58
+ first = true
59
+ @input.each_pair do |key, value|
60
+ if first
61
+ puts " => #{key}: #{value}"
62
+ first = false
63
+ else
64
+ puts " #{key}: #{value}"
65
+ end
66
+ end
67
+ end
68
+ end
69
+
70
+ def url
71
+ request = ::Carbon::EmissionEstimate.new(self).request
72
+ url = request.url
73
+ if request.body.present?
74
+ url << '?'
75
+ url << request.body
76
+ end
77
+ puts " => #{url}"
78
+ end
79
+
80
+ def methodology
81
+ first = true
82
+ ::Carbon::EmissionEstimate.new(self).reports.each do |report|
83
+ if first
84
+ w = ' => '
85
+ first = false
86
+ else
87
+ w = ' '
88
+ end
89
+ puts w + "#{report['committee']['name']}: #{report['quorum']['name']}"
90
+ end
91
+ end
92
+
93
+ def reports
94
+ first = true
95
+ ::Carbon::EmissionEstimate.new(self).reports.each do |report|
96
+ if first
97
+ w = ' => '
98
+ first = false
99
+ else
100
+ w = ' '
101
+ end
102
+ puts w + "#{report['committee']['name']}: #{report['conclusion'].inspect}"
103
+ end
104
+ end
105
+
106
+ def help
107
+ puts " => #{@characteristics.keys.join ', '}"
108
+ end
109
+
110
+ def to_s
111
+ "#{@emitter}*"
112
+ end
113
+
114
+ def inspect
115
+ "<Emitter[#{@emitter}]: #{@input.inspect}>"
116
+ end
117
+
118
+ def done
119
+ $emitters[@emitter] ||= []
120
+ $emitters[@emitter] << @input
121
+ puts " => Saved as #{@emitter} ##{$emitters[@emitter].length - 1}"
122
+ throw :IRB_EXIT
123
+ end
124
+ end
125
+ end
126
+ end
@@ -0,0 +1,16 @@
1
+ module Carbon
2
+ module Cli
3
+ class Environment
4
+ instance_methods.each do |m|
5
+ undef_method(m) if m.to_s !~ /(?:^__|^nil\?$|^send$|^instance_eval$|^define_method$|^class$|^object_id$)/
6
+ end
7
+
8
+ def get_binding() binding end
9
+
10
+ def method_missing(*args)
11
+ return if [:extend, :respond_to?].include? args.first
12
+ puts "Unknown command #{args.first}"
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,32 @@
1
+ require 'irb'
2
+
3
+ module IRB # :nodoc:
4
+ def self.start_session(binding)
5
+ unless @__initialized
6
+ args = ARGV
7
+ ARGV.replace(ARGV.dup)
8
+ IRB.setup(nil)
9
+ ARGV.replace(args)
10
+ @__initialized = true
11
+ end
12
+
13
+ workspace = WorkSpace.new(binding)
14
+
15
+ @CONF[:PROMPT][:CARBON] = {
16
+ :PROMPT_I => "%m> ",
17
+ :PROMPT_S => "%m\"> ",
18
+ :PROMPT_C => "%m…>",
19
+ :PROMPT_N => "%m→>",
20
+ :RETURN => ''
21
+ }
22
+ @CONF[:PROMPT_MODE] = :CARBON
23
+
24
+ irb = Irb.new(workspace)
25
+
26
+ @CONF[:IRB_RC].call(irb.context) if @CONF[:IRB_RC]
27
+ @CONF[:MAIN_CONTEXT] = irb.context
28
+ catch(:IRB_EXIT) do
29
+ irb.eval_input
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,42 @@
1
+ module Carbon
2
+ module Cli
3
+ class Shell < Environment
4
+ cattr_accessor :emitters
5
+
6
+ emitters_url = "http://carbon.brighterplanet.com/models.json"
7
+ response = REST.get(emitters_url)
8
+ if true || response.ok?
9
+ self.emitters = JSON.parse(response.body)
10
+ emitters.each do |e|
11
+ define_method e.to_sym do |*args|
12
+ if args.any? and num = args.first and saved = $emitters[e.to_sym][num]
13
+ emitter e.to_sym, saved
14
+ else
15
+ emitter e.to_sym
16
+ end
17
+ end
18
+ end
19
+ else
20
+ puts " => Sorry, emitter types couldn't be retrieved (via #{emitters_url})"
21
+ done
22
+ end
23
+
24
+ def help
25
+ puts " => #{self.class.emitters.join ', '}"
26
+ end
27
+
28
+ def to_s
29
+ 'carbon-'
30
+ end
31
+
32
+ def key(k)
33
+ ::Carbon.key = k
34
+ puts " => Using key #{::Carbon.key}"
35
+ end
36
+
37
+ def emitter(e, saved = {})
38
+ ::IRB.start_session(Emitter.new(e, saved).get_binding)
39
+ end
40
+ end
41
+ end
42
+ end
@@ -146,5 +146,9 @@ module Carbon
146
146
  def methodology
147
147
  data['methodology']
148
148
  end
149
+
150
+ def reports
151
+ data['reports']
152
+ end
149
153
  end
150
154
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: carbon
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 19
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 2
9
- - 7
10
- version: 0.2.7
8
+ - 3
9
+ - 0
10
+ version: 0.3.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Derek Kastner
@@ -17,8 +17,8 @@ autorequire:
17
17
  bindir: bin
18
18
  cert_chain: []
19
19
 
20
- date: 2010-08-18 00:00:00 -04:00
21
- default_executable:
20
+ date: 2010-10-01 00:00:00 -05:00
21
+ default_executable: carbon
22
22
  dependencies:
23
23
  - !ruby/object:Gem::Dependency
24
24
  name: activesupport
@@ -99,9 +99,23 @@ dependencies:
99
99
  type: :runtime
100
100
  version_requirements: *id005
101
101
  - !ruby/object:Gem::Dependency
102
- name: fakeweb
102
+ name: conversions
103
103
  prerelease: false
104
104
  requirement: &id006 !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ~>
108
+ - !ruby/object:Gem::Version
109
+ hash: 1
110
+ segments:
111
+ - 1
112
+ version: "1"
113
+ type: :runtime
114
+ version_requirements: *id006
115
+ - !ruby/object:Gem::Dependency
116
+ name: fakeweb
117
+ prerelease: false
118
+ requirement: &id007 !ruby/object:Gem::Requirement
105
119
  none: false
106
120
  requirements:
107
121
  - - ">="
@@ -113,11 +127,11 @@ dependencies:
113
127
  - 8
114
128
  version: 1.2.8
115
129
  type: :development
116
- version_requirements: *id006
130
+ version_requirements: *id007
117
131
  - !ruby/object:Gem::Dependency
118
132
  name: rspec
119
133
  prerelease: false
120
- requirement: &id007 !ruby/object:Gem::Requirement
134
+ requirement: &id008 !ruby/object:Gem::Requirement
121
135
  none: false
122
136
  requirements:
123
137
  - - ">="
@@ -131,11 +145,11 @@ dependencies:
131
145
  - 17
132
146
  version: 2.0.0.beta.17
133
147
  type: :development
134
- version_requirements: *id007
148
+ version_requirements: *id008
135
149
  description: Carbon is a Ruby API wrapper for the Brighter Planet emission estimate web service (http://carbon.brighterplanet.com). By querying the web service, it can estimate the carbon emissions of many real-life objects, such as cars and houses, based on particular attributes that they may have.
136
150
  email: derek.kastner@brighterplanet.com
137
- executables: []
138
-
151
+ executables:
152
+ - carbon
139
153
  extensions: []
140
154
 
141
155
  extra_rdoc_files:
@@ -146,14 +160,21 @@ files:
146
160
  - README.rdoc
147
161
  - Rakefile
148
162
  - VERSION
163
+ - bin/carbon
149
164
  - carbon.gemspec
150
165
  - doc/INTEGRATION_GUIDE.rdoc
151
166
  - doc/examining-response-with-jsonview.png
167
+ - doc/shell_example
152
168
  - doc/timeout-error.png
153
169
  - doc/with-committee-reports.png
154
170
  - doc/without-committee-reports.png
155
171
  - lib/carbon.rb
156
172
  - lib/carbon/base.rb
173
+ - lib/carbon/cli.rb
174
+ - lib/carbon/cli/emitter.rb
175
+ - lib/carbon/cli/environment.rb
176
+ - lib/carbon/cli/irb.rb
177
+ - lib/carbon/cli/shell.rb
157
178
  - lib/carbon/emission_estimate.rb
158
179
  - lib/carbon/emission_estimate/request.rb
159
180
  - lib/carbon/emission_estimate/response.rb