lib/submodules/ably-ruby/lib/ably/rest/channel.rb in ably-rest-0.8.2 vs lib/submodules/ably-ruby/lib/ably/rest/channel.rb in ably-rest-0.8.3

- old
+ new

@@ -23,34 +23,55 @@ # @option channel_options [Hash] :cipher_params A hash of options to configure the encryption. *:key* is required, all other options are optional. See {Ably::Util::Crypto#initialize} for a list of +cipher_params+ options # def initialize(client, name, channel_options = {}) ensure_utf_8 :name, name + update_options channel_options @client = client @name = name - @options = channel_options.clone.freeze end - # Publish a message to the channel + # Publish one or more messages to the channel. # - # @param name [String] The event name of the message to publish - # @param data [String] The message payload - # @return [Boolean] true if the message was published, otherwise false - def publish(name, data) - ensure_utf_8 :name, name - ensure_supported_payload data + # @param name [String, Array<Ably::Models::Message|Hash>, nil] The event name of the message to publish, or an Array of [Ably::Model::Message] objects or [Hash] objects with +:name+ and +:data+ pairs + # @param data [String, ByteArray, nil] The message payload unless an Array of [Ably::Model::Message] objects passed in the first argument + # @return [Boolean] true if the message was published, otherwise false + # + # @example + # # Publish a single message + # channel.publish 'click', { x: 1, y: 2 } + # + # # Publish an array of message Hashes + # messages = [ + # { name: 'click', { x: 1, y: 2 } }, + # { name: 'click', { x: 2, y: 3 } } + # ] + # channel.publish messages + # + # # Publish an array of Ably::Models::Message objects + # messages = [ + # Ably::Models::Message(name: 'click', { x: 1, y: 2 }) + # Ably::Models::Message(name: 'click', { x: 2, y: 3 }) + # ] + # channel.publish messages + # + def publish(name, data = nil) + messages = if name.kind_of?(Enumerable) + name + else + ensure_utf_8 :name, name, allow_nil: true + ensure_supported_payload data + [{ name: name, data: data }] + end - payload = { - name: name, - data: data - } - - message = Ably::Models::Message.new(payload).tap do |message| - message.encode self + payload = messages.map do |message| + Ably::Models::Message(message.dup).tap do |message| + message.encode self + end.as_json end - response = client.post("#{base_path}/publish", message) + response = client.post("#{base_path}/publish", payload.length == 1 ? payload.first : payload) [201, 204].include?(response.status) end # Return the message of the channel @@ -69,10 +90,11 @@ :direction => :backwards, :limit => 100 }.merge(options) [:start, :end].each { |option| options[option] = as_since_epoch(options[option]) if options.has_key?(option) } + raise ArgumentError, ":end must be equal to or after :start" if options[:start] && options[:end] && (options[:start] > options[:end]) paginated_options = { coerce_into: 'Ably::Models::Message', async_blocking_operations: options.delete(:async_blocking_operations), } @@ -89,9 +111,14 @@ # Return the {Ably::Rest::Presence} object # # @return [Ably::Rest::Presence] def presence @presence ||= Presence.new(client, self) + end + + # @api private + def update_options(channel_options) + @options = channel_options.clone.freeze end private def base_path "/channels/#{CGI.escape(name)}"