aboutsummaryrefslogtreecommitdiffstats
path: root/lib/plum/client/response.rb
blob: e937fa6cb7838338c3346c1736984a024500acf0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# frozen-string-literal: true

module Plum
  class Response
    # The response headers
    # @return [Hash<String, String>]
    attr_reader :headers

    # @api private
    def initialize(session, auto_decode: true, **options, &on_headers)
      @session = session
      @headers = nil
      @finished = false
      @failed = false
      @body = []
      @auto_decode = auto_decode
      @on_headers = on_headers
      @on_chunk = @on_finish = nil
    end

    # Returns the HTTP status code.
    # @return [String] the HTTP status code
    def status
      @headers&.fetch(":status")
    end

    # Returns the header value that correspond to the header name.
    # @param key [String] the header name
    # @return [String] the header value
    def [](key)
      @headers[key.to_s.downcase]
    end

    # Returns whether the response is complete or not.
    # @return [Boolean]
    def finished?
      @finished
    end

    # Returns whether the request has failed or not.
    # @return [Boolean]
    def failed?
      @failed
    end

    # Set callback that will be called when the response headers arrive
    # @yield [self]
    def on_headers(&block)
      raise ArgumentError, "block must be given" unless block_given?
      @on_headers = block
      yield self if @headers
      self
    end

    # Set callback that will be called when received a chunk of response body.
    # @yield [chunk] A chunk of the response body.
    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
      self
    end

    # Set callback that will be called when the response finished.
    def on_finish(&block)
      raise ArgumentError, "block must be given" unless block_given?
      if finished?
        yield
      else
        @on_finish = block
      end
      self
    end

    # Returns the complete response body. Use #each_body instead if the body can be very large.
    # @return [String] the whole response body
    def body
      raise "Body already read" if @on_chunk
      raise "Response body is not complete" unless finished?
      @body.join
    end

    def join
      @session.succ until (@finished || @failed)
      self
    end

    private
    # internal: set headers and setup decoder
    def set_headers(headers)
      @headers = headers.freeze
      @on_headers.call(self) if @on_headers
      @decoder = setup_decoder
    end

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

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

    def fail(ex = nil)
      @failed = ex || true # FIXME
    end

    def setup_decoder
      if @auto_decode
        klass = Decoders::DECODERS[@headers["content-encoding"]]
      end
      klass ||= Decoders::Base
      klass.new
    end
  end
end