Class: Plum::Response

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(auto_decode: true, **options) ⇒ Response

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Response.



9
10
11
12
13
14
15
# File 'lib/plum/client/response.rb', line 9

def initialize(auto_decode: true, **options)
  @body = Queue.new
  @finished = false
  @failed = false
  @body = []
  @auto_decode = auto_decode
end

Instance Attribute Details

#headersHash<String, String> (readonly)

The response headers

Returns:

  • (Hash<String, String>)


6
7
8
# File 'lib/plum/client/response.rb', line 6

def headers
  @headers
end

Instance Method Details

#[](key) ⇒ String

Returns the header value that correspond to the header name.

Parameters:

  • key (String)

    the header name

Returns:

  • (String)

    the header value



26
27
28
# File 'lib/plum/client/response.rb', line 26

def [](key)
  @headers[key.to_s.downcase]
end

#_chunk(encoded) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



80
81
82
83
84
85
86
87
# File 'lib/plum/client/response.rb', line 80

def _chunk(encoded)
  chunk = @decoder.decode(encoded)
  if @on_chunk
    @on_chunk.call(chunk)
  else
    @body << chunk
  end
end

#_failObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



97
98
99
# File 'lib/plum/client/response.rb', line 97

def _fail
  @failed = true
end

#_finishObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



90
91
92
93
94
# File 'lib/plum/client/response.rb', line 90

def _finish
  @finished = true
  @decoder.finish
  @on_finish.call if @on_finish
end

#_headers(raw_headers) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



73
74
75
76
77
# File 'lib/plum/client/response.rb', line 73

def _headers(raw_headers)
  # response headers should not have duplicates
  @headers = raw_headers.to_h.freeze
  @decoder = setup_decoder
end

#bodyString

Returns the complete response body. Use #each_body instead if the body can be very large.

Returns:

  • (String)

    the whole response body



66
67
68
69
70
# File 'lib/plum/client/response.rb', line 66

def body
  raise "Body already read" if @on_chunk
  raise "Response body is not complete" unless finished?
  @body.join
end

#failed?Boolean

Returns whether the request has failed or not.

Returns:

  • (Boolean)


38
39
40
# File 'lib/plum/client/response.rb', line 38

def failed?
  @failed
end

#finished?Boolean

Returns whether the response is complete or not.

Returns:

  • (Boolean)


32
33
34
# File 'lib/plum/client/response.rb', line 32

def finished?
  @finished
end

#on_chunk {|chunk| ... } ⇒ Object

Set callback tha called when received a chunk of response body.

Yields:

  • (chunk)

    A chunk of the response body.

Raises:

  • (ArgumentError)


44
45
46
47
48
49
50
51
52
# File 'lib/plum/client/response.rb', line 44

def on_chunk(&block)
  raise "Body already read" if @on_chunk
  raise ArgumentError, "block must be given" unless block_given?
  @on_chunk = block
  unless @body.empty?
    @body.each(&block)
    @body.clear
  end
end

#on_finish(&block) ⇒ Object

Set callback that will be called when the response finished.

Raises:

  • (ArgumentError)


55
56
57
58
59
60
61
62
# File 'lib/plum/client/response.rb', line 55

def on_finish(&block)
  raise ArgumentError, "block must be given" unless block_given?
  if finished?
    yield
  else
    @on_finish = block
  end
end

#statusString

Returns the HTTP status code.

Returns:

  • (String)

    the HTTP status code



19
20
21
# File 'lib/plum/client/response.rb', line 19

def status
  @headers && @headers[":status"]
end