Class: HTTPX::StreamResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/httpx/plugins/stream.rb

Instance Method Summary collapse

Constructor Details

#initialize(request, session) ⇒ StreamResponse

Returns a new instance of StreamResponse.



5
6
7
8
9
# File 'lib/httpx/plugins/stream.rb', line 5

def initialize(request, session)
  @request = request
  @session = session
  @response = nil
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object (private)



84
85
86
87
88
# File 'lib/httpx/plugins/stream.rb', line 84

def method_missing(meth, *args, &block)
  return super unless response.respond_to?(meth)

  response.__send__(meth, *args, &block)
end

Instance Method Details

#each(&block) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/httpx/plugins/stream.rb', line 11

def each(&block)
  return enum_for(__method__) unless block

  @request.stream = self

  begin
    @on_chunk = block

    if @request.response
      # if we've already started collecting the payload, yield it first
      # before proceeding.
      body = @request.response.body

      body.each do |chunk|
        on_chunk(chunk)
      end
    end

    response.raise_for_status
  ensure
    @on_chunk = nil
  end
end

#each_line {|line| ... } ⇒ Object

Yields:

  • (line)


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/httpx/plugins/stream.rb', line 35

def each_line
  return enum_for(__method__) unless block_given?

  line = "".b

  each do |chunk|
    line << chunk

    while (idx = line.index("\n"))
      yield line.byteslice(0..idx - 1)

      line = line.byteslice(idx + 1..-1)
    end
  end

  yield line unless line.empty?
end

#inspectObject

:nocov:



61
62
63
# File 'lib/httpx/plugins/stream.rb', line 61

def inspect
  "#<StreamResponse:#{object_id}>"
end

#on_chunk(chunk) ⇒ Object

This is a ghost method. It’s to be used ONLY internally, when processing streams

Raises:

  • (NoMethodError)


54
55
56
57
58
# File 'lib/httpx/plugins/stream.rb', line 54

def on_chunk(chunk)
  raise NoMethodError unless @on_chunk

  @on_chunk.call(chunk)
end

#to_sObject

:nocov:



66
67
68
# File 'lib/httpx/plugins/stream.rb', line 66

def to_s
  response.to_s
end