Class: TinyClient::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/tiny_client/response.rb

Overview

Wrap the curl request response.

Instance Attribute Summary collapse

Instance Method Summary collapse

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_strObject (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

#codeObject (readonly)

Returns the value of attribute code.



7
8
9
# File 'lib/tiny_client/response.rb', line 7

def code
  @code
end

#header_strObject (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

#statusObject (readonly)

Returns the value of attribute status.



7
8
9
# File 'lib/tiny_client/response.rb', line 7

def status
  @status
end

#urlObject (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.

Returns:

  • (Boolean)

    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.

Returns:

  • (Boolean)

    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.

Returns:

  • (Boolean)

    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.

Returns:

  • (Boolean)

    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_bodyObject

Convert the response json body into an hash.

Returns:

  • the parsed response body



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.

Returns:

  • (Boolean)

    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.

Returns:

  • (Boolean)

    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.

Returns:

  • (Boolean)

    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_hashObject

Returns Hash with url, status, body and headers fields.

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_sObject

Returns String of #to_hash.

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_countInteger

Parse the X-Total-Count header

Returns:

  • (Integer)

    the value of the X-Total-Count header, or nil if not present



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