Skip to content
Open
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
2 changes: 1 addition & 1 deletion lib/whois/parsers/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ def response_unavailable?
protected

def content_for_scanner
@content_for_scanner ||= content.to_s.gsub(/\r\n/, "\n")
@content_for_scanner ||= "#{content.to_s.gsub(/\r\n/, "\n").gsub(/\n$/, '')}\n"
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a matter of readability, it's not immidiately clear what we're doing here and why we're doing it.

Copy link
Copy Markdown
Author

@xaf xaf Jul 14, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What change would you suggest ?
Would that be acceptable ?

Suggested change
@content_for_scanner ||= "#{content.to_s.gsub(/\r\n/, "\n").gsub(/\n$/, '')}\n"
@content_for_scanner ||= begin
content_str = content.to_s
content_str.gsub!(/\r\n/, "\n")
# We need a new line at the end, which exists in some cases, and does
# not in others, we will thus remove the ending new line character if it
# exists (or do nothing if it does not), then return the content appended
# with a new line character
content_str.gsub!(/\n$/, '')
"#{content_str}\n"
end

end

def cached_properties_fetch(key)
Expand Down
9 changes: 7 additions & 2 deletions spec/whois/parsers/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,21 @@
end

describe "#content_for_scanner" do
it "returns the part body with line feed normalized" do
instance = described_class.new(Whois::Record::Part.new(:body => "This is\r\nthe response.\r\n", :host => "whois.example.test"))
expect(instance.send(:content_for_scanner)).to eq("This is\nthe response.\n")
end

it "returns the part body with line feed normalized" do
instance = described_class.new(Whois::Record::Part.new(:body => "This is\r\nthe response.", :host => "whois.example.test"))
expect(instance.send(:content_for_scanner)).to eq("This is\nthe response.")
expect(instance.send(:content_for_scanner)).to eq("This is\nthe response.\n")
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I understand that it's better for the scanner to ingest whole lines (including linebreak) instead of just the string on it, thus rendering the line empty but still present, right?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

end

it "caches the result" do
instance = described_class.new(Whois::Record::Part.new(:body => "This is\r\nthe response.", :host => "whois.example.test"))
expect(instance.instance_eval { @content_for_scanner }).to be_nil
instance.send(:content_for_scanner)
expect(instance.instance_eval { @content_for_scanner }).to eq("This is\nthe response.")
expect(instance.instance_eval { @content_for_scanner }).to eq("This is\nthe response.\n")
end
end

Expand Down