Skip to content

Commit

Permalink
Fix: don't raise on invalid URIs
Browse files Browse the repository at this point in the history
In the initial implementation to support URIs, I overlooked that it
could raise an exception. This is now handled, in which case we treat
the input String as plain text, instead of decomposing it into tokens.

The code triggers Rubocop's desire to have shorter methods, so I'll see
about reducing the complexity of the method next.
  • Loading branch information
Narnach committed Feb 28, 2022
1 parent 52f34e8 commit 7fd1ad7
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/groupie/tokenizer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ def strip_html_tags!
def tokenize_urls!
@raw.gsub!(%r{http[\w\-\#:/_.?&=]+}) do |url|
uri = URI.parse(url)
rescue URI::InvalidURIError
url
else
path = uri.path.to_s
path.tr!('/_\-', ' ')
query = uri.query.to_s
Expand Down
4 changes: 4 additions & 0 deletions spec/groupie_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,10 @@
%w[https example.org blog path 2022 1234 title of blog post custom query foo bar my anchor]
)
end

it 'treats invalid URLs as plain text' do
expect(Groupie.tokenize('http://localhost:3000&amp')).to eq(%w[http localhost 3000 amp])
end
end

describe 'when smart_weight is enabled' do
Expand Down

0 comments on commit 7fd1ad7

Please sign in to comment.