diff --git a/lib/ably/models/delta_extras.rb b/lib/ably/models/delta_extras.rb new file mode 100644 index 000000000..8a9c35538 --- /dev/null +++ b/lib/ably/models/delta_extras.rb @@ -0,0 +1,29 @@ +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 + + # The id of the message the delta was generated from. + # @return [String, nil] + # + attr_reader :from + + # The delta format. + # @return [String, nil] + # + attr_reader :format + + def initialize(attributes = {}) + @from, @format = IdiomaticRubyWrapper((attributes || {}), stop_at: [:from, :format]).attributes.values_at(:from, :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 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..fd5444ae9 100644 --- a/spec/unit/models/message_spec.rb +++ b/spec/unit/models/message_spec.rb @@ -606,4 +606,28 @@ end end end + + context '#delta_extras (TM2i)' do + let(:delta_extras) { message.delta_extras } + + 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 + + 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