diff --git a/lib/rackstash/message.rb b/lib/rackstash/message.rb index 3513735..3e61e80 100644 --- a/lib/rackstash/message.rb +++ b/lib/rackstash/message.rb @@ -132,6 +132,24 @@ module Rackstash copy_with(message.freeze) end + # @return [Message] Returns a copy of `self` with leading whitespace + # removed on the `message`. + def lstrip + copy_with(@message.lstrip.freeze) + end + + # @return [Message] Returns a copy of `self` with trailing whitespace + # removed on the `message`. + def rstrip + copy_with(@message.rstrip.freeze) + end + + # @return [Message] Returns a copy of `self` with leading and trailing + # whitespace removed on the `message`. + def strip + copy_with(@message.strip.freeze) + end + # @return [String] the human readable label for the {#severity}. # @see Rackstash.severity_label def severity_label diff --git a/spec/rackstash/message_spec.rb b/spec/rackstash/message_spec.rb index 8e0c007..66b5e37 100644 --- a/spec/rackstash/message_spec.rb +++ b/spec/rackstash/message_spec.rb @@ -13,7 +13,8 @@ require 'rackstash/message' describe Rackstash::Message do let(:message_args) { {} } - let(:message) { described_class.new 'message', **message_args } + let(:msg) { 'message' } + let(:message) { described_class.new msg, **message_args } describe '#initialize' do it 'encodes the message as UTF-8' do @@ -118,6 +119,48 @@ describe Rackstash::Message do end end + describe '#lstrip' do + let(:msg) { "\t \r\t\nmy \tmessage\r\n \t" } + + it 'returns a new Message' do + expect(message.lstrip).to be_instance_of(described_class) + expect(message.lstrip).not_to equal message + end + + it 'strips leading whitespace from the message' do + msg = "\t \r\t\nmy \tmessage\r\n \t" + expect(message.lstrip.to_s).to eql "my \tmessage\r\n \t" + end + end + + describe '#rstrip' do + let(:msg) { "\t \r\t\nmy \tmessage\r\n \t" } + + it 'returns a new Message' do + expect(message.rstrip).to be_instance_of(described_class) + expect(message.rstrip).not_to equal message + end + + it 'strips trailing whitespace from the message' do + expect(message.rstrip.to_s).to eql "\t \r\t\nmy \tmessage" + end + end + + describe '#strip' do + let(:msg) { "\t \r\t\nmy \tmessage\r\n \t" } + + it 'returns a new Message' do + expect(message.strip).to be_instance_of(described_class) + expect(message.strip).not_to equal message + end + + it 'strips the message' do + msg = "\t \r\t\nmy \tmessage\r\n \t" + expect(message.strip.to_s).to eql "my \tmessage" + end + end + + describe '#message' do it 'is aliased to to_str' do message = described_class.new('hello world')