Skip to content

Commit

Permalink
Merge pull request #365 from ably/issue-362
Browse files Browse the repository at this point in the history
Add RSL8, RSL8a lifecycle channel status
  • Loading branch information
owenpearson authored May 31, 2022
2 parents 4c472b4 + 24ef7ea commit 0f88cbe
Show file tree
Hide file tree
Showing 10 changed files with 389 additions and 0 deletions.
59 changes: 59 additions & 0 deletions lib/ably/models/channel_details.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
module Ably::Models
# Convert token details argument to a {ChannelDetails} object
#
# @param attributes (see #initialize)
#
# @return [ChannelDetails]
def self.ChannelDetails(attributes)
case attributes
when ChannelDetails
return attributes
else
ChannelDetails.new(attributes)
end
end

# ChannelDetails is a type that represents information for a channel including channelId, name, status and occupancy (CHD1)
#
class ChannelDetails
extend Ably::Modules::Enum
extend Forwardable
include Ably::Modules::ModelCommon

# The attributes of ChannelDetails (CHD2)
#
attr_reader :attributes

alias_method :to_h, :attributes

# Initialize a new ChannelDetails
#
def initialize(attrs)
@attributes = IdiomaticRubyWrapper(attrs.clone)
end

# The identifier of the channel (CHD2a)
#
# @return [String]
#
def channel_id
attributes[:channel_id]
end

# The identifier of the channel (CHD2a)
#
# @return [String]
#
def name
attributes[:name]
end

# The status of the channel (CHD2b)
#
# @return [Ably::Models::ChannelStatus, nil]
#
def status
Ably::Models::ChannelStatus(attributes[:status])
end
end
end
84 changes: 84 additions & 0 deletions lib/ably/models/channel_metrics.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
module Ably::Models
# Convert token details argument to a {ChannelMetrics} object
#
# @param attributes (see #initialize)
#
# @return [ChannelMetrics]
def self.ChannelMetrics(attributes)
case attributes
when ChannelMetrics
return attributes
else
ChannelMetrics.new(attributes)
end
end

# ChannelMetrics is a type that contains the count of publishers and subscribers, connections and presenceConnections,
# presenceMembers and presenceSubscribers (CHM1)
#
class ChannelMetrics
extend Ably::Modules::Enum
extend Forwardable
include Ably::Modules::ModelCommon

# The attributes of ChannelMetrics (CHM2)
#
attr_reader :attributes

alias_method :to_h, :attributes

# Initialize a new ChannelMetrics
#
def initialize(attrs)
@attributes = IdiomaticRubyWrapper(attrs.clone)
end

# The total number of connections to the channel (CHM2a)
#
# @return [Integer]
#
def connections
attributes[:connections]
end

# The total number of presence connections to the channel (CHM2b)
#
# @return [Integer]
#
def presence_connections
attributes[:presence_connections]
end

# The total number of presence members for the channel (CHM2c)
#
# @return [Integer]
#
def presence_members
attributes[:presence_members]
end

# The total number of presence subscribers for the channel (CHM2d)
#
# @return [Integer]
#
def presence_subscribers
attributes[:presence_subscribers]
end

# The total number of publishers to the channel (CHM2e)
#
# @return [Integer]
#
def publishers
attributes[:publishers]
end

# The total number of subscribers to the channel (CHM2f)
#
# @return [Integer]
#
def subscribers
attributes[:subscribers]
end
end
end
43 changes: 43 additions & 0 deletions lib/ably/models/channel_occupancy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
module Ably::Models
# Convert token details argument to a {ChannelOccupancy} object
#
# @param attributes (see #initialize)
#
# @return [ChannelOccupancy]
def self.ChannelOccupancy(attributes)
case attributes
when ChannelOccupancy
return attributes
else
ChannelOccupancy.new(attributes)
end
end

# Type that contain channel metrics (CHO1)
#
class ChannelOccupancy
extend Ably::Modules::Enum
extend Forwardable
include Ably::Modules::ModelCommon

# The attributes of ChannelOccupancy (CH02)
#
attr_reader :attributes

alias_method :to_h, :attributes

# Initialize a new ChannelOccupancy
#
def initialize(attrs)
@attributes = IdiomaticRubyWrapper(attrs.clone)
end

# Metrics object (CHO2a)
#
# @return [Ably::Models::ChannelMetrics, nil]
#
def metrics
Ably::Models::ChannelMetrics(attributes[:metrics])
end
end
end
53 changes: 53 additions & 0 deletions lib/ably/models/channel_status.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
module Ably::Models
# Convert token details argument to a {ChannelStatus} object
#
# @param attributes (see #initialize)
#
# @return [ChannelStatus]
def self.ChannelStatus(attributes)
case attributes
when ChannelStatus
return attributes
else
ChannelStatus.new(attributes)
end
end

# ChannelStatus is a type that contains status and occupancy for a channel (CHS1)
#
class ChannelStatus
extend Ably::Modules::Enum
extend Forwardable
include Ably::Modules::ModelCommon

# The attributes of ChannelStatus (CHS2)
#
attr_reader :attributes

alias_method :to_h, :attributes

