Class: Clients::HttpClient::Response

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/clients/http_client/response.rb

Constant Summary collapse

DEFAULT_ENCODING =
Encoding::UTF_8

Instance Method Summary collapse

Instance Method Details

#fail?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/clients/http_client/response.rb', line 15

def fail?
  !success?
end

#stream(size = HTTP::Connection::BUFFER_SIZE) ⇒ Object



50
51
52
53
54
# File 'lib/clients/http_client/response.rb', line 50

def stream(size = HTTP::Connection::BUFFER_SIZE)
  while (chunk = object.body.readpartial(size))
    yield chunk
  end
end

#success?Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/clients/http_client/response.rb', line 11

def success?
  object.status.success?
end

#to_html(**kargs) ⇒ Object



34
35
36
# File 'lib/clients/http_client/response.rb', line 34

def to_html(**kargs)
  Nokogiri::HTML.parse to_s(**kargs)
end

#to_ioObject



46
47
48
# File 'lib/clients/http_client/response.rb', line 46

def to_io
  StringIO.new(to_s)
end

#to_json(**kargs) ⇒ Object



42
43
44
# File 'lib/clients/http_client/response.rb', line 42

def to_json(**kargs)
  JSON.parse to_s(**kargs), symbolize_names: true
end

#to_s(force_utf8: false) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/clients/http_client/response.rb', line 19

def to_s(force_utf8: false)
  response = object.to_s
  return response unless force_utf8

  if object.charset
    response
      .encode(DEFAULT_ENCODING)
      .scrub("_")
  else
    response
      .force_encoding(DEFAULT_ENCODING)
      .scrub("_")
  end
end

#to_xml(**kargs) ⇒ Object



38
39
40
# File 'lib/clients/http_client/response.rb', line 38

def to_xml(**kargs)
  Nokogiri::XML.parse to_s(**kargs)
end