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
True if the HTTP status code of this response correspond to an client error.
-
#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.
-
#not_found_error? ⇒ Boolean
True if the HTTP status code of this response is 404.
-
#parse_body ⇒ Object
Convert the response json body into an hash.
-
#redirect? ⇒ Boolean
True if the HTTP status code of this response correspond to a redirect.
-
#server_error? ⇒ Boolean
True if the HTTP status code of this response correspond to a server error.
-
#success? ⇒ Boolean
True if the http request has been successful.
-
#to_hash ⇒ Object
Hash with url, status, body and headers fields.
-
#to_s ⇒ Object
String of #to_hash.
-
#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) @url = curb.url @body_str = curb.body_str @header_str = curb.header_str @status = curb.status @code = @status.to_i 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
Returns true if the HTTP status code of this response correspond to an client error.
52 53 54 |
# File 'lib/tiny_client/response.rb', line 52 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 |
#not_found_error? ⇒ Boolean
Returns true if the HTTP status code of this response is 404.
47 48 49 |
# File 'lib/tiny_client/response.rb', line 47 def not_found_error? @code == 404 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
Returns true if the HTTP status code of this response correspond to a redirect.
62 63 64 |
# File 'lib/tiny_client/response.rb', line 62 def redirect? (300..399).cover?(@code) end |
#server_error? ⇒ Boolean
Returns true if the HTTP status code of this response correspond to a server error.
57 58 59 |
# File 'lib/tiny_client/response.rb', line 57 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_hash ⇒ Object
Returns Hash with url, status, body and headers fields.
67 68 69 70 71 72 73 74 |
# File 'lib/tiny_client/response.rb', line 67 def to_hash { 'url' => url, 'status' => status, 'body' => (parse_body rescue body_str), 'headers' => (parse_headers rescue header_str) } end |
#to_s ⇒ Object
Returns String of #to_hash.
77 78 79 |
# File 'lib/tiny_client/response.rb', line 77 def to_s to_hash.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 |