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
Returns a new instance of 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
94
95
96
|
# File 'lib/webmate/socket.io/packets/base.rb', line 94
def packet_endpoint
@packet_endpoint ||= ''
end
|
#packet_id ⇒ Object
122
123
124
|
# File 'lib/webmate/socket.io/packets/base.rb', line 122
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
54
55
56
|
# File 'lib/webmate/socket.io/packets/base.rb', line 54
def self.build_response_packet(response)
new(self.prepare_packet_data(response))
end
|
.parse(packet) ⇒ Object
Parse packet created by socket.io spec Packet format
[packet type] ':' [packet id ('+')] ':' [packet endpoint] (':' [packet data])
packet_data = {
method: GET/POST/...
path: '/projects'
params: {
metadata: { data should be returned back with response },
foor: 'bar'
// and any other paranms
}
headers: [ {"SomeHeader": "SomeHeaderValue"} ]
}
40
41
42
43
44
45
46
47
48
49
50
|
# File 'lib/webmate/socket.io/packets/base.rb', line 40
def self.parse(packet)
packet_type_id, packet_id, packet_endpoint, json_data = packet.split(':', 4)
packet_data = (JSON.parse(json_data) rescue {}).with_indifferent_access
Webmate::SocketIO::Request.new(
path: packet_data[:path],
method: packet_data[:method].upcase,
params: packet_data[:params] || {}
)
end
|
.prepare_packet_data(response) ⇒ Object
58
59
60
61
62
63
64
65
66
|
# File 'lib/webmate/socket.io/packets/base.rb', line 58
def self.prepare_packet_data(response)
packet_data = {
body: response.data,
path: response.path,
params: response.params,
metadata: response.metadata,
status: response.status
}
end
|
Instance Method Details
#body ⇒ Object
114
115
116
|
# File 'lib/webmate/socket.io/packets/base.rb', line 114
def body
packet_data[:body]
end
|
#generate_packet_id ⇒ Object
unique packet id didn’t find any influence for now, uniqueness not matter
129
130
131
132
|
# File 'lib/webmate/socket.io/packets/base.rb', line 129
def generate_packet_id
self.class.current_id ||= 0
self.class.current_id += 1
end
|
102
103
104
|
# File 'lib/webmate/socket.io/packets/base.rb', line 102
def metadata
packet_data[:metadata]
end
|
#packet_data ⇒ Object
98
99
100
|
# File 'lib/webmate/socket.io/packets/base.rb', line 98
def packet_data
(@packet_data || {})
end
|
#packet_type_id ⇒ Object
90
91
92
|
# File 'lib/webmate/socket.io/packets/base.rb', line 90
def packet_type_id
PACKETS_TYPES.index(self.packet_type)
end
|
#params ⇒ Object
110
111
112
|
# File 'lib/webmate/socket.io/packets/base.rb', line 110
def params
packet_data[:params]
end
|
#path ⇒ Object
106
107
108
|
# File 'lib/webmate/socket.io/packets/base.rb', line 106
def path
packet_data[:path]
end
|
#status ⇒ Object
118
119
120
|
# File 'lib/webmate/socket.io/packets/base.rb', line 118
def status
packet_data[:status]
end
|
#to_packet ⇒ Object
socket io spec
- message type
-
‘:’ [message id (‘+’)] ‘:’ [message endpoint] (‘:’ [message data])
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
# File 'lib/webmate/socket.io/packets/base.rb', line 70
def to_packet
data = {
request: {
path: path,
metadata: metadata
},
response: {
body: body,
status: status || 200
}
}
encoded_data = JSON.dump(data)
[
packet_type_id,
packet_id,
packet_endpoint,
encoded_data
].join(':')
end
|