Class: DPN::Client::Response
- Inherits:
-
Object
- Object
- DPN::Client::Response
- Defined in:
- lib/dpn/client/response.rb
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
Class Method Summary collapse
-
.from_data(status, body) ⇒ Object
Manually create a response.
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
- #[](key) ⇒ Object
-
#initialize(httpclient_response = nil) ⇒ Response
constructor
A new instance of Response.
- #json ⇒ Object (also: #to_json, #to_s)
- #keys ⇒ Object
- #load_from_data!(status, body) ⇒ Object
- #load_from_response!(httpclient_message_response) ⇒ Object
- #ok? ⇒ Boolean (also: #success?)
Constructor Details
#initialize(httpclient_response = nil) ⇒ Response
Returns a new instance of Response.
15 16 17 18 19 |
# File 'lib/dpn/client/response.rb', line 15 def initialize(httpclient_response = nil) if httpclient_response load_from_response!(httpclient_response) end end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
13 14 15 |
# File 'lib/dpn/client/response.rb', line 13 def body @body end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
13 14 15 |
# File 'lib/dpn/client/response.rb', line 13 def status @status end |
Class Method Details
.from_data(status, body) ⇒ Object
Manually create a response.
23 24 25 |
# File 'lib/dpn/client/response.rb', line 23 def self.from_data(status, body) self.new.load_from_data!(status, body) end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
77 78 79 |
# File 'lib/dpn/client/response.rb', line 77 def ==(other) status == other.status && body == other.body end |
#[](key) ⇒ Object
46 47 48 |
# File 'lib/dpn/client/response.rb', line 46 def [](key) @body[key.to_sym] end |
#json ⇒ Object Also known as: to_json, to_s
28 29 30 |
# File 'lib/dpn/client/response.rb', line 28 def json @cached_json ||= @body.to_json end |
#keys ⇒ Object
41 42 43 |
# File 'lib/dpn/client/response.rb', line 41 def keys @body.keys end |
#load_from_data!(status, body) ⇒ Object
51 52 53 54 55 |
# File 'lib/dpn/client/response.rb', line 51 def load_from_data!(status, body) @body = body @status = status return self end |
#load_from_response!(httpclient_message_response) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/dpn/client/response.rb', line 57 def load_from_response!() raw_body = .body @status = .header.status_code begin @body = JSON.parse(raw_body, symbolize_names: true) @cached_json = raw_body rescue JSON::ParserError @body = { status: @status, parsed: nil, raw: raw_body } @cached_json = @body.to_json if success? # It wasn't actually successful @status = 999 end end return self end |
#ok? ⇒ Boolean Also known as: success?
35 36 37 |
# File 'lib/dpn/client/response.rb', line 35 def ok? [ 200, 201, 202, 203, 204, 205, 206, 207, 208, 226].include?(@status) end |