Class: Freno::Client::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/freno/client/result.rb

Constant Summary collapse

FRENO_STATUS_CODE_MEANINGS =
{
  200 => :ok,
  404 => :not_found,
  417 => :expectation_failed,
  429 => :too_many_requests,
  500 => :internal_server_error
}.freeze
ADDITIONAL_STATUS_CODE_MEANINGS =

these are included to add resiliency to freno-client

{
  408 => :request_timeout
}.freeze
CODE_MEANINGS =
FRENO_STATUS_CODE_MEANINGS.merge(ADDITIONAL_STATUS_CODE_MEANINGS).freeze
MEANING_CODES =
CODE_MEANINGS.invert.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code, body = nil) ⇒ Result

Returns a new instance of Result.



35
36
37
38
39
# File 'lib/freno/client/result.rb', line 35

def initialize(code, body = nil)
  @code = code
  @meaning = CODE_MEANINGS[code] || :unknown
  @raw_body = body
end

Instance Attribute Details

#codeObject (readonly)

Returns the value of attribute code.



33
34
35
# File 'lib/freno/client/result.rb', line 33

def code
  @code
end

#meaningObject (readonly)

Returns the value of attribute meaning.



33
34
35
# File 'lib/freno/client/result.rb', line 33

def meaning
  @meaning
end

#raw_bodyObject (readonly)

Returns the value of attribute raw_body.



33
34
35
# File 'lib/freno/client/result.rb', line 33

def raw_body
  @raw_body
end

Class Method Details

.from_faraday_response(response) ⇒ Object



25
26
27
# File 'lib/freno/client/result.rb', line 25

def self.from_faraday_response(response)
  new(response.status, response.body)
end

.from_meaning(meaning) ⇒ Object



29
30
31
# File 'lib/freno/client/result.rb', line 29

def self.from_meaning(meaning)
  new(MEANING_CODES[meaning] || 0)
end

Instance Method Details

#==(other) ⇒ Object



57
58
59
60
61
# File 'lib/freno/client/result.rb', line 57

def ==(other)
  return meaning == other if other.is_a? Symbol

  code == other
end

#bodyObject



53
54
55
# File 'lib/freno/client/result.rb', line 53

def body
  @body ||= JSON.parse(raw_body) if raw_body
end

#failed?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/freno/client/result.rb', line 45

def failed?
  !ok?
end

#ok?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/freno/client/result.rb', line 41

def ok?
  meaning == :ok
end

#unkown?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/freno/client/result.rb', line 49

def unkown?
  meaning == :unkown
end