Class: Feat::NetHTTPStreamTransport::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/feat/streaming.rb

Overview

Wraps a started Net::HTTP connection. #each_chunk blocks while the server holds the stream open; #close aborts it from another thread.

Instance Method Summary collapse

Constructor Details

#initialize(http, uri, headers) ⇒ Connection

Returns a new instance of Connection.



66
67
68
69
70
# File 'lib/feat/streaming.rb', line 66

def initialize(http, uri, headers)
  @http = http
  @request = Net::HTTP::Get.new(uri)
  headers.each { |name, value| @request[name] = value }
end

Instance Method Details

#closeObject



81
82
83
84
85
# File 'lib/feat/streaming.rb', line 81

def close
  @http.finish if @http.started?
rescue StandardError
  nil
end

#each_chunkObject



72
73
74
75
76
77
78
79
# File 'lib/feat/streaming.rb', line 72

def each_chunk
  @http.request(@request) do |response|
    code = response.code.to_i
    raise StreamError.new("datafile stream failed: HTTP #{code}", code: code) unless code == 200

    response.read_body { |chunk| yield chunk }
  end
end