Class: Stoplight::Failure

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

Overview

rubocop:disable Style/Documentation

Constant Summary collapse

TIME_FORMAT =
'%Y-%m-%dT%H:%M:%S.%N%:z'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(error_class, error_message, time) ⇒ Failure

Returns a new instance of Failure

Parameters:

  • error_class (String)
  • error_message (String)
  • time (Time)

41
42
43
44
45
# File 'lib/stoplight/failure.rb', line 41

def initialize(error_class, error_message, time)
  @error_class = error_class
  @error_message = error_message
  @time = time
end

Instance Attribute Details

#error_classString (readonly)

Returns:

  • (String)

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

def error_class
  @error_class
end

#error_messageString (readonly)

Returns:

  • (String)

13
14
15
# File 'lib/stoplight/failure.rb', line 13

def error_message
  @error_message
end

#timeTime (readonly)

Returns:

  • (Time)

15
16
17
# File 'lib/stoplight/failure.rb', line 15

def time
  @time
end

Class Method Details

.from_error(error) ⇒ Failure

Returns a new instance of Failure

Parameters:

  • error (Exception)

Returns:

  • (Failure)

    a new instance of Failure


19
20
21
# File 'lib/stoplight/failure.rb', line 19

def self.from_error(error)
  new(error.class.name, error.message, Time.now)
end

.from_json(json) ⇒ Failure

Returns a new instance of Failure

Parameters:

  • json (String)

Returns:

  • (Failure)

    a new instance of Failure

Raises:

  • (JSON::ParserError)
  • (ArgumentError)

27
28
29
30
31
32
33
34
35
36
# File 'lib/stoplight/failure.rb', line 27

def self.from_json(json)
  object = JSON.parse(json)
  error_object = object['error']

  error_class = error_object['class']
  error_message = error_object['message']
  time = Time.parse(object['time'])

  new(error_class, error_message, time)
end

Instance Method Details

#==(other) ⇒ Boolean

Parameters:

Returns:

  • (Boolean)

49
50
51
52
53
# File 'lib/stoplight/failure.rb', line 49

def ==(other)
  error_class == other.error_class &&
    error_message == other.error_message &&
    time == other.time
end

#to_jsonString

Returns:

  • (String)

56
57
58
59
60
61
62
63
64
# File 'lib/stoplight/failure.rb', line 56

def to_json
  JSON.generate(
    error: {
      class: error_class,
      message: error_message
    },
    time: time.strftime(TIME_FORMAT)
  )
end