1
0
mirror of https://github.com/meineerde/rackstash.git synced 2025-10-17 14:01:01 +00:00

Add Message#strip methods to strip whitespace from messages

These mehods work very similar to their String equivalents.
This commit is contained in:
Holger Just 2017-08-08 22:26:23 +02:00
parent e999746f1e
commit 3a1f3686c0
2 changed files with 62 additions and 1 deletions

View File

@ -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

View File

@ -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')