Class: Subscriber::Client::ParserBundle

Inherits:
Object
  • Object
show all
Defined in:
lib/nchan_tools/pubsub.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri, opt = {}) ⇒ ParserBundle

Returns a new instance of ParserBundle.



308
309
310
311
312
313
# File 'lib/nchan_tools/pubsub.rb', line 308

def initialize(uri, opt={})
  @uri=uri
  @id=(opt[:id] or :"~").to_s.to_sym
  @logger = opt[:logger]
  open_socket
end

Instance Attribute Details

#body_bufObject

Returns the value of attribute body_buf.



307
308
309
# File 'lib/nchan_tools/pubsub.rb', line 307

def body_buf
  @body_buf
end

#codeObject

Returns the value of attribute code.



307
308
309
# File 'lib/nchan_tools/pubsub.rb', line 307

def code
  @code
end

#connectedObject

Returns the value of attribute connected.



307
308
309
# File 'lib/nchan_tools/pubsub.rb', line 307

def connected
  @connected
end

#etagObject

Returns the value of attribute etag.



307
308
309
# File 'lib/nchan_tools/pubsub.rb', line 307

def etag
  @etag
end

#headersObject

Returns the value of attribute headers.



307
308
309
# File 'lib/nchan_tools/pubsub.rb', line 307

def headers
  @headers
end

#idObject

Returns the value of attribute id.



307
308
309
# File 'lib/nchan_tools/pubsub.rb', line 307

def id
  @id
end

#last_modifiedObject

Returns the value of attribute last_modified.



307
308
309
# File 'lib/nchan_tools/pubsub.rb', line 307

def last_modified
  @last_modified
end

#parserObject

Returns the value of attribute parser.



307
308
309
# File 'lib/nchan_tools/pubsub.rb', line 307

def parser
  @parser
end

#sockObject

Returns the value of attribute sock.



307
308
309
# File 'lib/nchan_tools/pubsub.rb', line 307

def sock
  @sock
end

#subparserObject

Returns the value of attribute subparser.



307
308
309
# File 'lib/nchan_tools/pubsub.rb', line 307

def subparser
  @subparser
end

#uriObject

Returns the value of attribute uri.



307
308
309
# File 'lib/nchan_tools/pubsub.rb', line 307

def uri
  @uri
end

#verboseObject

Returns the value of attribute verbose.



307
308
309
# File 'lib/nchan_tools/pubsub.rb', line 307

def verbose
  @verbose
end

Instance Method Details

#buffer_body!Object



328
329
330
# File 'lib/nchan_tools/pubsub.rb', line 328

def buffer_body!
  @body_buf||=""
end

#connected?Boolean

Returns:

  • (Boolean)


331
332
333
# File 'lib/nchan_tools/pubsub.rb', line 331

def connected?
  @connected
end

#on_chunk(ch = nil, &block) ⇒ Object



344
345
346
347
348
349
350
351
352
# File 'lib/nchan_tools/pubsub.rb', line 344

def on_chunk(ch=nil, &block)
  if block_given?
    @on_chunk = block
  else
    @body_buf << ch if @body_buf
    @logger.log @id, :chunk, ch if @logger
    @on_chunk.call(ch) if @on_chunk
  end
end

#on_error(msg = nil, e = nil, &block) ⇒ Object



364
365
366
367
368
369
370
371
# File 'lib/nchan_tools/pubsub.rb', line 364

def on_error(msg=nil, e=nil, &block)
  if block_given?
    @on_error = block
  else
    @logger.log @id, :error, "#{e.to_s}, #{msg}" if @logger
    @on_error.call(msg, e) if @on_error
  end
end

#on_headers(code = nil, h = nil, &block) ⇒ Object



334
335
336
337
338
339
340
341
342
# File 'lib/nchan_tools/pubsub.rb', line 334

def on_headers(code=nil, h=nil, &block)
  @body_buf.clear if @body_buf
  if block_given?
    @on_headers = block
  else
    @logger.log @id, :headers, "#{code or "no code"}; headers: #{h or "none"}" if @logger
    @on_headers.call(code, h) if @on_headers
  end
end

#on_response(code = nil, headers = nil, &block) ⇒ Object



354
355
356
357
358
359
360
361
362
# File 'lib/nchan_tools/pubsub.rb', line 354

def on_response(code=nil, headers=nil, &block)
  if block_given?
    @on_response = block
  else
    @logger.log @id, :response, "code #{code or "-"}, headers: #{headers or "-"}, body: #{@body_buf}" if @logger
    @on_response.call(code, headers, @body_buf) if @on_response
  end
  
end

#open_socketObject



314
315
316
317
318
319
320
321
322
323
324
325
326
# File 'lib/nchan_tools/pubsub.rb', line 314

def open_socket
  case uri.scheme
  when /^unix$/
    @sock = Celluloid::IO::UNIXSocket.new(uri.host)
  when /^(ws|http|h2c)$/
    @sock = Celluloid::IO::TCPSocket.new(uri.host, uri.port)
  when /^(wss|https|h2)$/
    @sock = Celluloid::IO::SSLSocket.new(Celluloid::IO::TCPSocket.new(uri.host, uri.port))
  else
    raise ArgumentError, "unexpected uri scheme #{uri.scheme}"
  end
  self
end