lib/ably/realtime/presence.rb in ably-0.8.1 vs lib/ably/realtime/presence.rb in ably-0.8.2

- old
+ new

@@ -1,8 +1,9 @@ module Ably::Realtime # Presence provides access to presence operations and state for the associated Channel class Presence + include Ably::Modules::Conversions include Ably::Modules::EventEmitter include Ably::Modules::AsyncWrapper include Ably::Modules::MessageEmitter include Ably::Modules::SafeYield extend Ably::Modules::Enum @@ -67,15 +68,20 @@ # # @yield [Ably::Realtime::Presence] On success, will call the block with this {Ably::Realtime::Presence} object # @return [Ably::Util::SafeDeferrable] Deferrable that supports both success (callback) and failure (errback) callbacks # def enter(options = {}, &success_block) - @client_id = options.fetch(:client_id, client_id) - @data = options.fetch(:data, nil) + client_id = options.fetch(:client_id, self.client_id) + data = options.fetch(:data, nil) deferrable = create_deferrable + ensure_supported_payload data unless data.nil? raise Ably::Exceptions::Standard.new('Unable to enter presence channel without a client_id', 400, 91000) unless client_id + + @data = data + @client_id = client_id + return deferrable_succeed(deferrable, &success_block) if state == STATE.Entered ensure_channel_attached(deferrable) do if entering? once_or_if(STATE.Entered, else: proc { |args| deferrable_fail deferrable, *args }) do @@ -111,10 +117,11 @@ # @yield [Ably::Realtime::Presence] On success, will call the block with this {Ably::Realtime::Presence} object # @return [Ably::Util::SafeDeferrable] Deferrable that supports both success (callback) and failure (errback) callbacks # def enter_client(client_id, options = {}, &success_block) raise ArgumentError, 'options must be a Hash' unless options.kind_of?(Hash) + ensure_supported_payload options[:data] if options.has_key?(:data) raise Ably::Exceptions::Standard.new('Unable to enter presence channel without a client_id', 400, 91000) unless client_id send_presence_action_for_client(Ably::Models::PresenceMessage::ACTION.Enter, client_id, options, &success_block) end @@ -126,14 +133,18 @@ # # @yield (see Presence#enter) # @return (see Presence#enter) # def leave(options = {}, &success_block) - @data = options.fetch(:data, data) # nil value defaults leave data to existing value + data = options.fetch(:data, self.data) # nil value defaults leave data to existing value deferrable = create_deferrable + ensure_supported_payload data unless data.nil? raise Ably::Exceptions::Standard.new('Unable to leave presence channel that is not entered', 400, 91002) unless able_to_leave? + + @data = data + return deferrable_succeed(deferrable, &success_block) if state == STATE.Left ensure_channel_attached(deferrable) do if leaving? once_or_if(STATE.Left, else: proc { |error|deferrable_fail deferrable, *args }) do @@ -163,10 +174,11 @@ # @yield (see Presence#enter_client) # @return (see Presence#enter_client) # def leave_client(client_id, options = {}, &success_block) raise ArgumentError, 'options must be a Hash' unless options.kind_of?(Hash) + ensure_supported_payload options[:data] if options.has_key?(:data) raise Ably::Exceptions::Standard.new('Unable to leave presence channel without a client_id', 400, 91000) unless client_id send_presence_action_for_client(Ably::Models::PresenceMessage::ACTION.Leave, client_id, options, &success_block) end @@ -179,13 +191,18 @@ # # @yield (see Presence#enter) # @return (see Presence#enter) # def update(options = {}, &success_block) - @data = options.fetch(:data, nil) + data = options.fetch(:data, nil) deferrable = create_deferrable + ensure_supported_payload data unless data.nil? + raise Ably::Exceptions::Standard.new('Unable to update presence channel without a client_id', 400, 91000) unless client_id + + @data = data + ensure_channel_attached(deferrable) do send_protocol_message_and_transition_state_to( Ably::Models::PresenceMessage::ACTION.Update, deferrable: deferrable, target_state: STATE.Entered, @@ -208,10 +225,11 @@ # @yield (see Presence#enter_client) # @return (see Presence#enter_client) # def update_client(client_id, options = {}, &success_block) raise ArgumentError, 'options must be a Hash' unless options.kind_of?(Hash) + ensure_supported_payload options[:data] if options.has_key?(:data) raise Ably::Exceptions::Standard.new('Unable to enter presence channel without a client_id', 400, 91000) unless client_id send_presence_action_for_client(Ably::Models::PresenceMessage::ACTION.Update, client_id, options, &success_block) end @@ -272,10 +290,10 @@ # # @param (see Ably::Rest::Presence#history) # @option options (see Ably::Rest::Presence#history) # @option options [Boolean] :until_attach When true, request for history will be limited only to messages published before the associated channel was attached. The associated channel must be attached. # - # @yield [Ably::Models::PaginatedResource<Ably::Models::PresenceMessage>] First {Ably::Models::PaginatedResource page} of {Ably::Models::PresenceMessage} objects accessible with {Ably::Models::PaginatedResource#items #items}. + # @yield [Ably::Models::PaginatedResult<Ably::Models::PresenceMessage>] First {Ably::Models::PaginatedResult page} of {Ably::Models::PresenceMessage} objects accessible with {Ably::Models::PaginatedResult#items #items}. # # @return [Ably::Util::SafeDeferrable] # def history(options = {}, &callback) if options.delete(:until_attach)