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
98
99
# File 'lib/travis/client/listener.rb', line 92

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

  socket.connect(true)
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



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

def disconnect
  socket.disconnect
end

#listenObject



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

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) } }
end

#on(*events, &block) ⇒ Object



111
112
113
114
# File 'lib/travis/client/listener.rb', line 111

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



116
117
118
# File 'lib/travis/client/listener.rb', line 116

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

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

Yields:



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

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