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.



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

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.



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

def code
  @code
end

#meaningObject (readonly)

Returns the value of attribute meaning.



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

def meaning
  @meaning
end

#raw_bodyObject (readonly)

Returns the value of attribute raw_body.



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

def raw_body
  @raw_body
end

Class Method Details

.from_faraday_response(response) ⇒ Object



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

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

.from_meaning(meaning) ⇒ Object



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

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

Instance Method Details

#==(other) ⇒ Object



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

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

#bodyObject



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

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

#failed?Boolean

Returns:

  • (Boolean)


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

def failed?
  !ok?
end

#ok?Boolean

Returns:

  • (Boolean)


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

def ok?
  meaning == :ok
end

#unkown?Boolean

Returns:

  • (Boolean)


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

def unkown?
  meaning == :unkown
end