From 0d7af1b75338e34390c724102e64bfbec2e42da4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20=C5=9Aliwa?= Date: Mon, 13 Sep 2021 23:50:26 +0200 Subject: [PATCH 1/3] [#273] Added DeltaExtras class and Message#delta_extras method. --- lib/ably/models/delta_extras.rb | 38 +++++++++++++++++++++++++++++++++ lib/ably/models/message.rb | 11 ++++++++++ 2 files changed, 49 insertions(+) create mode 100644 lib/ably/models/delta_extras.rb diff --git a/lib/ably/models/delta_extras.rb b/lib/ably/models/delta_extras.rb new file mode 100644 index 000000000..e1eab8f85 --- /dev/null +++ b/lib/ably/models/delta_extras.rb @@ -0,0 +1,38 @@ +module Ably::Models + # + # @!attribute [r] from + # @return [String] The id of the message the delta was generated from + # @!attribute [r] format + # @return [String] The delta format. Only vcdiff is supported as at API version 1.2 + # + class DeltaExtras + include Ably::Modules::ModelCommon + + # DeltaExtras attributes + # @return [Hash] + # + attr_reader :attributes + + def initialize(attributes = {}) + @attributes = IdiomaticRubyWrapper((attributes || {}), stop_at: [:from, :format]) + end + + # The id of the message the delta was generated from. + # @return [String, nil] + # + def from + attributes[:from] + end + + # The delta format. + # @return [String, nil] + # + def format + attributes[:format] + end + + def to_json(*args) + as_json(args).to_json + end + end +end diff --git a/lib/ably/models/message.rb b/lib/ably/models/message.rb index a8eb89e12..c1b3e0eec 100644 --- a/lib/ably/models/message.rb +++ b/lib/ably/models/message.rb @@ -142,6 +142,9 @@ def protocol_message # Contains any arbitrary key value pairs which may also contain other primitive JSON types, JSON-encodable objects or JSON-encodable arrays. # The extras field is provided to contain message metadata and/or ancillary payloads in support of specific functionality, e.g. push + # 1.2 adds the delta extension which is of type DeltaExtras, and the headers extension, which contains arbitrary string->string key-value pairs, + # settable at publish time. Unless otherwise specified, the client library should not attempt to do any filtering or validation of the extras + # field itself, but should treat it opaquely, encoding it and passing it to realtime unaltered. # @api private def extras attributes[:extras].tap do |val| @@ -151,6 +154,14 @@ def extras end end + # Delta extras extension (TM2i) + # @return [DeltaExtras, nil] + # @api private + def delta_extras + return nil if attributes[:extras][:delta].nil? + @delta_extras ||= DeltaExtras.new(attributes[:extras][:delta]).freeze + end + private def raw_hash_object @raw_hash_object From dea7c53e61066d3fa6c77c5d0e44dd21d4f53119 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20=C5=9Aliwa?= Date: Mon, 20 Sep 2021 19:03:36 +0200 Subject: [PATCH 2/3] [#273] Added specs for DeltaExtras and Message#delta_extras. Updated DeltaExtras class. --- lib/ably/models/delta_extras.rb | 19 +++++-------------- spec/unit/models/delta_extras_spec.rb | 14 ++++++++++++++ spec/unit/models/message_spec.rb | 14 ++++++++++++++ 3 files changed, 33 insertions(+), 14 deletions(-) create mode 100644 spec/unit/models/delta_extras_spec.rb diff --git a/lib/ably/models/delta_extras.rb b/lib/ably/models/delta_extras.rb index e1eab8f85..8a9c35538 100644 --- a/lib/ably/models/delta_extras.rb +++ b/lib/ably/models/delta_extras.rb @@ -8,27 +8,18 @@ module Ably::Models class DeltaExtras include Ably::Modules::ModelCommon - # DeltaExtras attributes - # @return [Hash] - # - attr_reader :attributes - - def initialize(attributes = {}) - @attributes = IdiomaticRubyWrapper((attributes || {}), stop_at: [:from, :format]) - end - # The id of the message the delta was generated from. # @return [String, nil] # - def from - attributes[:from] - end + attr_reader :from # The delta format. # @return [String, nil] # - def format - attributes[:format] + attr_reader :format + + def initialize(attributes = {}) + @from, @format = IdiomaticRubyWrapper((attributes || {}), stop_at: [:from, :format]).attributes.values_at(:from, :format) end def to_json(*args) diff --git a/spec/unit/models/delta_extras_spec.rb b/spec/unit/models/delta_extras_spec.rb new file mode 100644 index 000000000..4b7da7ed5 --- /dev/null +++ b/spec/unit/models/delta_extras_spec.rb @@ -0,0 +1,14 @@ +# encoding: utf-8 +require 'spec_helper' + +describe Ably::Models::DeltaExtras do + subject { described_class.new({ format: 'vcdiff', from: '1234-4567-8910-1001-1111'}) } + + it 'should have `from` attribute' do + expect(subject.from).to eq('1234-4567-8910-1001-1111') + end + + it 'should have `format` attribute' do + expect(subject.format).to eq('vcdiff') + end +end diff --git a/spec/unit/models/message_spec.rb b/spec/unit/models/message_spec.rb index fd51005d5..e94a594b7 100644 --- a/spec/unit/models/message_spec.rb +++ b/spec/unit/models/message_spec.rb @@ -606,4 +606,18 @@ end end end + + context '#delta_extras (TM2i)' do + let(:message) { subject.new({ extras: { delta: { from: '1234-1234-5678-9009', format: 'vcdiff' } } }) } + + let(:delta_extras) { message.delta_extras } + + it 'should return vcdiff format' do + expect(delta_extras.format).to eq('vcdiff') + end + + it 'should return 1234-1234-5678-9009 message id' do + expect(delta_extras.from).to eq('1234-1234-5678-9009') + end + end end From 140195f8a6ff8d6a73a6e7bdfb9fc22d08809f4b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20=C5=9Aliwa?= Date: Mon, 20 Sep 2021 19:21:28 +0200 Subject: [PATCH 3/3] [#273] Added a test when no delta was provided --- spec/unit/models/message_spec.rb | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/spec/unit/models/message_spec.rb b/spec/unit/models/message_spec.rb index e94a594b7..fd5444ae9 100644 --- a/spec/unit/models/message_spec.rb +++ b/spec/unit/models/message_spec.rb @@ -608,16 +608,26 @@ end context '#delta_extras (TM2i)' do - let(:message) { subject.new({ extras: { delta: { from: '1234-1234-5678-9009', format: 'vcdiff' } } }) } - let(:delta_extras) { message.delta_extras } - it 'should return vcdiff format' do - expect(delta_extras.format).to eq('vcdiff') + context 'when delta' do + let(:message) { subject.new({ extras: { delta: { from: '1234-1234-5678-9009', format: 'vcdiff' } } }) } + + it 'should return vcdiff format' do + expect(delta_extras.format).to eq('vcdiff') + end + + it 'should return 1234-1234-5678-9009 message id' do + expect(delta_extras.from).to eq('1234-1234-5678-9009') + end end - it 'should return 1234-1234-5678-9009 message id' do - expect(delta_extras.from).to eq('1234-1234-5678-9009') + context 'when no delta' do + let(:message) { subject.new({ extras: {} }) } + + it 'should return nil' do + expect(delta_extras).to eq(nil) + end end end end