class TinyClient::Response

Wrap the curl request response.

Attributes

body_str[R]
code[R]
header_str[R]
status[R]
url[R]

Public Class Methods

new(curb) click to toggle source
# 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

Public Instance Methods

client_error?() click to toggle source
# File lib/tiny_client/response.rb, line 46
def client_error?
  (400..499).cover?(@code)
end
error?() click to toggle source

@return true if the HTTP status code of this response correspond to an client or server error.

# File lib/tiny_client/response.rb, line 42
def error?
  @code >= 400
end
gzip?() click to toggle source

@return true if this response Content-Encoding is gzip

# File lib/tiny_client/response.rb, line 32
def gzip?
  /Content-Encoding: gzip/ =~ header_str
end
parse_body() click to toggle source

Convert the response json body into an hash. @return the parsed response body

# 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?() click to toggle source
# File lib/tiny_client/response.rb, line 54
def redirect?
  (300..399).cover?(@code)
end
server_error?() click to toggle source
# File lib/tiny_client/response.rb, line 50
def server_error?
  @code >= 500
end
success?() click to toggle source

@return true if the http request has been successful.

# File lib/tiny_client/response.rb, line 37
def success?
  (200..299).cover?(@code)
end
to_s() click to toggle source
# 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() click to toggle source

Parse the X-Total-Count header @return [Integer] the value of the X-Total-Count header, or nil if not present

# 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

Protected Instance Methods

gzip_decompress() click to toggle source
# File lib/tiny_client/response.rb, line 69
def gzip_decompress
  ActiveSupport::Gzip.decompress(body_str)
end