Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions lib/ably/models/delta_extras.rb
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions lib/ably/models/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand All @@ -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
Expand Down
14 changes: 14 additions & 0 deletions spec/unit/models/delta_extras_spec.rb
Original file line number Diff line number Diff line change
@@ -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
24 changes: 24 additions & 0 deletions spec/unit/models/message_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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