lib/pubnub/events/channel_registration.rb in pubnub-3.8.5 vs lib/pubnub/events/channel_registration.rb in pubnub-4.0.0beta1
- old
+ new
@@ -14,11 +14,11 @@
private
def parameters
parameters = super
if @action == :add && !@channel.blank?
- parameters.merge!(add: @channel.join(','))
+ parameters.merge!(add: @channel.join(','))
end
if @action == :remove && !@channel.blank?
parameters.merge!(remove: @channel.join(','))
end
@@ -28,16 +28,22 @@
def path
head = "/v1/channel-registration/sub-key/#{@subscribe_key}/"
body = case @action
- when :list_groups then body_list_groups
- when :list_namespaces then body_list_namespaces
- when :get then body_get
- when :add then body_add
- when :remove then body_remove
- else raise_action_key_error
+ when :list_groups then
+ body_list_groups
+ when :list_namespaces then
+ body_list_namespaces
+ when :get then
+ body_get
+ when :add then
+ body_add
+ when :remove then
+ body_remove
+ else
+ raise_action_key_error
end
head + body
end
@@ -86,38 +92,130 @@
@group_id,
('remove' if @channel.blank?)
].delete_if(&:blank?).join('/')
end
- def format_envelopes(response)
+ def format_envelopes(response, request)
parsed_response, error = Formatter.parse_json(response.body)
error = response if parsed_response && response.code.to_i != 200
- envelopes = if error
- [error_envelope(parsed_response, error)]
- else
- [valid_envelope(parsed_response)]
- end
-
- add_common_data_to_envelopes(envelopes, response)
+ if error
+ error_envelope(parsed_response, error, request: request, response: response)
+ else
+ valid_envelope(parsed_response, request: request, response: response)
+ end
end
- def valid_envelope(parsed_response)
- Envelope.new(
- parsed_response: parsed_response,
- payload: parsed_response['payload'],
- service: parsed_response['service'],
- message: parsed_response['message'],
- status: parsed_response['status'],
- error: parsed_response['error']
+ def valid_envelope(parsed_response, req_res_objects)
+ # {"status"=>200, "message"=>"OK", "service"=>"channel-registry", "error"=>false}
+ Pubnub::Envelope.new(
+ event: @event,
+ event_options: @given_options,
+ timetoken: nil,
+ status: {
+ code: req_res_objects[:response].code,
+ operation: define_operation,
+ client_request: req_res_objects[:request],
+ server_response: req_res_objects[:response],
+ data: nil,
+ category: Pubnub::Constants::STATUS_ACK,
+ error: false,
+ auto_retried: false,
+
+ current_timetoken: nil,
+ last_timetoken: nil,
+ subscribed_channels: nil,
+ subscribed_channel_groups: nil,
+
+ config: get_config
+ },
+
+ result: {
+ data: parsed_response['payload'],
+ code: req_res_objects[:response].code,
+ operation: define_operation,
+ client_request: req_res_objects[:request],
+ server_response: req_res_objects[:response],
+ }
)
end
- def error_envelope(parsed_response, error)
- ErrorEnvelope.new(
- error: error,
- response_message: response_message(parsed_response)
- )
+ def error_envelope(parsed_response, error, req_res_objects)
+ if error
+ Pubnub::ErrorEnvelope.new(
+ event: @event,
+ event_options: @given_options,
+ timetoken: nil,
+ status: {
+ code: req_res_objects[:response].code,
+ operation: define_operation,
+ client_request: req_res_objects[:request],
+ server_response: req_res_objects[:response],
+ data: nil,
+ category: :json_parse_error,
+ error: true,
+ auto_retried: false,
+
+ current_timetoken: nil,
+ last_timetoken: nil,
+ subscribed_channels: nil,
+ subscribed_channel_groups: nil,
+
+ config: get_config
+ },
+
+ result: {
+ data: parsed_response['payload'],
+ code: req_res_objects[:response].code,
+ operation: define_operation,
+ client_request: req_res_objects[:request],
+ server_response: req_res_objects[:response]
+ }
+ )
+ else
+ ErrorEnvelope.new(
+ event: @event,
+ event_options: @given_options,
+ timetoken: nil,
+ status: {
+ code: req_res_objects[:response].code,
+ client_request: req_res_objects[:request],
+ server_response: req_res_objects[:response],
+ category: nil,
+ error: true,
+ auto_retried: false,
+
+ current_timetoken: nil,
+ last_timetoken: nil,
+ subscribed_channels: nil,
+ subscribed_channel_groups: nil,
+
+ config: get_config
+
+ },
+
+ result: {
+ data: parsed_response['payload'],
+ code: req_res_objects[:response].code,
+ operation: define_operation,
+ client_request: req_res_objects[:request],
+ server_response: req_res_objects[:response]
+ }
+ )
+ end
+ end
+
+ def define_operation
+ case @action
+ when :add
+ Pubnub::Constants::OPERATION_CHANNEL_GROUP_ADD
+ when :remove
+ Pubnub::Constants::OPERATION_CHANNEL_GROUP_REMOVE
+ when :get
+ Pubnub::Constants::OPERATION_LIST_ALL_CHANNELS_IN_CHANNEL_GROUP
+ when :list_groups
+ Pubnub::Constants::OPERATION_LIST_ALL_CHANNEL_GROUPS
+ end
end
end
end