Class: CircuitBreaker::Failure

Inherits:
Object
  • Object
show all
Defined in:
lib/circuit_breaker/failure.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(error, recorded = Time.now.utc) ⇒ Failure

Returns a new instance of Failure.



11
12
13
14
# File 'lib/circuit_breaker/failure.rb', line 11

def initialize(error, recorded = Time.now.utc)
  @error = error
  @recorded = recorded
end

Class Method Details

.from_json(json) ⇒ Object



4
5
6
7
8
9
# File 'lib/circuit_breaker/failure.rb', line 4

def self.from_json(json)
  failure = JSON.parse(json)
  error = Object.const_get(failure["error"])
  error = error.new(failure["message"])
  new(error, Time.parse(failure["timestamp"]))
end

Instance Method Details

#timestampObject



16
17
18
# File 'lib/circuit_breaker/failure.rb', line 16

def timestamp
  recorded
end

#to_jsonObject



24
25
26
27
28
29
30
# File 'lib/circuit_breaker/failure.rb', line 24

def to_json
  JSON.generate({
    error: error.class,
    message: error.message,
    timestamp: timestamp.to_s
  })
end

#to_sObject



20
21
22
# File 'lib/circuit_breaker/failure.rb', line 20

def to_s
  "[#{error.class}] - #{error.message}"
end