Class: Eagle::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_param = nil) ⇒ Client

Returns a new instance of Client.



6
7
8
9
10
11
# File 'lib/eagle/client.rb', line 6

def initialize(config_param=nil)
  @config = config_param ||Config.config
  pusher_configurator = PusherConfigurator.new(@config)
  pusher_configurator.init_pusher # static
  @pusher_socket = pusher_configurator.init_pusher_client
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



3
4
5
# File 'lib/eagle/client.rb', line 3

def config
  @config
end

#pusher_socketObject

Returns the value of attribute pusher_socket.



4
5
6
# File 'lib/eagle/client.rb', line 4

def pusher_socket
  @pusher_socket
end

Instance Method Details

#pongObject



51
52
53
# File 'lib/eagle/client.rb', line 51

def pong
  Pusher['pong'].trigger!('pong', :host => @config.hostname)
end

#publish_result(event, data) ⇒ Object



55
56
57
# File 'lib/eagle/client.rb', line 55

def publish_result(event, data)
  Pusher['channel2'].trigger!(event, data)
end

#runObject



13
14
15
16
17
18
19
# File 'lib/eagle/client.rb', line 13

def run
  subscribe_to_channels
  subscribe_ping
  loop do
    sleep(1) # Keep your main thread running
  end
end

#subscribe_pingObject



44
45
46
47
48
49
# File 'lib/eagle/client.rb', line 44

def subscribe_ping
  @pusher_socket.subscribe('ping')
  @pusher_socket['ping'].bind('ping') do
    self.pong
  end
end

#subscribe_to_channelsObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/eagle/client.rb', line 22

def subscribe_to_channels
  @pusher_socket.subscribe('channel1')
  @pusher_socket.subscribe('channel2')

  # Bind to a global event (can occur on either channel1 or channel2)
  @pusher_socket.bind('globalevent') do |data|
    puts data
  end

  # Bind to a channel event (can only occur on channel1)
  @pusher_socket['channel1'].bind('channelevent') do |data|
    self.publish_result('hello', :rock => 'on!')
    puts data
  end

  # Bind to a channel event
  @pusher_socket['channel2'].bind('hello') do |data|
    puts "GOT BACK! with #{data}"
  end
end