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)
  @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_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:

  • (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.

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

#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:

  • (Boolean)


54
55
56
# File 'lib/tiny_client/response.rb', line 54

def redirect?
  (300..399).cover?(@code)
end

#server_error?Boolean

Returns:

  • (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.

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_sObject



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_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