Class: Kubeclient::Common::WatchStream

Inherits:
Object
  • Object
show all
Defined in:
lib/kubeclient/watch_stream.rb

Overview

HTTP Stream used to watch changes on entities

Instance Method Summary collapse

Constructor Details

#initialize(uri, http_options, formatter:) ⇒ WatchStream

Returns a new instance of WatchStream.



7
8
9
10
11
12
13
# File 'lib/kubeclient/watch_stream.rb', line 7

def initialize(uri, http_options, formatter:)
  @uri = uri
  @http_client = nil
  @http_options = http_options
  @http_options[:http_max_redirects] ||= Kubeclient::Client::DEFAULT_HTTP_MAX_REDIRECTS
  @formatter = formatter
end

Instance Method Details

#eachObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/kubeclient/watch_stream.rb', line 15

def each
  @finished = false

  @http_client = build_client
  response = @http_client.request(:get, @uri, build_client_options)
  unless response.code < 300
    raise Kubeclient::HttpError.new(response.code, response.reason, response)
  end

  buffer = ''
  response.body.each do |chunk|
    buffer << chunk
    while (line = buffer.slice!(/.+\n/))
      yield @formatter.call(line.chomp)
    end
  end
rescue StandardError
  raise unless @finished
end

#finishObject



35
36
37
38
# File 'lib/kubeclient/watch_stream.rb', line 35

def finish
  @finished = true
  @http_client.close unless @http_client.nil?
end