diff --git a/lib/ably/models/channel_details.rb b/lib/ably/models/channel_details.rb new file mode 100644 index 000000000..09bf0d4b1 --- /dev/null +++ b/lib/ably/models/channel_details.rb @@ -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 diff --git a/lib/ably/models/channel_metrics.rb b/lib/ably/models/channel_metrics.rb new file mode 100644 index 000000000..e83c25df5 --- /dev/null +++ b/lib/ably/models/channel_metrics.rb @@ -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 diff --git a/lib/ably/models/channel_occupancy.rb b/lib/ably/models/channel_occupancy.rb new file mode 100644 index 000000000..2a02a6126 --- /dev/null +++ b/lib/ably/models/channel_occupancy.rb @@ -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 diff --git a/lib/ably/models/channel_status.rb b/lib/ably/models/channel_status.rb new file mode 100644 index 000000000..3355ce716 --- /dev/null +++ b/lib/ably/models/channel_status.rb @@ -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 diff --git a/lib/ably/rest/channel.rb b/lib/ably/rest/channel.rb index 734d689a4..ed61c8930 100644 --- a/lib/ably/rest/channel.rb +++ b/lib/ably/rest/channel.rb @@ -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 diff --git a/spec/acceptance/rest/channel_spec.rb b/spec/acceptance/rest/channel_spec.rb index 49fe963b1..ae2665cb8 100644 --- a/spec/acceptance/rest/channel_spec.rb +++ b/spec/acceptance/rest/channel_spec.rb @@ -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 diff --git a/spec/unit/models/channel_details_spec.rb b/spec/unit/models/channel_details_spec.rb new file mode 100644 index 000000000..1616fc4aa --- /dev/null +++ b/spec/unit/models/channel_details_spec.rb @@ -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 diff --git a/spec/unit/models/channel_metrics_spec.rb b/spec/unit/models/channel_metrics_spec.rb new file mode 100644 index 000000000..275893f91 --- /dev/null +++ b/spec/unit/models/channel_metrics_spec.rb @@ -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 diff --git a/spec/unit/models/channel_occupancy_spec.rb b/spec/unit/models/channel_occupancy_spec.rb new file mode 100644 index 000000000..5c90c7d39 --- /dev/null +++ b/spec/unit/models/channel_occupancy_spec.rb @@ -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 diff --git a/spec/unit/models/channel_status_spec.rb b/spec/unit/models/channel_status_spec.rb new file mode 100644 index 000000000..ba99d20bf --- /dev/null +++ b/spec/unit/models/channel_status_spec.rb @@ -0,0 +1,36 @@ +require 'spec_helper' +require 'shared/model_behaviour' + +describe Ably::Models::ChannelStatus do + subject { Ably::Models::ChannelStatus({ isActive: 'true', occupancy: { metrics: { connections: 1, presence_connections: 2, presence_members: 2, presence_subscribers: 5, publishers: 7, subscribers: 9 } } }) } + + describe '#is_active' do + context 'when occupancy is active' do + subject { Ably::Models::ChannelStatus({ isActive: true, occupancy: { metrics: { connections: 1, presence_connections: 2, presence_members: 2, presence_subscribers: 5, publishers: 7, subscribers: 9 } } }) } + + it 'should return true' do + expect(subject.is_active).to eq(true) + end + end + + context 'when occupancy is not active' do + subject { Ably::Models::ChannelStatus({ isActive: false, occupancy: { metrics: { connections: 1, presence_connections: 2, presence_members: 2, presence_subscribers: 5, publishers: 7, subscribers: 9 } } }) } + + it 'should return false' do + expect(subject.is_active).to eq(false) + end + end + end + + describe '#occupancy' do + it 'should return occupancy object' do + expect(subject.occupancy).to be_a(Ably::Models::ChannelOccupancy) + expect(subject.occupancy.metrics.connections).to eq(1) + expect(subject.occupancy.metrics.presence_connections).to eq(2) + expect(subject.occupancy.metrics.presence_members).to eq(2) + expect(subject.occupancy.metrics.presence_subscribers).to eq(5) + expect(subject.occupancy.metrics.publishers).to eq(7) + expect(subject.occupancy.metrics.subscribers).to eq(9) + end + end +end