lib/pubnub/events/channel_registration.rb in pubnub-3.7.12 vs lib/pubnub/events/channel_registration.rb in pubnub-3.8.0
- old
+ new
@@ -1,132 +1,123 @@
+# Toplevel Pubnub module.
module Pubnub
- class ChannelRegistration
- include Pubnub::Event
- include Pubnub::SingleEvent
- include Pubnub::Formatter
- include Pubnub::Validator
+ # Holds channel_registration functionality
+ class ChannelRegistration < SingleEvent
+ include Celluloid
+ include Pubnub::Validator::ChannelRegistration
def initialize(options, app)
- @group = options[:group]
- @action = options[:action]
- @cloak = options[:cloak]
+ @event = :channel_registration
super
-
- @channel = nil if @channel.size == 1 && @channel.first.empty?
- format_group if @group
-
- @event = 'channel_registration'
+ @action = @action.to_sym unless @action.nil?
end
private
- def validate!
- # Callback
- raise ArgumentError.new(:object => self, :message => 'Callback parameter is required while using async channel_registration') if !@http_sync && @callback.blank?
-
- # Channel group
- if @channel_group.class == Array
- @channel_group.each do |cg|
- # raise ArgumentError.new(:object => self, :message => ':group argument has to be in format "ns:cg", "ns:" or ":cg"') if cg.count(':') != 1
- end
- elsif !@channel_group.blank?
- number_of_groups = @channel_group.to_s.split(',').size # In case it will be given as csv
- # raise ArgumentError.new(:object => self, :message => ':group argument has to be in format "ns:cg", "ns:" or ":cg"') if @channel_group.to_s.count(':') != number_of_groups
+ def parameters
+ parameters = super
+ if @action == :add && !@channel.blank?
+ parameters.merge!(add: @channel.join(','))
end
- end
- def format_group
- if @group.to_s.count(':') > 0
- @namespace_id, @group_id = @group.to_s.split(':')
- else
- @namespace_id = nil
- @group_id = @group.to_s
+ if @action == :remove && !@channel.blank?
+ parameters.merge!(remove: @channel.join(','))
end
- end
- def parameters(app)
- parameters = super(app)
- parameters.merge!({cloak: @cloak}) if @cloak
- parameters.merge!({add: @channel.join(',')}) if @action == :add && !@channel.blank?
- parameters.merge!({remove: @channel.join(',')}) if @action == :remove && !@channel.blank?
parameters
end
- def path(app)
+ def path
head = "/v1/channel-registration/sub-key/#{@subscribe_key}/"
- body = ''
- case @action
- when :list_groups
- body << [
- 'channel-group'
- ].join('/')
- when :list_namespaces
- body << [
- 'namespace'
- ].join('/')
-
- when :get
- body << [
- ('namespace' unless @namespace_id.blank?),
- @namespace_id,
- 'channel-group',
- @group_id
- ].delete_if { |e| e.blank? }.join('/')
- when :add
- body << [
- ('namespace' unless @namespace_id.blank?),
- @namespace_id,
- 'channel-group',
- @group_id
- ].delete_if { |e| e.blank? }.join('/')
+ 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
+ end
- when :remove
- body << [
- ('namespace' unless @namespace_id.blank?),
- @namespace_id,
- ('channel-group' unless @group_id.blank?),
- @group_id,
- ('remove' if @channel.blank?)
- ].delete_if { |e| e.blank? }.join('/')
+ head + body
+ end
- when :set_cloak
- body << [
- ('namespace' unless @namespace_id.blank?),
- @namespace_id,
- 'channel-group',
- @group_id
- ].delete_if { |e| e.blank? }.join('/')
+ def raise_action_key_error
+ fail ArgumentError.new(
+ object: self,
+ message: 'ChannelRegistration requires proper :action key'
+ ), 'ChannelRegistration requires proper :action key'
+ end
- else
- fail ArgumentError.new(:object => self, :message => 'ChannelRegistration requires proper :action key'), 'ChannelRegistration requires proper :action key'
- end
+ def body_list_groups
+ [
+ 'channel-group'
+ ].join('/')
+ end
- head + body
+ def body_list_namespaces
+ [
+ 'namespace'
+ ].join('/')
end
+ def body_get
+ [
+ ('namespace' unless @namespace_id.blank?),
+ @namespace_id,
+ 'channel-group',
+ @group_id
+ ].delete_if(&:blank?).join('/')
+ end
- def format_envelopes(response, app, error)
+ def body_add
+ [
+ ('namespace' unless @namespace_id.blank?),
+ @namespace_id,
+ 'channel-group',
+ @group_id
+ ].delete_if(&:blank?).join('/')
+ end
- parsed_response = Parser.parse_json(response.body) if Parser.valid_json?(response.body)
+ def body_remove
+ [
+ ('namespace' unless @namespace_id.blank?),
+ @namespace_id,
+ ('channel-group' unless @group_id.blank?),
+ @group_id,
+ ('remove' if @channel.blank?)
+ ].delete_if(&:blank?).join('/')
+ end
- envelopes = Array.new
- envelopes << Envelope.new({
- :parsed_response => parsed_response,
- :payload => (parsed_response['payload'] if parsed_response),
- :service => (parsed_response['service'] if parsed_response),
- :message => (parsed_response['message'] if parsed_response),
- :status => (parsed_response['status'] if parsed_response),
- :error => (parsed_response['error'] if parsed_response)
- }, app)
+ def format_envelopes(response)
+ parsed_response, error = Formatter.parse_json(response.body)
- envelopes = add_common_data_to_envelopes(envelopes, response, app, error)
+ error = response if parsed_response && response.code.to_i != 200
- envelopes
+ envelopes = if error
+ [error_envelope(parsed_response, error)]
+ else
+ [valid_envelope(parsed_response)]
+ end
+ add_common_data_to_envelopes(envelopes, response)
end
- def encode_state(state)
- URI.encode_www_form_component(state.to_json).gsub('+', '%20')
+ 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']
+ )
+ end
+
+ def error_envelope(parsed_response, error)
+ ErrorEnvelope.new(
+ error: error,
+ response_message: response_message(parsed_response)
+ )
end
end
end