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 CHANGED
@@ -18,5 +18,12 @@ module Carbon
18
18
  def base_url=(val)
19
19
  @base_url = val
20
20
  end
21
+
22
+ def debug=(val)
23
+ @debug = val
24
+ end
25
+ def debug
26
+ @debug ||= false
27
+ end
21
28
  end
22
29
  end
@@ -1,12 +1,16 @@
1
1
  require 'uri'
2
2
  require 'active_support/inflector'
3
- require 'net/http'
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
- attr_accessor :options, :source, :value, :methodology_url
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
- hsh[characteristic.name] = source.send(characteristic.field)
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
- request = Net::HTTP::Post.new(url.path, 'Accept' => 'application/json')
46
- request.set_form_data(fields)
47
- response = Net::HTTP.new(url.host).start { |http| http.request(request) }
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
- - 5
9
- version: 0.0.5
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-27 00:00:00 -04:00
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: active_support
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: fakeweb
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: :development
42
+ type: :runtime
43
43
  version_requirements: *id002
44
44
  - !ruby/object:Gem::Dependency
45
- name: rspec
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: []