Class: Ably::Rest::Presence

Inherits:
Object
  • Object
show all
Includes:
Modules::Conversions
Defined in:
lib/ably/rest/presence.rb

Overview

Enables the retrieval of the current and historic presence set for a channel.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, channel) ⇒ Presence

Initialize a new Presence object

Parameters:



26
27
28
29
# File 'lib/ably/rest/presence.rb', line 26

def initialize(client, channel)
  @client  = client
  @channel = channel
end

Instance Attribute Details

#channelAbly::Rest::Channel (readonly)

Channel this Presence object is associated with

Returns:



19
20
21
# File 'lib/ably/rest/presence.rb', line 19

def channel
  @channel
end

#clientAbly::Rest::Client (readonly)

Client for this Presence object

Returns:



13
14
15
# File 'lib/ably/rest/presence.rb', line 13

def client
  @client
end

Instance Method Details

#get(options = {}) ⇒ Ably::Models::PaginatedResult<Ably::Models::PresenceMessage>

Retrieves the current members present on the channel and the metadata for each member, such as their Models::PresenceMessage::ACTION and ID. Returns a Models::PaginatedResult object, containing an array of Models::PresenceMessage objects.

Parameters:

  • options (Hash) (defaults to: {})

    the options for the set of members present

Options Hash (options):

  • :limit (Integer)

    An upper limit on the number of messages returned. The default is 100, and the maximum is 1000. (RSP3a)

  • :client_id (String)

    Filters the list of returned presence members by a specific client using its ID. (RSP3a2)

  • :connection_id (String)

    Filters the list of returned presence members by a specific connection using its ID. (RSP3a3)

Returns:



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/ably/rest/presence.rb', line 44

def get(options = {})
  options = options = {
    :limit     => 100
  }.merge(options)

  paginated_options = {
    coerce_into: 'Ably::Models::PresenceMessage',
    async_blocking_operations: options.delete(:async_blocking_operations),
  }

  response = client.get(base_path, options)

  Ably::Models::PaginatedResult.new(response, base_path, client, paginated_options) do |presence_message|
    presence_message.tap do |message|
      decode_message message
    end
  end
end

#history(options = {}) ⇒ Ably::Models::PaginatedResult<Ably::Models::PresenceMessage>

Retrieves a Models::PaginatedResult object, containing an array of historical Models::PresenceMessage objects for the channel. If the channel is configured to persist messages, then presence messages can be retrieved from history for up to 72 hours in the past. If not, presence messages can only be retrieved from history for up to two minutes in the past.

Parameters:

  • options (Hash) (defaults to: {})

    the options for the message history request

Options Hash (options):

  • :start (Integer, Time)

    The time from which messages are retrieved, specified as milliseconds since the Unix epoch. (RSP4b1)

  • :end (Integer, Time)

    The time until messages are retrieved, specified as milliseconds since the Unix epoch. (RSP4b1)

  • :direction (Symbol)

    The order for which messages are returned in. Valid values are backwards which orders messages from most recent to oldest, or forwards which orders messages from oldest to most recent. The default is backwards. (RSP4b2)

  • :limit (Integer)

    An upper limit on the number of messages returned. The default is 100, and the maximum is 1000. (RSP4b3)

Returns:

Raises:

  • (ArgumentError)


77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/ably/rest/presence.rb', line 77

def history(options = {})
  url = "#{base_path}/history"
  options = options = {
    :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::PresenceMessage',
    async_blocking_operations: options.delete(:async_blocking_operations),
  }

  response = client.get(url, options)

  Ably::Models::PaginatedResult.new(response, url, client, paginated_options) do |presence_message|
    presence_message.tap do |message|
      decode_message message
    end
  end
end