class DockerCloud::StreamAPI

Constants

STREAM_API_PATH

Public Instance Methods

on(type, &block) click to toggle source
# File lib/docker_cloud/api/stream_api.rb, line 12
def on(type, &block)
  @listeners = {} unless @listeners
  @listeners[type.to_sym] = block
end
run!(&block) click to toggle source
# File lib/docker_cloud/api/stream_api.rb, line 17
def run!(&block)
  EventMachine.kqueue = true if EventMachine.kqueue?

  EM.run {
    @shutting_down = false
    Signal.trap('INT')  { signal_handler('INT') }
    Signal.trap('TERM') { signal_handler('TERM') }

    if block_given?
      block.call
    end

    connect
  }
end
websocket() click to toggle source
# File lib/docker_cloud/api/stream_api.rb, line 5
def websocket
  @websocket ||= begin
    url = URI.escape(STREAM_API_PATH + '/' + root_path + '/' + API_VERSION + websocket_path)
    Faye::WebSocket::Client.new(url, nil, ping: 240, headers: headers)
  end
end

Private Instance Methods

_on_close(event) click to toggle source
# File lib/docker_cloud/api/stream_api.rb, line 76
def _on_close(event)
  @listeners[:close].call(event) if @listeners[:close]
  unless @shutting_down
    connect
  end
end
_on_error(event) click to toggle source
# File lib/docker_cloud/api/stream_api.rb, line 72
def _on_error(event)
  @listeners[:error].call(event) if @listeners[:error]
end
_on_message(event) click to toggle source
# File lib/docker_cloud/api/stream_api.rb, line 68
def _on_message(event)
  @listeners[:message].call(event) if @listeners[:message]
end
_on_open(event) click to toggle source
# File lib/docker_cloud/api/stream_api.rb, line 64
def _on_open(event)
  @listeners[:open].call(event) if @listeners[:open]
end
connect() click to toggle source
# File lib/docker_cloud/api/stream_api.rb, line 35
def connect
  websocket.on :open do |event|
    _on_open(event)
  end
  websocket.on :message do |event|
    _on_message(event)
  end

  websocket.on :error do |event|
    _on_error(event)
  end

  websocket.on :close do |event|
    _on_close(event)
  end
end
signal_handler(type) click to toggle source
# File lib/docker_cloud/api/stream_api.rb, line 56
def signal_handler(type)
  # In rare cases the signal comes multiple times. If we're already shutting down ignore this.
  unless @shutting_down
    @shutting_down = true
    EventMachine.stop
  end
end
websocket_path() click to toggle source
# File lib/docker_cloud/api/stream_api.rb, line 52
def websocket_path
  nil
end