carbon 0.0.5 → 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/carbon.rb +7 -0
- data/lib/carbon/emissions_calculation.rb +19 -7
- metadata +19 -7
data/lib/carbon.rb
CHANGED
@@ -1,12 +1,16 @@
|
|
1
1
|
require 'uri'
|
2
2
|
require 'active_support/inflector'
|
3
|
-
require '
|
3
|
+
require 'httparty'
|
4
4
|
|
5
5
|
module Carbon
|
6
6
|
class EmissionsCalculation
|
7
7
|
class NotYetCalculated < StandardError; end
|
8
|
+
class CalculationRequestFailed < StandardError; end
|
8
9
|
|
9
|
-
|
10
|
+
include HTTParty
|
11
|
+
|
12
|
+
attr_accessor :options, :source
|
13
|
+
attr_reader :value, :methodology_url, :committees
|
10
14
|
|
11
15
|
def initialize(options, source)
|
12
16
|
self.options = options
|
@@ -26,6 +30,7 @@ module Carbon
|
|
26
30
|
fetch_calculation
|
27
31
|
@value = result['emission']
|
28
32
|
@methodology_url = result['methodology']
|
33
|
+
@committees = result['committees']
|
29
34
|
end
|
30
35
|
|
31
36
|
private
|
@@ -34,17 +39,24 @@ module Carbon
|
|
34
39
|
end
|
35
40
|
|
36
41
|
def fields
|
37
|
-
options.characteristics.inject({}) do |hsh, characteristic|
|
38
|
-
|
42
|
+
fields_hash = options.characteristics.inject({}) do |hsh, characteristic|
|
43
|
+
value = source.send(characteristic.field)
|
44
|
+
hsh[characteristic.name.to_sym] = value if value
|
39
45
|
hsh
|
40
46
|
end
|
47
|
+
{ :body => { options.emitter_type => fields_hash } }
|
41
48
|
end
|
42
49
|
|
43
50
|
def fetch_calculation
|
44
51
|
url = URI.join(Carbon.base_url, options.emitter_type.to_s.pluralize)
|
45
|
-
|
46
|
-
|
47
|
-
|
52
|
+
options = fields.merge(:headers => { 'Accept' => 'application/json' })
|
53
|
+
response = self.class.post(url.to_s, options)
|
54
|
+
|
55
|
+
unless (200..399).include?(response.code)
|
56
|
+
raise CalculationRequestFailed, response.body
|
57
|
+
end
|
58
|
+
|
59
|
+
puts response.body if Carbon.debug
|
48
60
|
|
49
61
|
@result = JSON.parse(response.body)
|
50
62
|
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
|
+
- 7
|
9
|
+
version: 0.0.7
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Derek Kastner
|
@@ -14,11 +14,11 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-04-
|
17
|
+
date: 2010-04-28 00:00:00 -04:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
|
-
name:
|
21
|
+
name: activesupport
|
22
22
|
prerelease: false
|
23
23
|
requirement: &id001 !ruby/object:Gem::Requirement
|
24
24
|
requirements:
|
@@ -30,7 +30,7 @@ dependencies:
|
|
30
30
|
type: :runtime
|
31
31
|
version_requirements: *id001
|
32
32
|
- !ruby/object:Gem::Dependency
|
33
|
-
name:
|
33
|
+
name: httparty
|
34
34
|
prerelease: false
|
35
35
|
requirement: &id002 !ruby/object:Gem::Requirement
|
36
36
|
requirements:
|
@@ -39,10 +39,10 @@ dependencies:
|
|
39
39
|
segments:
|
40
40
|
- 0
|
41
41
|
version: "0"
|
42
|
-
type: :
|
42
|
+
type: :runtime
|
43
43
|
version_requirements: *id002
|
44
44
|
- !ruby/object:Gem::Dependency
|
45
|
-
name:
|
45
|
+
name: fakeweb
|
46
46
|
prerelease: false
|
47
47
|
requirement: &id003 !ruby/object:Gem::Requirement
|
48
48
|
requirements:
|
@@ -53,6 +53,18 @@ dependencies:
|
|
53
53
|
version: "0"
|
54
54
|
type: :development
|
55
55
|
version_requirements: *id003
|
56
|
+
- !ruby/object:Gem::Dependency
|
57
|
+
name: rspec
|
58
|
+
prerelease: false
|
59
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
segments:
|
64
|
+
- 0
|
65
|
+
version: "0"
|
66
|
+
type: :development
|
67
|
+
version_requirements: *id004
|
56
68
|
description: Carbon allows you to easily calculate the carbon footprint of various activities. This is an API for the Brighter Planet Carbon Middleware service.
|
57
69
|
email: derek@brighterplanet.com
|
58
70
|
executables: []
|