Class: Pcli::ApiResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/pcli/api_response.rb

Defined Under Namespace

Classes: KnownError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code, success_status, status, body) ⇒ ApiResponse

Returns a new instance of ApiResponse.



9
10
11
12
13
14
# File 'lib/pcli/api_response.rb', line 9

def initialize(code, success_status, status, body)
  @code = code
  @success_status = success_status
  @status = status
  @body = body
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



7
8
9
# File 'lib/pcli/api_response.rb', line 7

def body
  @body
end

#codeObject (readonly)

Returns the value of attribute code.



7
8
9
# File 'lib/pcli/api_response.rb', line 7

def code
  @code
end

#statusObject (readonly)

Returns the value of attribute status.



7
8
9
# File 'lib/pcli/api_response.rb', line 7

def status
  @status
end

Instance Method Details

#errorObject



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/pcli/api_response.rb', line 48

def error
  return @error unless @error.nil?

  return @error = false unless state == :error

  @error = KnownError.new(
    json['status'],
    json.dig('error', 'type'),
    json.dig('error', 'title'),
    json.dig('error', 'message')
  )
end

#failure?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/pcli/api_response.rb', line 44

def failure?
  !success?
end

#jsonObject



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/pcli/api_response.rb', line 16

def json
  if @json.nil?
    begin
      @json = JSON.parse(body)
    rescue JSON::ParserError
      @json = false
    end
  end

  @json
end

#known_error?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/pcli/api_response.rb', line 36

def known_error?
  state == :error
end

#stateObject



28
29
30
# File 'lib/pcli/api_response.rb', line 28

def state
  @state ||= calculate_state
end

#success?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/pcli/api_response.rb', line 32

def success?
  state == :success
end

#unknown_error?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/pcli/api_response.rb', line 40

def unknown_error?
  state == :unknown_error
end