Class: Pcli::ApiResponse
- Inherits:
-
Object
- Object
- Pcli::ApiResponse
- Defined in:
- lib/pcli/api_response.rb
Defined Under Namespace
Classes: KnownError
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#code ⇒ Object
readonly
Returns the value of attribute code.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
Instance Method Summary collapse
- #error ⇒ Object
- #failure? ⇒ Boolean
-
#initialize(code, success_status, status, body) ⇒ ApiResponse
constructor
A new instance of ApiResponse.
- #json ⇒ Object
- #known_error? ⇒ Boolean
- #state ⇒ Object
- #success? ⇒ Boolean
- #unknown_error? ⇒ Boolean
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
#body ⇒ Object (readonly)
Returns the value of attribute body.
7 8 9 |
# File 'lib/pcli/api_response.rb', line 7 def body @body end |
#code ⇒ Object (readonly)
Returns the value of attribute code.
7 8 9 |
# File 'lib/pcli/api_response.rb', line 7 def code @code end |
#status ⇒ Object (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
#error ⇒ Object
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
44 45 46 |
# File 'lib/pcli/api_response.rb', line 44 def failure? !success? end |
#json ⇒ Object
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
36 37 38 |
# File 'lib/pcli/api_response.rb', line 36 def known_error? state == :error end |
#state ⇒ Object
28 29 30 |
# File 'lib/pcli/api_response.rb', line 28 def state @state ||= calculate_state end |
#success? ⇒ Boolean
32 33 34 |
# File 'lib/pcli/api_response.rb', line 32 def success? state == :success end |
#unknown_error? ⇒ Boolean
40 41 42 |
# File 'lib/pcli/api_response.rb', line 40 def unknown_error? state == :unknown_error end |