Class: Travis::Client::Listener

Inherits:
Object
  • Object
show all
Defined in:
lib/travis/client/listener.rb

Defined Under Namespace

Classes: EntityListener, Event, Socket

Constant Summary collapse

EVENTS =
%w[
  build:created build:started build:finished
  job:created job:started job:log job:finished
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(session) ⇒ Listener

Returns a new instance of Listener.



102
103
104
105
106
107
# File 'lib/travis/client/listener.rb', line 102

def initialize(session)
  @session   = session
  @socket    = Socket.new(pusher_key, pusher_options)
  @channels  = []
  @callbacks = []
end

Instance Attribute Details

#sessionObject (readonly)

Returns the value of attribute session.



100
101
102
# File 'lib/travis/client/listener.rb', line 100

def session
  @session
end

#socketObject (readonly)

Returns the value of attribute socket.



100
101
102
# File 'lib/travis/client/listener.rb', line 100

def socket
  @socket
end

Instance Method Details

#disconnectObject



136
137
138
# File 'lib/travis/client/listener.rb', line 136

def disconnect
  socket.disconnect
end

#listenObject



128
129
130
131
132
133
134
# File 'lib/travis/client/listener.rb', line 128

def listen
  @channels = default_channels if @channels.empty?
  @channels.map! { |c| c.start_with?('private-') ? c : "private-#{c}" } if session.private_channels?
  @channels.uniq.each { |c| socket.subscribe(c) }
  @callbacks.each { |e, b| socket.bind(e) { |d| dispatch(e, d, &b) } }
  socket.connect
end

#on(*events, &block) ⇒ Object



119
120
121
122
# File 'lib/travis/client/listener.rb', line 119

def on(*events, &block)
  events = events.flat_map { |e| e.respond_to?(:to_str) ? e.to_str : EVENTS.grep(e) }.uniq
  events.each { |e| @callbacks << [e, block] }
end

#on_connect(&block) ⇒ Object



124
125
126
# File 'lib/travis/client/listener.rb', line 124

def on_connect(&block)
  socket.bind('pusher:connection_established', &block)
end

#subscribe(*entities) {|entities.any? ? EntityListener.new(self, entities) : self| ... } ⇒ Object

Yields:



109
110
111
112
113
114
115
116
117
# File 'lib/travis/client/listener.rb', line 109

def subscribe(*entities)
  entities = entities.map do |entity|
    entity = entity.pusher_entity while entity.respond_to? :pusher_entity
    @channels.concat(entity.pusher_channels)
    entity
  end

  yield entities.any? ? EntityListener.new(self, entities) : self if block_given?
end