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, format: :json) ⇒ WatchStream

Returns a new instance of WatchStream.



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

def initialize(uri, http_options, format: :json)
  @uri = uri
  @http_client = nil
  @http_options = http_options
  @format = format
end

Instance Method Details

#eachObject



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

def each
  @finished = false

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

  buffer = ''
  response.body.each do |chunk|
    buffer << chunk
    while (line = buffer.slice!(/.+\n/))
      yield @format == :json ? WatchNotice.new(JSON.parse(line)) : line.chomp
    end
  end
rescue IOError
  raise unless @finished
end

#finishObject



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

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