energy 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +5 -0
- data/Gemfile +5 -0
- data/MIT-LICENSE.txt +19 -0
- data/README.rdoc +32 -0
- data/Rakefile +21 -0
- data/energy.gemspec +25 -0
- data/lib/energy.rb +19 -0
- data/lib/energy/automobile_trip.rb +31 -0
- data/lib/energy/config.rb +6 -0
- data/lib/energy/consumer.rb +19 -0
- data/lib/energy/flight.rb +52 -0
- data/lib/energy/rail_trip.rb +27 -0
- data/lib/energy/version.rb +3 -0
- data/test/helper.rb +9 -0
- data/test/test_energy.rb +26 -0
- metadata +108 -0
data/Gemfile
ADDED
data/MIT-LICENSE.txt
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright (c) 2011 Brighter Planet, Inc.
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
5
|
+
in the Software without restriction, including without limitation the rights
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
8
|
+
furnished to do so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
11
|
+
all copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
=energy
|
2
|
+
|
3
|
+
Get energy use estimates for flights, rail trips, and automobile trips (more coming soon!). BETA from Brighter Planet.
|
4
|
+
|
5
|
+
==BETA
|
6
|
+
|
7
|
+
This is beta software and subject to change at any time.
|
8
|
+
|
9
|
+
==Examples
|
10
|
+
|
11
|
+
===Fuel used by a Bentley
|
12
|
+
|
13
|
+
?> Energy::AutomobileTrip.fuel_use(:make => 'Bentley', :distance => 500)
|
14
|
+
=> [how many litres of gas a Bentley uses to drive 500km]
|
15
|
+
|
16
|
+
===Fuel used by a Continental Airlines flight
|
17
|
+
|
18
|
+
?> Energy::Flight.fuel_use(:airline => 'Continental Airlines')
|
19
|
+
=> [how many litres of aviation fuel a Continental Airlines flight consumes]
|
20
|
+
|
21
|
+
===Fuel used by an Amtrak train
|
22
|
+
|
23
|
+
?> Energy::RailTrip.fuel_use(:rail_class => 'intercity', :distance => 500)
|
24
|
+
=> [how many litres of diesel an intercity uses over 500km]
|
25
|
+
|
26
|
+
==TODO
|
27
|
+
|
28
|
+
* always provide a conversion of, for example, fuel use in litres to energy use in joules to be true to the intent of the gem
|
29
|
+
* decide how we expect people to use this gem (maybe it should be more like Carbon and not define its own classes)
|
30
|
+
* hit impact.brighterplanet.com or energy.brighterplanet.com directly
|
31
|
+
|
32
|
+
Copyright 2011 Brighter Planet, Inc.
|
data/Rakefile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'bundler'
|
2
|
+
Bundler::GemHelper.install_tasks
|
3
|
+
|
4
|
+
require 'rake'
|
5
|
+
require 'rake/testtask'
|
6
|
+
Rake::TestTask.new(:test) do |test|
|
7
|
+
test.libs << 'lib' << 'test'
|
8
|
+
test.pattern = 'test/**/test_*.rb'
|
9
|
+
test.verbose = true
|
10
|
+
end
|
11
|
+
|
12
|
+
require 'rake/rdoctask'
|
13
|
+
Rake::RDocTask.new do |rdoc|
|
14
|
+
rdoc.rdoc_dir = 'rdoc'
|
15
|
+
rdoc.title = 'energy'
|
16
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
17
|
+
rdoc.rdoc_files.include('README*')
|
18
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
19
|
+
end
|
20
|
+
|
21
|
+
task :default => :test
|
data/energy.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "energy/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "energy"
|
7
|
+
s.version = Energy::VERSION
|
8
|
+
s.authors = ["Seamus Abshere"]
|
9
|
+
s.email = ["seamus@abshere.net"]
|
10
|
+
s.homepage = "https://github.com/brighterplanet/energy"
|
11
|
+
s.summary = %q{Get energy use estimates for flights, rail trips, and automobile trips (more coming soon!). BETA from Brighter Planet.}
|
12
|
+
s.description = %q{Get energy use estimates for flights, rail trips, and automobile trips (more coming soon!). BETA from Brighter Planet.}
|
13
|
+
|
14
|
+
s.rubyforge_project = "energy"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
s.add_development_dependency 'test-unit'
|
22
|
+
s.add_development_dependency 'rake'
|
23
|
+
s.add_runtime_dependency 'activesupport'
|
24
|
+
s.add_runtime_dependency 'carbon', '>=1.1.2'
|
25
|
+
end
|
data/lib/energy.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require "energy/version"
|
2
|
+
require 'active_support/core_ext'
|
3
|
+
|
4
|
+
module Energy
|
5
|
+
autoload :AutomobileTrip, 'energy/automobile_trip'
|
6
|
+
autoload :Config, 'energy/config'
|
7
|
+
autoload :Consumer, 'energy/consumer'
|
8
|
+
autoload :Flight, 'energy/flight'
|
9
|
+
autoload :RailTrip, 'energy/rail_trip'
|
10
|
+
|
11
|
+
# Set configuration variables like
|
12
|
+
#
|
13
|
+
# Energy.config[:key] = '456'
|
14
|
+
#
|
15
|
+
# You can get your key at http://keys.brighterplanet.com
|
16
|
+
def self.config
|
17
|
+
Config.instance
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module Energy
|
2
|
+
class AutomobileTrip < Consumer
|
3
|
+
attr_accessor :make
|
4
|
+
attr_accessor :model
|
5
|
+
attr_accessor :distance
|
6
|
+
|
7
|
+
emit_as :automobile_trip do
|
8
|
+
provide :make
|
9
|
+
provide :model
|
10
|
+
provide :distance
|
11
|
+
end
|
12
|
+
|
13
|
+
def initialize(attrs = {}) # :nodoc:
|
14
|
+
attrs = attrs.symbolize_keys
|
15
|
+
@make = attrs[:make]
|
16
|
+
@model = attrs[:model]
|
17
|
+
@distance = attrs[:distance]
|
18
|
+
end
|
19
|
+
|
20
|
+
class << self
|
21
|
+
# Estimate the fuel used by an automobile trip given any of the following optional characteristics:
|
22
|
+
# * <tt>make</tt> - "nissan"
|
23
|
+
# * <tt>model</tt> - "altima"
|
24
|
+
# * <tt>distance</tt> - 500 (kilometres)
|
25
|
+
# See http://carbon.brighterplanet.com/models/rail_trip for more details
|
26
|
+
def fuel_use(attrs = {})
|
27
|
+
estimate :fuel_use, attrs
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'carbon'
|
2
|
+
|
3
|
+
module Energy
|
4
|
+
class Consumer # :nodoc:
|
5
|
+
include ::Carbon
|
6
|
+
class << self
|
7
|
+
private
|
8
|
+
|
9
|
+
# Use CM1 to get an energy consumption estimate
|
10
|
+
def estimate(k, attrs = {})
|
11
|
+
attrs = attrs.symbolize_keys
|
12
|
+
timeframe = attrs.delete(:timeframe) || ::Timeframe.this_year
|
13
|
+
emitter_instance = new attrs
|
14
|
+
emission_estimate = emitter_instance.emission_estimate(:timeframe => timeframe, :key => Config.instance[:key])
|
15
|
+
emission_estimate.send k
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
module Energy
|
2
|
+
class Flight < Consumer
|
3
|
+
attr_accessor :airline
|
4
|
+
attr_accessor :aircraft
|
5
|
+
attr_accessor :origin_airport
|
6
|
+
attr_accessor :destination_airport
|
7
|
+
attr_accessor :segments_per_trip
|
8
|
+
|
9
|
+
emit_as :flight do
|
10
|
+
provide :airline
|
11
|
+
provide :aircraft
|
12
|
+
provide :origin_airport
|
13
|
+
provide :destination_airport
|
14
|
+
provide :segments_per_trip
|
15
|
+
end
|
16
|
+
|
17
|
+
def initialize(attrs = {}) # :nodoc:
|
18
|
+
attrs = attrs.symbolize_keys
|
19
|
+
@airline = attrs[:airline]
|
20
|
+
@aircraft = attrs[:aircraft]
|
21
|
+
@origin_airport = attrs[:origin_airport]
|
22
|
+
@destination_airport = attrs[:destination_airport]
|
23
|
+
@segments_per_trip = attrs[:segments_per_trip] || 1
|
24
|
+
end
|
25
|
+
|
26
|
+
class << self
|
27
|
+
# Estimate the fuel used by a flight given any of the following optional characteristics:
|
28
|
+
# * <tt>airline</tt> - "american eagle"
|
29
|
+
# * <tt>aircraft</tt> - "b747"
|
30
|
+
# * <tt>origin_airport</tt> - "msn"
|
31
|
+
# * <tt>destination_airport</tt> - "iah"
|
32
|
+
# * <tt>segments_per_trip</tt> - 1 (defaults to 1 to force calculation as a one-segment trip)
|
33
|
+
# See http://carbon.brighterplanet.com/models/flight for more details
|
34
|
+
def fuel_use(attrs = {})
|
35
|
+
estimate :fuel_use, attrs
|
36
|
+
end
|
37
|
+
|
38
|
+
# Estimate the fuel efficiency (length / volume, not taking into account passengers) of a flight given any of the following optional characteristics:
|
39
|
+
# * <tt>airline</tt> - "american eagle"
|
40
|
+
# * <tt>aircraft</tt> - "b747"
|
41
|
+
# * <tt>origin_airport</tt> - "msn"
|
42
|
+
# * <tt>destination_airport</tt> - "iah"
|
43
|
+
# * <tt>segments_per_trip</tt> - 1 (defaults to 1 to force calculation as a one-segment trip)
|
44
|
+
# See http://carbon.brighterplanet.com/models/flight for more details
|
45
|
+
def fuel_efficiency(attrs = {})
|
46
|
+
d = estimate(:distance, attrs)
|
47
|
+
f = estimate(:fuel_use, attrs)
|
48
|
+
d / f
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module Energy
|
2
|
+
class RailTrip < Consumer
|
3
|
+
attr_accessor :distance
|
4
|
+
attr_accessor :rail_class
|
5
|
+
|
6
|
+
emit_as :rail_trip do
|
7
|
+
provide :distance, :as => :distance_estimate
|
8
|
+
provide :rail_class
|
9
|
+
end
|
10
|
+
|
11
|
+
def initialize(attrs = {}) # :nodoc:
|
12
|
+
attrs = attrs.symbolize_keys
|
13
|
+
@distance = attrs[:distance]
|
14
|
+
@rail_class = attrs[:rail_class]
|
15
|
+
end
|
16
|
+
|
17
|
+
class << self
|
18
|
+
# Estimate the diesel consumed by a rail trip given any of the following optional characteristics:
|
19
|
+
# * <tt>distance</tt> - 500 (kilometres)
|
20
|
+
# * <tt>rail_class</tt> - "intercity"
|
21
|
+
# See http://carbon.brighterplanet.com/models/rail_trip for more details
|
22
|
+
def fuel_use(attrs = {})
|
23
|
+
estimate :diesel_consumed, attrs
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
data/test/helper.rb
ADDED
data/test/test_energy.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require 'helper'
|
3
|
+
|
4
|
+
Energy.config[:key] = '456'
|
5
|
+
|
6
|
+
class TestEnergy < Test::Unit::TestCase
|
7
|
+
def test_001_automobile_trip_fuel_use
|
8
|
+
assert (1.5..3).include?(Energy::AutomobileTrip.fuel_use)
|
9
|
+
assert(Energy::AutomobileTrip.fuel_use(:make => 'Bentley') > Energy::AutomobileTrip.fuel_use(:make => 'Smart'))
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_002_flight_fuel_use
|
13
|
+
assert (18910..18920).include?(Energy::Flight.fuel_use)
|
14
|
+
assert(Energy::Flight.fuel_use(:origin_airport => 'MSN', :destination_airport =>'IAH') > Energy::Flight.fuel_use(:origin_airport => 'MSN', :destination_airport =>'ORD'))
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_003_rail_trip_fuel_use
|
18
|
+
assert (1640..1642).include?(Energy::RailTrip.fuel_use(:distance => 400, :rail_class => 'intercity'))
|
19
|
+
assert(Energy::RailTrip.fuel_use(:distance => 400) > Energy::RailTrip.fuel_use(:distance => 300))
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_004_flight_meta_fuel_efficiency
|
23
|
+
# american eagle's fuel efficiency is higher than continental's because we're not taking passengers into account
|
24
|
+
assert(Energy::Flight.fuel_efficiency(:airline => 'american eagle') > Energy::Flight.fuel_efficiency(:airline => 'continental airlines'))
|
25
|
+
end
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: energy
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Seamus Abshere
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-07-15 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: test-unit
|
16
|
+
requirement: &2157408560 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *2157408560
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: rake
|
27
|
+
requirement: &2157408140 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *2157408140
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: activesupport
|
38
|
+
requirement: &2157407720 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :runtime
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *2157407720
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: carbon
|
49
|
+
requirement: &2157407220 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 1.1.2
|
55
|
+
type: :runtime
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *2157407220
|
58
|
+
description: Get energy use estimates for flights, rail trips, and automobile trips
|
59
|
+
(more coming soon!). BETA from Brighter Planet.
|
60
|
+
email:
|
61
|
+
- seamus@abshere.net
|
62
|
+
executables: []
|
63
|
+
extensions: []
|
64
|
+
extra_rdoc_files: []
|
65
|
+
files:
|
66
|
+
- .gitignore
|
67
|
+
- Gemfile
|
68
|
+
- MIT-LICENSE.txt
|
69
|
+
- README.rdoc
|
70
|
+
- Rakefile
|
71
|
+
- energy.gemspec
|
72
|
+
- lib/energy.rb
|
73
|
+
- lib/energy/automobile_trip.rb
|
74
|
+
- lib/energy/config.rb
|
75
|
+
- lib/energy/consumer.rb
|
76
|
+
- lib/energy/flight.rb
|
77
|
+
- lib/energy/rail_trip.rb
|
78
|
+
- lib/energy/version.rb
|
79
|
+
- test/helper.rb
|
80
|
+
- test/test_energy.rb
|
81
|
+
homepage: https://github.com/brighterplanet/energy
|
82
|
+
licenses: []
|
83
|
+
post_install_message:
|
84
|
+
rdoc_options: []
|
85
|
+
require_paths:
|
86
|
+
- lib
|
87
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
88
|
+
none: false
|
89
|
+
requirements:
|
90
|
+
- - ! '>='
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '0'
|
93
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
94
|
+
none: false
|
95
|
+
requirements:
|
96
|
+
- - ! '>='
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '0'
|
99
|
+
requirements: []
|
100
|
+
rubyforge_project: energy
|
101
|
+
rubygems_version: 1.8.5
|
102
|
+
signing_key:
|
103
|
+
specification_version: 3
|
104
|
+
summary: Get energy use estimates for flights, rail trips, and automobile trips (more
|
105
|
+
coming soon!). BETA from Brighter Planet.
|
106
|
+
test_files:
|
107
|
+
- test/helper.rb
|
108
|
+
- test/test_energy.rb
|