Class: Webmate::SocketIO::Packets::Base
- Inherits:
-
Object
- Object
- Webmate::SocketIO::Packets::Base
show all
- Defined in:
- lib/webmate/socket.io/packets/base.rb
Constant Summary
collapse
- PACKETS_TYPES =
the same order, as in socket io packet.packets
%W{
disconnect
connect
heartbeat
message
json
event
ack
error
noop
}
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(packet_data = {}) ⇒ Base
22
23
24
|
# File 'lib/webmate/socket.io/packets/base.rb', line 22
def initialize(packet_data = {})
@packet_data = packet_data.with_indifferent_access
end
|
Instance Attribute Details
#packet_endpoint ⇒ Object
104
105
106
|
# File 'lib/webmate/socket.io/packets/base.rb', line 104
def packet_endpoint
@packet_endpoint ||= ''
end
|
#packet_id ⇒ Object
136
137
138
|
# File 'lib/webmate/socket.io/packets/base.rb', line 136
def packet_id
@id ||= generate_packet_id
end
|
Class Method Details
.build_response_packet(response) ⇒ Object
convert response from Responders::Base to socket io message
63
64
65
|
# File 'lib/webmate/socket.io/packets/base.rb', line 63
def self.build_response_packet(response)
new(self.prepare_packet_data(response))
end
|
.parse(packet) ⇒ Object
packet should be created by socket.io spec
- message type
-
‘:’ [message id (‘+’)] ‘:’ [message endpoint] (‘:’ [message data])
and webmate spec message_data =
method: GET/POST/...
path: '/projects'
params: {
metadata: { data should be returned back with answer }
}
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
# File 'lib/webmate/socket.io/packets/base.rb', line 35
def self.parse(packet)
packet_type_id, packet_id, packet_endpoint, json_data = packet.split(':', 4)
packet_data = (Yajl::Parser.parse(json_data) || {}).with_indifferent_access
if packet_data[:params].is_a?(String)
packet_data[:params] = Yajl::Parser.parse(packet_data[:params])
end
if packet_data[:metadata].is_a?(String)
packet_data[:metadata] = Yajl::Parser.parse(packet_data[:metadata])
end
packet = OpenStruct.new(
path: packet_data[:path],
method: packet_data[:method],
params: packet_data[:params] || {},
metadata: packet_data[:metadata] || {},
packet_id: packet_id,
packet_endpoint: packet_endpoint
)
packet
end
|
.prepare_packet_data(response) ⇒ Object
67
68
69
70
71
72
73
74
75
|
# File 'lib/webmate/socket.io/packets/base.rb', line 67
def self.prepare_packet_data(response)
packet_data = {
action: response.action,
body: response.data,
path: response.path,
params: response.params,
metadata: response.metadata
}
end
|
Instance Method Details
#action ⇒ Object
120
121
122
|
# File 'lib/webmate/socket.io/packets/base.rb', line 120
def action
packet_data[:action]
end
|
#body ⇒ Object
128
129
130
|
# File 'lib/webmate/socket.io/packets/base.rb', line 128
def body
packet_data[:body]
end
|
#generate_packet_id ⇒ Object
unique packet id didn’t find any influence for now, uniqueness not matter
149
150
151
152
|
# File 'lib/webmate/socket.io/packets/base.rb', line 149
def generate_packet_id
self.class.current_id ||= 0
self.class.current_id += 1
end
|
112
113
114
|
# File 'lib/webmate/socket.io/packets/base.rb', line 112
def metadata
packet_data[:metadata]
end
|
#packet_data ⇒ Object
108
109
110
|
# File 'lib/webmate/socket.io/packets/base.rb', line 108
def packet_data
(@packet_data || {})
end
|
#packet_type_id ⇒ Object
100
101
102
|
# File 'lib/webmate/socket.io/packets/base.rb', line 100
def packet_type_id
PACKETS_TYPES.index(self.packet_type)
end
|
#params ⇒ Object
124
125
126
|
# File 'lib/webmate/socket.io/packets/base.rb', line 124
def params
packet_data[:params]
end
|
#path ⇒ Object
116
117
118
|
# File 'lib/webmate/socket.io/packets/base.rb', line 116
def path
packet_data[:path]
end
|
#status ⇒ Object
132
133
134
|
# File 'lib/webmate/socket.io/packets/base.rb', line 132
def status
packet_data[:status]
end
|
#to_packet ⇒ Object
socket io spec
- message type
-
‘:’ [message id (‘+’)] ‘:’ [message endpoint] (‘:’ [message data])
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
# File 'lib/webmate/socket.io/packets/base.rb', line 79
def to_packet
data = {
action: action,
request: {
path: path,
metadata: metadata
},
response: {
body: body,
status: status || 200
}
}
encoded_data = Yajl::Encoder.new.encode(data)
[
packet_type_id,
packet_id,
packet_endpoint,
encoded_data
].join(':')
end
|