diff --git a/lib/skyfall/firehose/commit_message.rb b/lib/skyfall/firehose/commit_message.rb index 00a787f..551ac29 100644 --- a/lib/skyfall/firehose/commit_message.rb +++ b/lib/skyfall/firehose/commit_message.rb @@ -64,7 +64,8 @@ module Skyfall # @param op [Firehose::Operation] # @return [Hash, nil] def raw_record_for_operation(op) - op.cid && blocks.section_with_cid(op.cid) + cid = op.cid + cid && blocks.section_with_cid(cid) end end end diff --git a/lib/skyfall/firehose/operation.rb b/lib/skyfall/firehose/operation.rb index 69b4a94..7736f59 100644 --- a/lib/skyfall/firehose/operation.rb +++ b/lib/skyfall/firehose/operation.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true require_relative '../collection' +require_relative '../errors' require_relative '../firehose' require 'oxygene' @@ -48,22 +49,34 @@ module Skyfall # @return [Symbol] type of the operation (`:create`, `:update` or `:delete`) def action - @json['action'].to_sym + @action ||= @json['action'].to_sym end # @return [String] record collection NSID def collection - @json['path'].split('/')[0] + @collection ||= begin + path = @json['path'] + slash = path.index('/') + raise DecodeError, "Path doesn't contain a /: #{path}" if slash.nil? + + path[0...slash] + end end # @return [String] record rkey def rkey - @json['path'].split('/')[1] + @rkey ||= begin + path = @json['path'] + slash = path.index('/') + raise DecodeError, "Path doesn't contain a /: #{path}" if slash.nil? + + path[(slash + 1)..-1] + end end # @return [String] full AT URI of the record def uri - "at://#{repo}/#{path}" + @uri ||= "at://#{repo}/#{path}" end # @return [Oxygene::CID, nil] CID (Content Identifier) of the record (nil for delete operations) diff --git a/spec/firehose/operation_spec.rb b/spec/firehose/operation_spec.rb index 4f1c184..f41ca02 100644 --- a/spec/firehose/operation_spec.rb +++ b/spec/firehose/operation_spec.rb @@ -73,4 +73,20 @@ describe Skyfall::Firehose::Operation do op.cid.should be_nil end end + + context "if operation path doesn't contain a slash" do + before do + commit_data[1]['ops'][0]['path'] = 'post123' + end + + it 'should raise a DecodeError in #collection' do + op = described_class.new(commit, commit_data[1]['ops'][0]) + expect { op.collection }.to raise_error(Skyfall::DecodeError) + end + + it 'should raise a DecodeError in #rkey' do + op = described_class.new(commit, commit_data[1]['ops'][0]) + expect { op.rkey }.to raise_error(Skyfall::DecodeError) + end + end end