Class: Ably::Rest::Channel
- Inherits:
-
Object
- Object
- Ably::Rest::Channel
- Includes:
- Modules::Conversions
- Defined in:
- lib/ably/rest/channel.rb,
lib/ably/rest/channel/push_channel.rb
Overview
Enables messages to be published and historic messages to be retrieved for a channel.
Defined Under Namespace
Classes: PushChannel
Constant Summary collapse
- IDEMPOTENT_LIBRARY_GENERATED_ID_LENGTH =
See spec RSL1k1
9
Instance Attribute Summary collapse
-
#client ⇒ Ably::Realtime::Client
readonly
private
Ably client associated with this channel.
-
#name ⇒ String
readonly
The channel name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#push ⇒ Ably::Rest::Channel::PushChannel
readonly
private
A PushChannel object.
Instance Method Summary collapse
-
#history(options = {}) ⇒ Ably::Models::PaginatedResult<Ably::Models::Message>
Retrieves a Models::PaginatedResult object, containing an array of historical Models::Message objects for the channel.
-
#initialize(client, name, channel_options = {}) ⇒ Channel
constructor
Initialize a new Channel object.
-
#presence ⇒ Ably::Rest::Presence
A Presence object.
-
#publish(name, data = nil, attributes = {}) ⇒ Boolean
Publishes a message to the channel.
-
#set_options(channel_options) ⇒ Ably::Models::ChannelOptions
(also: #options=)
Sets the Models::ChannelOptions for the channel.
-
#status ⇒ Ably::Models::ChannelDetails
Retrieves a Models::ChannelDetails object for the channel, which includes status and occupancy metrics.
Constructor Details
#initialize(client, name, channel_options = {}) ⇒ Channel
Initialize a new Channel object
33 34 35 36 37 38 39 40 |
# File 'lib/ably/rest/channel.rb', line 33 def initialize(client, name, = {}) name = (ensure_utf_8 :name, name) @options = Ably::Models::ChannelOptions() @client = client @name = name @push = PushChannel.new(self) end |
Instance Attribute Details
#client ⇒ Ably::Realtime::Client (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Ably client associated with this channel
11 12 13 |
# File 'lib/ably/rest/channel.rb', line 11 def client @client end |
#name ⇒ String (readonly)
The channel name.
15 16 17 |
# File 'lib/ably/rest/channel.rb', line 15 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
17 18 19 |
# File 'lib/ably/rest/channel.rb', line 17 def @options end |
#push ⇒ Ably::Rest::Channel::PushChannel (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
A PushChannel object
23 24 25 |
# File 'lib/ably/rest/channel.rb', line 23 def push @push end |
Instance Method Details
#history(options = {}) ⇒ Ably::Models::PaginatedResult<Ably::Models::Message>
Retrieves a Models::PaginatedResult object, containing an array of historical Models::Message objects for the channel. If the channel is configured to persist messages, then messages can be retrieved from history for up to 72 hours in the past. If not, messages can only be retrieved from history for up to two minutes in the past.
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/ably/rest/channel.rb', line 132 def history( = {}) url = "#{base_path}/messages" = { :direction => :backwards, :limit => 100 }.merge() [:start, :end].each { |option| [option] = as_since_epoch([option]) if .has_key?(option) } raise ArgumentError, ":end must be equal to or after :start" if [:start] && [:end] && ([:start] > [:end]) = { coerce_into: 'Ably::Models::Message', async_blocking_operations: .delete(:async_blocking_operations), } response = client.get(url, ) Ably::Models::PaginatedResult.new(response, url, client, ) do || .tap do |msg| msg end end end |
#presence ⇒ Ably::Rest::Presence
A Presence object.
159 160 161 |
# File 'lib/ably/rest/channel.rb', line 159 def presence @presence ||= Presence.new(client, self) end |
#publish(name, data = nil, attributes = {}) ⇒ Boolean
Publishes a message to the channel. A callback may optionally be passed in to this call to be notified of success or failure of the operation.
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/ably/rest/channel.rb', line 77 def publish(name, data = nil, attributes = {}) qs_params = nil qs_params = data if name.kind_of?(Enumerable) || name.kind_of?(Ably::Models::Message) = (name, data, attributes) # (RSL1a, RSL1b) if .sum(&:size) > ( = client. || Ably::Rest::Client::MAX_MESSAGE_SIZE) raise Ably::Exceptions::MaxMessageSizeExceeded.new("Maximum message size exceeded #{} bytes.") end payload = .map do || Ably::Models::Message(.dup).tap do |msg| msg.encode client.encoders, next if msg.client_id.nil? if msg.client_id == '*' raise Ably::Exceptions::IncompatibleClientId.new('Wildcard client_id is reserved and cannot be used when publishing messages') end unless client.auth.can_assume_client_id?(msg.client_id) raise Ably::Exceptions::IncompatibleClientId.new("Cannot publish with client_id '#{msg.client_id}' as it is incompatible with the current configured client_id '#{client.client_id}'") end end.as_json end.tap do |payload| if client.idempotent_rest_publishing # We cannot mutate for idempotent publishing if one or more messages already has an ID if payload.all? { |msg| !msg['id'] } # Mutate the JSON to support idempotent publishing where a Message.id does not exist idempotent_publish_id = SecureRandom.base64(IDEMPOTENT_LIBRARY_GENERATED_ID_LENGTH) payload.each_with_index do |msg, idx| msg['id'] = "#{idempotent_publish_id}:#{idx}" end end end end = qs_params ? { qs_params: qs_params } : {} response = client.post("#{base_path}/publish", payload.length == 1 ? payload.first : payload, ) [201, 204].include?(response.status) end |
#set_options(channel_options) ⇒ Ably::Models::ChannelOptions Also known as: options=
Sets the Models::ChannelOptions for the channel.
167 168 169 |
# File 'lib/ably/rest/channel.rb', line 167 def () @options = Ably::Models::ChannelOptions() end |
#status ⇒ Ably::Models::ChannelDetails
Retrieves a Models::ChannelDetails object for the channel, which includes status and occupancy metrics.
175 176 177 |
# File 'lib/ably/rest/channel.rb', line 175 def status Ably::Models::ChannelDetails.new(client.get(base_path).body) end |