carbon 0.1.2 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -47,6 +47,30 @@ When you want to calculate emissions, simply call <tt>RentalCar</tt>#<tt>emissio
47
47
 
48
48
  Read the {carbon gem RDoc}[http://rdoc.info/projects/brighterplanet/carbon] for more!
49
49
 
50
+ == Exceptions to watch out for
51
+
52
+ Since this gem connects to a web service, you need to be ready for network problems and latency. For example:
53
+
54
+ begin
55
+ my_emission = my_car.emission
56
+ rescue ::SocketError, ::Timeout::Error, ::Errno::ETIMEDOUT, ::Errno::ENETUNREACH, ::Errno::ECONNRESET, ::Errno::ECONNREFUSED
57
+ # These are general network errors raised by Net::HTTP.
58
+ # Your internet connection might be down, or our servers might be down.
59
+ rescue ::Carbon::RateLimited
60
+ # Realtime mode only.
61
+ # In order to prevent denial-of-service attacks, our servers rate limit requests.
62
+ # The gem will try up to three times to get an answer back from the server, waiting slightly longer each time.
63
+ # If you still get this exception, please contact us at staff@brighterplanet.com and we'll lift your rate.
64
+ rescue ::Carbon::RealtimeEstimateFailed
65
+ # Realtime mode only.
66
+ # Our server returned a 4XX or 5XX error.
67
+ # Please contact us at staff@brighterplanet.com if you get these more than a couple times.
68
+ rescue ::Carbon::QueueingFailed
69
+ # Async mode only.
70
+ # The gem connects directly to Amazon SQS in order to provide maximum throughput. If that service returns anything other than success, you get this exception.
71
+ # Please contact us at staff@brighterplanet.com if you see too many of these.
72
+ end
73
+
50
74
  == A note on API keys
51
75
 
52
76
  You should get an API key from http://keys.brighterplanet.com and set it globally:
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.2
1
+ 0.1.3
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{carbon}
8
- s.version = "0.1.2"
8
+ s.version = "0.1.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Derek Kastner", "Seamus Abshere"]
12
- s.date = %q{2010-07-19}
12
+ s.date = %q{2010-07-20}
13
13
  s.description = %q{Carbon allows you to easily calculate the carbon footprint of various activities. This is an API for the Brighter Planet Carbon Middleware service.}
14
14
  s.email = %q{derek.kastner@brighterplanet.com}
15
15
  s.extra_rdoc_files = [
@@ -170,7 +170,19 @@ module Carbon
170
170
  end
171
171
 
172
172
  def _realtime_emission(options = {}) # :nodoc:
173
- response = _carbon_response options
173
+ attempts = 0
174
+ begin
175
+ response = _carbon_response options
176
+ raise ::Carbon::RateLimited if response.status_code == 403 and response.body =~ /Rate Limit/i
177
+ rescue ::Carbon::RateLimited
178
+ if attempts < 4
179
+ attempts += 1
180
+ sleep 0.2 * attempts
181
+ retry
182
+ else
183
+ raise $!, "Rate limited #{attempts} time(s) in a row"
184
+ end
185
+ end
174
186
  raise ::Carbon::RealtimeEstimateFailed unless response.success?
175
187
  ::Carbon::EmissionEstimate.new ::ActiveSupport::JSON.decode(response.body)
176
188
  end
@@ -206,6 +218,8 @@ module Carbon
206
218
  # Returns an emission estimate.
207
219
  #
208
220
  # Note: <b>You need to take care of storing the return value to a local variable!</b> Every call to <tt>emission</tt> runs a query.
221
+ #
222
+ # Note also: please see the README about exceptions that you should watch out for.
209
223
  #
210
224
  # You can use it like a number...
211
225
  # > my_car.emission + 5.1
@@ -94,7 +94,7 @@ describe Carbon do
94
94
  it 'should post a message to SQS' do
95
95
  c = RentalCar.new
96
96
  c._carbon_request_url.should =~ /queue.amazonaws.com/
97
- c.emission :timeframe => Timeframe.new(:year => 2009), :callback => 'http://www.postbin.org/1dj0145'
97
+ c.emission :timeframe => Timeframe.new(:year => 2009), :callback => 'http://www.postbin.org/1dj0146'
98
98
  end
99
99
  end
100
100
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: carbon
3
3
  version: !ruby/object:Gem::Version
4
- hash: 31
4
+ hash: 29
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 2
10
- version: 0.1.2
9
+ - 3
10
+ version: 0.1.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Derek Kastner
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2010-07-19 00:00:00 -05:00
19
+ date: 2010-07-20 00:00:00 -05:00
20
20
  default_executable:
21
21
  dependencies:
22
22
  - !ruby/object:Gem::Dependency