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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(session) ⇒ Listener

Returns a new instance of Listener.


92
93
94
95
96
97
# File 'lib/travis/client/listener.rb', line 92

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


90
91
92
# File 'lib/travis/client/listener.rb', line 90

def session
  @session
end

#socketObject (readonly)

Returns the value of attribute socket


90
91
92
# File 'lib/travis/client/listener.rb', line 90

def socket
  @socket
end

Instance Method Details

#disconnectObject


126
127
128
# File 'lib/travis/client/listener.rb', line 126

def disconnect
  socket.disconnect
end

#listenObject


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

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


109
110
111
112
# File 'lib/travis/client/listener.rb', line 109

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_connectObject


114
115
116
# File 'lib/travis/client/listener.rb', line 114

def on_connect
  socket.bind('pusher:connection_established') { yield }
end

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

Yields:


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

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