Class: TinyClient::Response
- Inherits:
-
Object
- Object
- TinyClient::Response
- Defined in:
- lib/tiny_client/response.rb
Overview
Wrap the curl request response.
Instance Attribute Summary collapse
-
#body_str ⇒ Object
readonly
Returns the value of attribute body_str.
-
#code ⇒ Object
readonly
Returns the value of attribute code.
-
#header_str ⇒ Object
readonly
Returns the value of attribute header_str.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Instance Method Summary collapse
- #client_error? ⇒ Boolean
-
#error? ⇒ Boolean
True if the HTTP status code of this response correspond to an client or server error.
-
#gzip? ⇒ Boolean
True if this response Content-Encoding is gzip.
-
#initialize(curb) ⇒ Response
constructor
A new instance of Response.
-
#parse_body ⇒ Object
Convert the response json body into an hash.
- #redirect? ⇒ Boolean
- #server_error? ⇒ Boolean
-
#success? ⇒ Boolean
True if the http request has been successful.
- #to_s ⇒ Object
-
#total_count ⇒ Integer
Parse the X-Total-Count header.
Constructor Details
#initialize(curb) ⇒ Response
Returns a new instance of Response
9 10 11 12 13 14 15 |
# File 'lib/tiny_client/response.rb', line 9 def initialize(curb) @status = curb.status @body_str = curb.body_str @header_str = curb.header_str @code = @status.to_i @url = curb.url end |
Instance Attribute Details
#body_str ⇒ Object (readonly)
Returns the value of attribute body_str
7 8 9 |
# File 'lib/tiny_client/response.rb', line 7 def body_str @body_str end |
#code ⇒ Object (readonly)
Returns the value of attribute code
7 8 9 |
# File 'lib/tiny_client/response.rb', line 7 def code @code end |
#header_str ⇒ Object (readonly)
Returns the value of attribute header_str
7 8 9 |
# File 'lib/tiny_client/response.rb', line 7 def header_str @header_str end |
#status ⇒ Object (readonly)
Returns the value of attribute status
7 8 9 |
# File 'lib/tiny_client/response.rb', line 7 def status @status end |
#url ⇒ Object (readonly)
Returns the value of attribute url
7 8 9 |
# File 'lib/tiny_client/response.rb', line 7 def url @url end |
Instance Method Details
#client_error? ⇒ Boolean
46 47 48 |
# File 'lib/tiny_client/response.rb', line 46 def client_error? (400..499).cover?(@code) end |
#error? ⇒ Boolean
Returns true if the HTTP status code of this response correspond to an client or server error.
42 43 44 |
# File 'lib/tiny_client/response.rb', line 42 def error? @code >= 400 end |
#gzip? ⇒ Boolean
Returns true if this response Content-Encoding is gzip
32 33 34 |
# File 'lib/tiny_client/response.rb', line 32 def gzip? /Content-Encoding: gzip/ =~ header_str end |
#parse_body ⇒ Object
Convert the response json body into an hash.
19 20 21 22 |
# File 'lib/tiny_client/response.rb', line 19 def parse_body body = gzip? ? gzip_decompress : body_str ActiveSupport::JSON.decode(body) if body.present? end |
#redirect? ⇒ Boolean
54 55 56 |
# File 'lib/tiny_client/response.rb', line 54 def redirect? (300..399).cover?(@code) end |
#server_error? ⇒ Boolean
50 51 52 |
# File 'lib/tiny_client/response.rb', line 50 def server_error? @code >= 500 end |
#success? ⇒ Boolean
Returns true if the http request has been successful.
37 38 39 |
# File 'lib/tiny_client/response.rb', line 37 def success? (200..299).cover?(@code) end |
#to_s ⇒ Object
58 59 60 61 62 63 64 65 |
# File 'lib/tiny_client/response.rb', line 58 def to_s { url: url, status: status, body: body_str, headers: header_str }.to_s end |
#total_count ⇒ Integer
Parse the X-Total-Count header
26 27 28 29 |
# File 'lib/tiny_client/response.rb', line 26 def total_count count = header_str[/X-Total-Count: ([0-9]+)/, 1] count.present? ? count.to_i : nil end |