Class: Monri::ApiHttpResponse
- Inherits:
-
Object
- Object
- Monri::ApiHttpResponse
- Defined in:
- lib/monri/api_http_response.rb
Instance Attribute Summary collapse
- #body ⇒ Hash readonly
- #code ⇒ Integer readonly
- #exception ⇒ Exception readonly
- #headers ⇒ Hash readonly
- #response_body ⇒ String readonly
Instance Method Summary collapse
Instance Attribute Details
#body ⇒ Hash (readonly)
5 6 7 |
# File 'lib/monri/api_http_response.rb', line 5 def body @body end |
#code ⇒ Integer (readonly)
9 10 11 |
# File 'lib/monri/api_http_response.rb', line 9 def code @code end |
#exception ⇒ Exception (readonly)
13 14 15 |
# File 'lib/monri/api_http_response.rb', line 13 def exception @exception end |
#headers ⇒ Hash (readonly)
11 12 13 |
# File 'lib/monri/api_http_response.rb', line 11 def headers @headers end |
#response_body ⇒ String (readonly)
7 8 9 |
# File 'lib/monri/api_http_response.rb', line 7 def response_body @response_body end |
Instance Method Details
#failed? ⇒ Boolean
38 39 40 |
# File 'lib/monri/api_http_response.rb', line 38 def failed? exception != nil end |
#from_net(response) ⇒ Monri::ApiHttpResponse
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/monri/api_http_response.rb', line 17 def from_net(response) @code = response.code.to_i @headers = response.each_header.to_h @response_body = response.body @body = begin JSON.parse(response.body, symbolize_names: true) rescue @exception = create_exception('JSON parsing failed') nil end unless success? @exception = create_exception("Request failed, got status code=#{code}") end self end |
#success? ⇒ Boolean
34 35 36 |
# File 'lib/monri/api_http_response.rb', line 34 def success? exception == nil && code >= 200 && code < 300 end |