stoplight 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e500fe8c6d34e694cfbc2c46540a445eb997f89a
4
- data.tar.gz: ec03f4f69321194449e3e0918183c6cbea4f9e2b
3
+ metadata.gz: e5bb148c31a3c94f65125fba1628f7399755c74d
4
+ data.tar.gz: ed907f0fcfee16fbb9a760d863f47f0d89341d69
5
5
  SHA512:
6
- metadata.gz: 8b6f647cb561a37f14e44054b2549defcf1f72df27c0d8ba93c39d98b13d1b3f44c34b5c10070aa4d9c5d5d7c4b438983a532531bbad253605769b6a53925bdf
7
- data.tar.gz: 75a107deca63c8fca90ec6466f66b98524af37263750d77ae06526a280331f65de4a83e1647ad41ef568c6afa7e103241d070ebe5fedd23d6b41dcbf312514e2
6
+ metadata.gz: 73b02ee787610aef6491804b789c449fc91ed49ba6c511d800e7119178b120dcc9f87341838eb0b28ce4acb880ee2d67d956cedce1883c1cd49b76feccb5c699
7
+ data.tar.gz: 567b87080e6cd46487e09ea136319317aeebd6f0042f1b2e60aa5438d13f92cf99f2f42af76097609c35201903beac2c8d96f9ec912f5e7b220a25763ce8f2c1
data/CHANGELOG.md CHANGED
@@ -1,10 +1,25 @@
1
1
  # Changelog
2
2
 
3
+ ## v0.3.1 (2014-09-12)
4
+
5
+ - Replaced `Stoplight::Failure#error` with `#error_class` and `#error_message`.
6
+ Also changed the constructor to take the class and the message instead of the
7
+ error.
8
+
3
9
  ## v0.3.0 (2014-09-11)
4
10
 
5
11
  - Allowed formatting notifications.
6
12
  - Added automatic recovery from red lights.
7
13
  - Added `Stoplight::DataStore#clear_stale` for clearing stale lights.
14
+ - Removed forwarded methods on `Stoplight` module.
15
+ - Moved some methods from `Stoplight::DataStore::Base` to
16
+ `Stoplight::DataStore`.
17
+ - Renamed `Stoplight::DataStore::Base#purge` to `#clear_stale`.
18
+ - Renamed `Stoplight::DataStore::Base#delete` to `#clear`.
19
+ - Prefixed data store getters with `get_`.
20
+ - Added `Stoplight::DataStore::Base#sync`.
21
+ - Changed `Stoplight::DataStore::Base#get_failures` to return actual failures
22
+ (`Stoplight::Failure`) instead of strings.
8
23
 
9
24
  ## v0.2.1 (2014-08-20)
10
25
 
data/README.md CHANGED
@@ -30,7 +30,7 @@ Check out [stoplight-admin][12] for controlling your stoplights.
30
30
  Add it to your Gemfile:
31
31
 
32
32
  ``` rb
33
- gem 'stoplight', '~> 0.3.0'
33
+ gem 'stoplight', '~> 0.3.1'
34
34
  ```
35
35
 
36
36
  Or install it manually:
@@ -225,7 +225,8 @@ Stoplight::Error::RedLight: example-6
225
225
  ### Custom timeout
226
226
 
227
227
  Stoplights will automatically attempt to recover after a certain amount of time.
228
- This timeout is customizable.
228
+ A light in the red state for longer than the timeout will transition to the
229
+ yellow state. This timeout is customizable.
229
230
 
