carbon 0.0.1 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/carbon.rb +1 -1
- data/lib/carbon/emissions_calculation.rb +53 -0
- data/lib/carbon/emitter.rb +5 -22
- data/lib/carbon/emitter/characteristic.rb +19 -0
- data/lib/carbon/emitter/class_methods.rb +13 -7
- data/lib/carbon/emitter/options.rb +30 -0
- metadata +6 -5
- data/lib/carbon/flight.rb +0 -10
- data/lib/carbon/resource.rb +0 -60
data/lib/carbon.rb
CHANGED
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'uri'
|
2
|
+
require 'rest_client'
|
3
|
+
require 'activesupport'
|
4
|
+
|
5
|
+
module Carbon
|
6
|
+
class EmissionsCalculation
|
7
|
+
class NotYetCalculated < StandardError; end
|
8
|
+
|
9
|
+
attr_accessor :options, :source, :value, :methodology_url
|
10
|
+
|
11
|
+
def initialize(options, source)
|
12
|
+
self.options = options
|
13
|
+
self.source = source
|
14
|
+
end
|
15
|
+
|
16
|
+
def methodology_url
|
17
|
+
raise NotYetCalculated if result.nil?
|
18
|
+
@methodology_url
|
19
|
+
end
|
20
|
+
def value
|
21
|
+
raise NotYetCalculated if result.nil?
|
22
|
+
@value
|
23
|
+
end
|
24
|
+
|
25
|
+
def calculate!
|
26
|
+
fetch_calculation
|
27
|
+
@value = result['emission']
|
28
|
+
@methodology_url = result['methodology']
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
def result
|
33
|
+
@result
|
34
|
+
end
|
35
|
+
|
36
|
+
def resource
|
37
|
+
url = URI.join(Carbon.base_url, options.emitter_type.to_s.pluralize)
|
38
|
+
@resource ||= RestClient::Resource.new(url.to_s)
|
39
|
+
end
|
40
|
+
|
41
|
+
def fields
|
42
|
+
options.characteristics.inject({}) do |hsh, characteristic|
|
43
|
+
hsh[characteristic.name] = source.send(characteristic.field)
|
44
|
+
hsh
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def fetch_calculation
|
49
|
+
response = resource.post fields, :accept => :json
|
50
|
+
@result = JSON.parse(response.body)
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
data/lib/carbon/emitter.rb
CHANGED
@@ -4,33 +4,16 @@ require 'carbon/emitter/class_methods'
|
|
4
4
|
module Carbon
|
5
5
|
module Emitter
|
6
6
|
class NotYetCalculated < StandardError; end
|
7
|
-
include Resource
|
8
7
|
|
9
8
|
def self.included(target)
|
10
9
|
target.extend Carbon::Emitter::ClassMethods
|
11
10
|
end
|
12
11
|
|
13
|
-
def
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
end
|
19
|
-
|
20
|
-
def methodology
|
21
|
-
raise Emitter::NotYetCalculated if result.nil?
|
22
|
-
@methodology
|
23
|
-
end
|
24
|
-
|
25
|
-
def emission
|
26
|
-
raise Emitter::NotYetCalculated if result.nil?
|
27
|
-
@emission
|
28
|
-
end
|
29
|
-
|
30
|
-
def calculate!
|
31
|
-
fetch_calculation
|
32
|
-
@emission = result['emission']
|
33
|
-
@methodology = result['methodology']
|
12
|
+
def emissions
|
13
|
+
return @emissions unless @emissions.nil?
|
14
|
+
@emissions = Carbon::EmissionsCalculation.new(self.class.emitter_options, self)
|
15
|
+
@emissions.calculate!
|
16
|
+
@emissions
|
34
17
|
end
|
35
18
|
end
|
36
19
|
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module Carbon
|
2
|
+
module Emitter
|
3
|
+
class Characteristic
|
4
|
+
attr_accessor :name, :field
|
5
|
+
|
6
|
+
def self.from_options_hash(name, options)
|
7
|
+
new(:name => name, :field => options[:with])
|
8
|
+
end
|
9
|
+
|
10
|
+
def initialize(options = {})
|
11
|
+
options.each { |name, value| self.send("#{name}=", value) unless value.nil? }
|
12
|
+
end
|
13
|
+
|
14
|
+
def field
|
15
|
+
@field ||= self.name
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -1,14 +1,20 @@
|
|
1
|
+
require 'carbon/emitter/options'
|
2
|
+
|
1
3
|
module Carbon
|
2
4
|
module Emitter
|
3
5
|
module ClassMethods
|
4
|
-
def
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
self.instance_eval { attr_accessor *@characteristics }
|
10
|
-
end
|
6
|
+
def emits_as(emitter_type, &blk)
|
7
|
+
self.emitter_type = emitter_type
|
8
|
+
|
9
|
+
self.emitter_options = Carbon::Emitter::Options.new(emitter_type)
|
10
|
+
self.emitter_options.instance_eval &blk if block_given?
|
11
11
|
end
|
12
|
+
|
13
|
+
def emitter_type; @emitter_type; end
|
14
|
+
def emitter_type=(val); @emitter_type = val; end
|
15
|
+
|
16
|
+
def emitter_options; @emitter_options; end
|
17
|
+
def emitter_options=(val); @emitter_options = val; end
|
12
18
|
end
|
13
19
|
end
|
14
20
|
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'carbon/emitter/characteristic'
|
2
|
+
|
3
|
+
module Carbon
|
4
|
+
module Emitter
|
5
|
+
class Options
|
6
|
+
attr_accessor :emitter_type, :characteristics
|
7
|
+
|
8
|
+
def initialize(emitter_type)
|
9
|
+
self.emitter_type = emitter_type.to_sym
|
10
|
+
end
|
11
|
+
|
12
|
+
def keys
|
13
|
+
characteristics.map { |c| c.name }
|
14
|
+
end
|
15
|
+
|
16
|
+
def [](name)
|
17
|
+
characteristics.find { |c| c.name == name }
|
18
|
+
end
|
19
|
+
|
20
|
+
def characteristics
|
21
|
+
@characteristics ||= []
|
22
|
+
end
|
23
|
+
|
24
|
+
def provides(characteristic_name, options = {})
|
25
|
+
characteristics <<
|
26
|
+
Carbon::Emitter::Characteristic.from_options_hash(characteristic_name, options)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: 0.0.
|
8
|
+
- 3
|
9
|
+
version: 0.0.3
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Derek Kastner
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-
|
17
|
+
date: 2010-04-26 00:00:00 -04:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -50,10 +50,11 @@ extensions: []
|
|
50
50
|
extra_rdoc_files:
|
51
51
|
- MIT-LICENSE.txt
|
52
52
|
files:
|
53
|
+
- lib/carbon/emissions_calculation.rb
|
54
|
+
- lib/carbon/emitter/characteristic.rb
|
53
55
|
- lib/carbon/emitter/class_methods.rb
|
56
|
+
- lib/carbon/emitter/options.rb
|
54
57
|
- lib/carbon/emitter.rb
|
55
|
-
- lib/carbon/flight.rb
|
56
|
-
- lib/carbon/resource.rb
|
57
58
|
- lib/carbon.rb
|
58
59
|
- MIT-LICENSE.txt
|
59
60
|
has_rdoc: true
|
data/lib/carbon/flight.rb
DELETED
@@ -1,10 +0,0 @@
|
|
1
|
-
module Carbon
|
2
|
-
class Flight
|
3
|
-
include Carbon::Emitter
|
4
|
-
|
5
|
-
characteristics :date, :year, :time_of_day, :destination_airport,
|
6
|
-
:origin_airport, :distance_class, :distance_estimate, :domesticity,
|
7
|
-
:airline, :trips, :emplanements_per_trip, :seat_class, :load_factor,
|
8
|
-
:seats_estimate, :aircraft_class, :aircraft, :propulsion
|
9
|
-
end
|
10
|
-
end
|
data/lib/carbon/resource.rb
DELETED
@@ -1,60 +0,0 @@
|
|
1
|
-
require 'uri'
|
2
|
-
require 'json'
|
3
|
-
|
4
|
-
module Carbon
|
5
|
-
module Resource
|
6
|
-
class << self
|
7
|
-
def included(target)
|
8
|
-
if target == Carbon::Emitter
|
9
|
-
Carbon::Emitter::ClassMethods.instance_eval { include(Carbon::Resource::ClassMethods) }
|
10
|
-
else
|
11
|
-
target.extend(Carbon::Resource::ClassMethods)
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
def underscore(string)
|
16
|
-
string.gsub(/([a-z])([A-Z])/,'\1_\2').downcase
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
|
21
|
-
module ClassMethods
|
22
|
-
def resource_name(val = nil)
|
23
|
-
@resource_name = val.to_s unless val.nil?
|
24
|
-
if @resource_name.nil?
|
25
|
-
class_name = self.to_s.split('::').last
|
26
|
-
@resource_name ||= "#{Carbon::Resource.underscore(class_name)}s"
|
27
|
-
end
|
28
|
-
@resource_name
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
private
|
33
|
-
attr_accessor :result
|
34
|
-
|
35
|
-
def fields
|
36
|
-
self.class.characteristics.map(&:to_s).inject({}) do |hash, char|
|
37
|
-
value = self.send(char)
|
38
|
-
if value.respond_to?(:keys)
|
39
|
-
hash[char] = {}
|
40
|
-
value.each do |key, value|
|
41
|
-
hash[char][key.to_s] = value
|
42
|
-
end
|
43
|
-
elsif !value.nil?
|
44
|
-
hash[char] = value
|
45
|
-
end
|
46
|
-
hash
|
47
|
-
end
|
48
|
-
end
|
49
|
-
|
50
|
-
def resource
|
51
|
-
url = URI.join(Carbon.base_url, self.class.resource_name)
|
52
|
-
@resource ||= RestClient::Resource.new(url.to_s)
|
53
|
-
end
|
54
|
-
|
55
|
-
def fetch_calculation
|
56
|
-
response = resource.post fields, :accept => :json
|
57
|
-
self.result = JSON.parse(response.body)
|
58
|
-
end
|
59
|
-
end
|
60
|
-
end
|