carbon 0.0.0 → 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/lib/carbon.rb CHANGED
@@ -1,3 +1,5 @@
1
+ require 'carbon/emitter'
2
+ require 'carbon/flight'
1
3
 
2
4
  module Carbon
3
5
  class << self
@@ -7,5 +9,12 @@ module Carbon
7
9
  def api_key=(val)
8
10
  @api_key = val
9
11
  end
12
+
13
+ def base_url
14
+ @base_url ||= 'http://carbon.brighterplanet.com'
15
+ end
16
+ def base_url=(val)
17
+ @base_url = val
18
+ end
10
19
  end
11
20
  end
@@ -1,7 +1,36 @@
1
+ require 'carbon/resource'
2
+ require 'carbon/emitter/class_methods'
3
+
1
4
  module Carbon
2
5
  module Emitter
3
- def calculate
6
+ class NotYetCalculated < StandardError; end
7
+ include Resource
8
+
9
+ def self.included(target)
10
+ target.extend Carbon::Emitter::ClassMethods
11
+ end
12
+
13
+ def initialize(args = {})
14
+ args.each do |name, value|
15
+ next unless self.class.characteristics.include?(name.to_sym)
16
+ self.send("#{name}=", value)
17
+ end
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
4
29
 
30
+ def calculate!
31
+ fetch_calculation
32
+ @emission = result['emission']
33
+ @methodology = result['methodology']
5
34
  end
6
35
  end
7
36
  end
@@ -0,0 +1,14 @@
1
+ module Carbon
2
+ module Emitter
3
+ module ClassMethods
4
+ def characteristics(*args)
5
+ if args.empty?
6
+ @characteristics
7
+ else
8
+ @characteristics = args
9
+ self.instance_eval { attr_accessor *@characteristics }
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,10 @@
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
@@ -0,0 +1,60 @@
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
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 0
8
- - 0
9
- version: 0.0.0
8
+ - 1
9
+ version: 0.0.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - Derek Kastner
@@ -18,7 +18,7 @@ date: 2010-01-22 00:00:00 -05:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
- name: httparty
21
+ name: rest-client
22
22
  prerelease: false
23
23
  requirement: &id001 !ruby/object:Gem::Requirement
24
24
  requirements:
@@ -29,6 +29,18 @@ dependencies:
29
29
  version: "0"
30
30
  type: :runtime
31
31
  version_requirements: *id001
32
+ - !ruby/object:Gem::Dependency
33
+ name: fakeweb
34
+ prerelease: false
35
+ requirement: &id002 !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ segments:
40
+ - 0
41
+ version: "0"
42
+ type: :development
43
+ version_requirements: *id002
32
44
  description: Carbon allows you to easily calculate the carbon footprint of various activities. This is an API for the Brighter Planet Carbon Middleware service.
33
45
  email: derek@brighterplanet.com
34
46
  executables: []
@@ -38,7 +50,10 @@ extensions: []
38
50
  extra_rdoc_files:
39
51
  - MIT-LICENSE.txt
40
52
  files:
53
+ - lib/carbon/emitter/class_methods.rb
41
54
  - lib/carbon/emitter.rb
55
+ - lib/carbon/flight.rb
56
+ - lib/carbon/resource.rb
42
57
  - lib/carbon.rb
43
58
  - MIT-LICENSE.txt
44
59
  has_rdoc: true