Class: Tkellem::PushService
- Inherits:
-
Object
- Object
- Tkellem::PushService
show all
- Includes:
- EasyLogger
- Defined in:
- lib/tkellem/plugins/push_service.rb
Overview
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from EasyLogger
#failsafe, logger, logger=, trace, #trace, trace=
Constructor Details
#initialize(bouncer, add_device_msg) ⇒ PushService
Returns a new instance of PushService.
60
61
62
63
|
# File 'lib/tkellem/plugins/push_service.rb', line 60
def initialize(bouncer, add_device_msg)
@bouncer = bouncer
@device_token, @device_name = add_device_msg.args[1,2]
end
|
Instance Attribute Details
#device_token ⇒ Object
Returns the value of attribute device_token.
12
13
14
|
# File 'lib/tkellem/plugins/push_service.rb', line 12
def device_token
@device_token
end
|
#port ⇒ Object
Returns the value of attribute port.
12
13
14
|
# File 'lib/tkellem/plugins/push_service.rb', line 12
def port
@port
end
|
#server ⇒ Object
Returns the value of attribute server.
12
13
14
|
# File 'lib/tkellem/plugins/push_service.rb', line 12
def server
@server
end
|
Class Method Details
.active_instances ⇒ Object
18
19
20
21
|
# File 'lib/tkellem/plugins/push_service.rb', line 18
def self.active_instances
@instances || @instances = {}
end
|
.client_msg(bouncer, client, msg) ⇒ Object
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
# File 'lib/tkellem/plugins/push_service.rb', line 28
def self.client_msg(bouncer, client, msg)
case msg.command
when 'PUSH'
if service = client.data(self)[:instance]
service.client_message(msg)
elsif msg.args.first != 'add-device'
else
service = PushService.new(bouncer, msg)
active_instances[service.device_token] = service
client.data(self)[:instance] = service
end
false
else
true
end
end
|
.connections ⇒ Object
14
15
16
|
# File 'lib/tkellem/plugins/push_service.rb', line 14
def self.connections
@connections || @connections = {}
end
|
.new_client_connected(bouncer, client) ⇒ Object
25
26
|
# File 'lib/tkellem/plugins/push_service.rb', line 25
def self.new_client_connected(bouncer, client)
end
|
.server_msg(bouncer, msg) ⇒ Object
48
49
50
|
# File 'lib/tkellem/plugins/push_service.rb', line 48
def self.server_msg(bouncer, msg)
active_instances.each { |token, service| service.handle_message(msg) }
end
|
.stop_service(push_service) ⇒ Object
52
53
54
|
# File 'lib/tkellem/plugins/push_service.rb', line 52
def self.stop_service(push_service)
active_instances.delete(push_service.device_token)
end
|
Instance Method Details
#client_message(msg) ⇒ Object
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
# File 'lib/tkellem/plugins/push_service.rb', line 65
def client_message(msg)
raise("only push plz") unless msg.command == 'PUSH'
case msg.args.first
when 'add-device'
when 'service'
@server, @port = msg.args[1,2].map { |a| a.downcase }
ensure_connection
when 'connection'
when 'highlight-word'
when 'highlight-sound'
@highlight_sound = msg.args.last
when 'message-sound'
@message_sound = msg.args.last
when 'end-device'
when 'remove-device'
self.class.stop_service(self)
end
end
|
#ensure_connection ⇒ Object
112
113
114
115
|
# File 'lib/tkellem/plugins/push_service.rb', line 112
def ensure_connection
@connection = self.class.connections[[@server, @port]] ||=
EM.connect(@server, @port, PushServiceConnection, self, @server, @port)
end
|
#handle_message(msg) ⇒ Object
87
88
89
90
91
92
93
|
# File 'lib/tkellem/plugins/push_service.rb', line 87
def handle_message(msg)
return unless @connection
case msg.command
when /privmsg/i
send_message(msg) if msg.args.last =~ /#{@bouncer.nick}/
end
end
|
#log_name ⇒ Object
56
57
58
|
# File 'lib/tkellem/plugins/push_service.rb', line 56
def log_name
"#{@bouncer.log_name}:#{@device_token}"
end
|
#lost_connection ⇒ Object
117
118
119
120
121
|
# File 'lib/tkellem/plugins/push_service.rb', line 117
def lost_connection
self.class.connections.delete([@server, @port])
@connection = nil
ensure_connection
end
|
#send_message(msg) ⇒ Object
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
# File 'lib/tkellem/plugins/push_service.rb', line 95
def send_message(msg)
trace "forwarding #{msg} for #{@device_token}"
sender = msg.prefix.split('!', 2).first
room = msg.args.first
args = {
'device-token' => @device_token,
'message' => msg.args.last.to_s,
'sender' => sender,
'room' => msg.args.first,
'server' => 'blah',
'badge' => 1,
}
args['sound'] = @message_sound if @message_sound
@connection.send_data(args.to_json) if @connection
end
|