Class: SmarterMeter::Services::BrighterPlanet

Inherits:
Object
  • Object
show all
Defined in:
lib/smartermeter/services/brighterplanet.rb

Overview

Allows users to calculate the carbon produced by their energy usage by accessing the Brighter Planet CM1 Electricity API.

More details can be found here: carbon.brighterplanet.com/models/electricity_use

Example:

api = SmarterMeter::Services::BrighterPlanet.new
puts api.calculate_kg_carbon(10.1)

Prints:

0.6460529833458619

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ BrighterPlanet

Initializes the Brigher Planet CM1 Electricity API.

Example:

SmarterMeter::Services::BrighterPlanet.new do |c|
  c.api_key = "key"
end

Yields:

  • (_self)

Yield Parameters:



34
35
36
# File 'lib/smartermeter/services/brighterplanet.rb', line 34

def initialize
  yield self if block_given?
end

Instance Attribute Details

#api_keyObject

The api key required for commericial access to the API. For more detail see: carbon.brighterplanet.com/pricing



23
24
25
# File 'lib/smartermeter/services/brighterplanet.rb', line 23

def api_key
  @api_key
end

Instance Method Details

#calculate_kg_carbon(kwh, opts = {}) ⇒ Float

Calculates the number of kilograms of carbon produced from the given electrical energy usage.

Note: this is an estimate and depends on many factors, to improve accuracy include an optional zipcode of where this energy was consumed.

Parameters:

  • kwh (Float)

    the number of kilowatt hours consumed.

Returns:

  • (Float)

    kilograms of carbon produced by this much energy.

Raises:

  • (ArgumentError)


47
48
49
50
51
52
53
54
55
56
# File 'lib/smartermeter/services/brighterplanet.rb', line 47

def calculate_kg_carbon(kwh, opts={})
  allowed_keys = [:zip_code, :date]
  non_permitted_keys = opts.keys.reject { |k| allowed_keys.include? k }
  raise ArgumentError, "opts contains keys not found in #{allowed_keys}" unless non_permitted_keys.empty?

  params = {:energy => kwh}
  params.merge!(opts)
  response = RestClient.get 'http://carbon.brighterplanet.com/electricity_uses.json', :params => params
  JSON.parse(response.body)["emission"]
end