230
231
  ``` irb
231
232
  >> light = Stoplight::Light.new('example-7') { fail }.
data/lib/stoplight.rb CHANGED
@@ -15,7 +15,7 @@ require 'stoplight/notifier/standard_error'
15
15
 
16
16
  module Stoplight
17
17
  # @return [Gem::Version]
18
- VERSION = Gem::Version.new('0.3.0')
18
+ VERSION = Gem::Version.new('0.3.1')
19
19
 
20
20
  @data_store = DataStore::Memory.new
21
21
  @notifiers = [Notifier::StandardError.new]
@@ -1,36 +1,51 @@
1
1
  # coding: utf-8
2
2
 
3
3
  require 'json'
4
+ require 'time'
4
5
 
5
6
  module Stoplight
6
7
  class Failure
7
- # @return [Exception]
8
- attr_reader :error
8
+ # @return [String]
9
+ attr_reader :error_class
10
+
11
+ # @return [String]
12
+ attr_reader :error_message
9
13
 
10
14
  # @return [Time]
11
15
  attr_reader :time
12
16
 
17
+ # @param error [Exception]
18
+ def self.create(error)
19
+ new(error.class.name, error.message)
20
+ end
21
+
22
+ # @param json [String]
13
23
  def self.from_json(json)
14
24
  h = JSON.parse(json)
15
-
16
- match = /#<(.+): (.+)>/.match(h['error'])
17
- error = Object.const_get(match[1]).new(match[2]) if match
18
-
19
- time = Time.parse(h['time'])
20
-
21
- new(error, time)
25
+ new(
26
+ h['error']['class'],
27
+ h['error']['message'],
28
+ Time.parse(h['time']))
29
+ rescue => error
30
+ new(Error::InvalidFailure.name, error.message)
22
31
  end
23
32
 
24
- # @param error [Exception]
25
- def initialize(error, time = nil)
26
- @error = error
33
+ # @param error_class [String]
34
+ # @param error_message [String]
35
+ # @param time [Time, nil]
36
+ def initialize(error_class, error_message, time = nil)
37
+ @error_class = error_class
38
+ @error_message = error_message
27
39
  @time = time || Time.now
28
40
  end
29
41
 
30
42
  def to_json(*args)
31
43
  {
32
- error: @error.inspect,
33
- time: time.inspect
44
+ error: {
45
+ class: error_class,
46
+ message: error_message
47
+ },
48
+ time: time.iso8601
34
49
  }.to_json(*args)
35
50
  end
36
51
  end
@@ -131,7 +131,7 @@ module Stoplight
131
131
  if error_allowed?(error)
132
132
  Stoplight.data_store.clear_failures(name)
133
133
  else
134
- Stoplight.data_store.record_failure(name, Failure.new(error))
134
+ Stoplight.data_store.record_failure(name, Failure.create(error))
135
135
  end
136
136
  end
137
137
 
@@ -3,25 +3,56 @@
3
3
  require 'spec_helper'
4
4
 
5
5
  describe Stoplight::Failure do
6
- subject(:failure) { described_class.new(error, time) }
7
- let(:error) { error_class.new(error_message) }
8
- let(:error_class) { StandardError }
6
+ subject(:failure) { described_class.new(error_class, error_message, time) }
7
+ let(:error_class) { SecureRandom.hex }
9
8
  let(:error_message) { SecureRandom.hex }
10
9
  let(:time) { Time.now }
11
10
 
11
+ describe '.create' do
12
+ subject(:result) { described_class.create(error) }
13
+ let(:error) { error_class.new(error_message) }
14
+ let(:error_class) { Class.new(StandardError) }
15
+
16
+ it 'creates a failure' do
17
+ expect(result).to be_a(Stoplight::Failure)
18
+ expect(result.error_class).to eql(error.class.name)
19
+ expect(result.error_message).to eql(error.message)
20
+ expect(result.time).to be_within(1).of(Time.now)
21
+ end
22
+ end
23
+
12
24
  describe '.from_json' do
13
25
  subject(:result) { described_class.from_json(json) }
14
26
  let(:json) { failure.to_json }
15
27
 
16
- it do
17
- expect(result.error).to eq(failure.error)
28
+ it 'can be round-tripped' do
29
+ expect(result.error_class).to eq(failure.error_class)
30
+ expect(result.error_message).to eq(failure.error_message)
18
31
  expect(result.time).to be_within(1).of(failure.time)
19
32
  end
33
+
34
+ context 'with invalid JSON' do
35
+ let(:json) { nil }
36
+
37
+ it 'does not raise an error' do
38
+ expect { result }.to_not raise_error
39
+ end
40
+
41
+ it 'returns a self-describing invalid failure' do
42
+ expect(result.error_class).to eq('Stoplight::Error::InvalidFailure')
43
+ expect(result.error_message).to end_with('nil into String')
44
+ expect(result.time).to be_within(1).of(Time.now)
45
+ end
46
+ end
20
47
  end
21
48
 
22
49
  describe '#initialize' do
23
- it 'sets the error' do
24
- expect(failure.error).to eql(error)
50
+ it 'sets the error class' do
51
+ expect(failure.error_class).to eql(error_class)
52
+ end
53
+
54
+ it 'sets the error message' do
55
+ expect(failure.error_message).to eql(error_message)
25
56
  end
26
57
 
27
58
  it 'sets the time' do
@@ -40,10 +71,12 @@ describe Stoplight::Failure do
40
71
  describe '#to_json' do
41
72
  subject(:json) { failure.to_json }
42
73
  let(:data) { JSON.load(json) }
74
+ let(:time) { Time.utc(2001, 2, 3, 4, 5, 6) }
43
75
 
44
76
  it 'converts to JSON' do
45
- expect(data['error']).to eql(error.inspect)
46
- expect(data['time']).to eql(time.inspect)
77
+ expect(data['error']['class']).to eql(error_class)
78
+ expect(data['error']['message']).to eql(error_message)
79
+ expect(data['time']).to eql('2001-02-03T04:05:06Z')
47
80
  end
48
81
  end
49
82
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stoplight
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cameron Desautels
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-09-11 00:00:00.000000000 Z
12
+ date: 2014-09-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: benchmark-ips