# Initialize a new ChannelStatus
#
def initialize(attrs)
@attributes = IdiomaticRubyWrapper(attrs.clone)
end

# Represents if the channel is active (CHS2a)
#
# @return [Boolean]
#
def is_active
attributes[:isActive]
end
alias_method :active?, :is_active
alias_method :is_active?, :is_active

# Occupancy ChannelOccupancy – occupancy is an object containing the metrics for the channel (CHS2b)
#
# @return [Ably::Models::ChannelOccupancy, nil]
#
def occupancy
Ably::Models::ChannelOccupancy(attributes[:occupancy])
end
end
end
7 changes: 7 additions & 0 deletions lib/ably/rest/channel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,13 @@ def set_options(channel_options)
end
alias options= set_options

# Makes GET request for channel details (#RSL8, #RSL8a)
#
# @return [Ably::Models::ChannelDetails]
def status
Ably::Models::ChannelDetails.new(client.get(base_path).body)
end

private

def base_path
Expand Down
18 changes: 18 additions & 0 deletions spec/acceptance/rest/channel_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -595,5 +595,23 @@
expect(channel.presence).to be_a(Ably::Rest::Presence)
end
end

context '#status' do
let(:channel_name) { "persisted:#{random_str(4)}" }
let(:channel) { client.channel(channel_name) }
let(:channel_details) { channel.status }

it 'should return channel details status (#RSL8, #RSL8a)' do
expect(channel_details.channel_id).to eq(channel_name)
expect(channel_details.name).to eq(channel_name)
expect(channel_details.status).to be_a(Ably::Models::ChannelStatus)
expect(channel_details.status.is_active).to eq(true)
expect(channel_details.status.occupancy.metrics.publishers).to eq(0)
expect(channel_details.status.occupancy.metrics.subscribers).to eq(0)
expect(channel_details.status.occupancy.metrics.presence_connections).to eq(0)
expect(channel_details.status.occupancy.metrics.presence_members).to eq(0)
expect(channel_details.status.occupancy.metrics.presence_subscribers).to eq(0)
end
end
end
end
30 changes: 30 additions & 0 deletions spec/unit/models/channel_details_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require 'spec_helper'
require 'shared/model_behaviour'

describe Ably::Models::ChannelDetails do
subject { Ably::Models::ChannelDetails(channel_id: 'channel-id-123-xyz', name: 'name', status: { isActive: 'true', occupancy: { metrics: { connections: 1, presence_connections: 2, presence_members: 2, presence_subscribers: 5, publishers: 7, subscribers: 9 } } }) }

describe '#channel_id' do
it 'should return channel id' do
expect(subject.channel_id).to eq('channel-id-123-xyz')
end
end

describe '#name' do
it 'should return name' do
expect(subject.name).to eq('name')
end
end

describe '#status' do
it 'should return status' do
expect(subject.status).to be_a(Ably::Models::ChannelStatus)
expect(subject.status.occupancy.metrics.connections).to eq(1)
expect(subject.status.occupancy.metrics.presence_connections).to eq(2)
expect(subject.status.occupancy.metrics.presence_members).to eq(2)
expect(subject.status.occupancy.metrics.presence_subscribers).to eq(5)
expect(subject.status.occupancy.metrics.publishers).to eq(7)
expect(subject.status.occupancy.metrics.subscribers).to eq(9)
end
end
end
42 changes: 42 additions & 0 deletions spec/unit/models/channel_metrics_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
require 'spec_helper'
require 'shared/model_behaviour'

describe Ably::Models::ChannelMetrics do
subject { Ably::Models::ChannelMetrics(connections: 1, presence_connections: 2, presence_members: 2, presence_subscribers: 5, publishers: 7, subscribers: 9) }

describe '#connections' do
it 'should return integer' do
expect(subject.connections).to eq(1)
end
end

describe '#presence_connections' do
it 'should return integer' do
expect(subject.presence_connections).to eq(2)
end
end

describe '#presence_members' do
it 'should return integer' do
expect(subject.presence_members).to eq(2)
end
end

describe '#presence_subscribers' do
it 'should return integer' do
expect(subject.presence_subscribers).to eq(5)
end
end

describe '#publishers' do
it 'should return integer' do
expect(subject.publishers).to eq(7)
end
end

describe '#subscribers' do
it 'should return integer' do
expect(subject.subscribers).to eq(9)
end
end
end
17 changes: 17 additions & 0 deletions spec/unit/models/channel_occupancy_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require 'spec_helper'
require 'shared/model_behaviour'

describe Ably::Models::ChannelOccupancy do
subject { Ably::Models::ChannelOccupancy({ metrics: { connections: 1, presence_connections: 2, presence_members: 2, presence_subscribers: 5, publishers: 7, subscribers: 9 } }) }

describe '#metrics' do
it 'should return attributes' do
expect(subject.metrics.connections).to eq(1)
expect(subject.metrics.presence_connections).to eq(2)
expect(subject.metrics.presence_members).to eq(2)
expect(subject.metrics.presence_subscribers).to eq(5)
expect(subject.metrics.publishers).to eq(7)
expect(subject.metrics.subscribers).to eq(9)
end
end
end
Loading

0 comments on commit 0f88cbe

Please sign in to comment.