carbon 2.2.2 → 2.2.3

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,13 @@
1
+ 2.2.3 / 2012-04-12
2
+
3
+ * Enhancements
4
+
5
+ * Raise an ArgumentError if you make the common mistake of calling Carbon.query(array) {} instead of Carbon.query(array).each {}
6
+
7
+ * Bug fixes
8
+
9
+ * Fix how shell gets characteristics for an emitter
10
+
1
11
  2.2.2 / 2012-04-05
2
12
 
3
13
  * Enhancements
@@ -95,6 +95,8 @@ module Carbon
95
95
  #
96
96
  # @raise [ArgumentError] If your arguments don't match any of the method signatures.
97
97
  #
98
+ # @raise [ArgumentError] If you try to pass a block - you probably want +Carbon.query(array).each {}+ or something.
99
+ #
98
100
  # @example A flight taken in 2009
99
101
  # Carbon.query('Flight', :origin_airport => 'MSN', :destination_airport => 'ORD', :date => '2009-01-01', :timeframe => Timeframe.new(:year => 2009), :comply => [:tcr])
100
102
  #
@@ -132,6 +134,7 @@ module Carbon
132
134
  # puts "Carbon emitter by #{car_or_flight} was #{impact.decisions.carbon.object.value.round(1)}"
133
135
  # end
134
136
  def Carbon.query(*args)
137
+ raise ::ArgumentError, "Don't pass a block directly - instead use Carbon.query(array).each (for example)." if block_given?
135
138
  case Carbon.method_signature(*args)
136
139
  when :plain_query
137
140
  plain_query = args
@@ -1,11 +1,22 @@
1
1
  require 'uri'
2
2
  require 'net/http'
3
3
  require 'conversions'
4
+ require 'cache_method'
4
5
 
5
6
  module Carbon
6
7
  class Shell
7
8
  # @private
8
9
  class Emitter < Bombshell::Environment
10
+ class << self
11
+ # @private
12
+ def characteristics(emitter)
13
+ ::MultiJson.decode ::Net::HTTP.get(::URI.parse("http://impact.brighterplanet.com/#{emitter.underscore.pluralize}/options.json"))
14
+ rescue
15
+ # oops
16
+ end
17
+ cache_method :characteristics, 300
18
+ end
19
+
9
20
  include Bombshell::Shell
10
21
  include Carbon
11
22
 
@@ -13,10 +24,8 @@ module Carbon
13
24
  def initialize(name, input = {})
14
25
  @emitter = name.to_s.singularize.camelcase
15
26
  @input = input
16
- response = ::Net::HTTP.get_response(::URI.parse("http://impact.brighterplanet.com/#{@emitter.underscore.pluralize}/options.json"))
17
- if (200..299).include?(response.code.to_i)
18
- @characteristics = ::MultiJson.decode response.body
19
- @characteristics.keys.each do |characteristic|
27
+ if characteristics = Emitter.characteristics(@emitter)
28
+ characteristics.each do |characteristic|
20
29
  instance_eval <<-meth
21
30
  def #{characteristic}(arg = nil)
22
31
  if arg
@@ -28,12 +37,12 @@ module Carbon
28
37
  end
29
38
  meth
30
39
  end
31
- provisions = @characteristics.keys.map { |k| "provide :#{k}"}.join('; ')
40
+ provisions = characteristics.map { |k| "provide :#{k}"}.join('; ')
32
41
  emit_as_block = "emit_as(:#{name}) { #{provisions} }"
33
42
  self.class.class_eval emit_as_block
34
43
  emission
35
44
  else
36
- puts " => Sorry, characteristics couldn't be retrieved for #{@emitter.underscore.pluralize} (via #{url})"
45
+ puts " => Sorry, characteristics couldn't be retrieved for #{@emitter.underscore.pluralize}. Please try again later."
37
46
  done
38
47
  end
39
48
  end
@@ -123,7 +132,7 @@ module Carbon
123
132
 
124
133
  # @private
125
134
  def help
126
- puts " => #{@characteristics.keys.join ', '}"
135
+ puts " => #{Emitter.characteristics(@emitter).join ', '}"
127
136
  end
128
137
 
129
138
  prompt_with do |emitter|
@@ -1,3 +1,3 @@
1
1
  module Carbon
2
- VERSION = "2.2.2"
2
+ VERSION = "2.2.3"
3
3
  end
@@ -126,6 +126,11 @@ describe Carbon do
126
126
  it "doesn't hang up on 0 queries" do
127
127
  Timeout.timeout(0.5) { Carbon.query([]) }.must_equal(Hash.new)
128
128
  end
129
+ it "raises if you pass it a block directly" do
130
+ lambda {
131
+ Carbon.query([]) { }
132
+ }.must_raise(ArgumentError)
133
+ end
129
134
  it "can be used on objects that respond to #as_impact_query" do
130
135
  a = MyNissanAltima.new(2001)
131
136
  b = MyNissanAltima.new(2006)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: carbon
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.2
4
+ version: 2.2.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-04-06 00:00:00.000000000 Z
12
+ date: 2012-04-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport