Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix invalid url raising error during Groupie.tokenize #44

Merged
merged 3 commits into from
Feb 28, 2022

Conversation

Narnach
Copy link
Owner

@Narnach Narnach commented Feb 28, 2022

What's the problem?

An invalid URL-like String would cause Groupie.tokenize to raise URI::InvalidURIError.

How to reproduce it?

Groupie.tokenize('http://localhost:3000&amp')

What's changed in the solution?

The input that resembles a URL but fails to parse is no longer treated like a real URL, but simply as text that's processed by the rest of the code.

  • Fix: don't raise on invalid URIs (7fd1ad7)

Due to the added responsibility of handling this new concern (is the URL valid or not?) I've extracted a helper method that encapsulates the new behavior and condensed the old code (which improves readability to my eyes) to stay within the line count guidelines that Rubocop enforces by default.

  • Refactor: condense Tokenizer#tokenize_urls! (c48148e)

The changelog now points to both PRs for this feature.

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.
- Extracted `maybe_parse_url` to encapsulate that Strings matched by
  gsub might not in fact be valid URls.
- Condensed the `var = (uri part).to_s; var.tr!()` logic required due to
  `String#tr!` not returning `self` in case of a no-op to put it in a
  `tap {}` block instead. I'm not in love with the solution, but it's
  a minor improvement over the previous one. One line now matches to one
  part of the url.
@Narnach Narnach merged commit 60fe7cf into stable Feb 28, 2022
@Narnach Narnach deleted the fix-invalid-url branch February 28, 2022 10:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant