urlpattern
An implementation of the URL Pattern Standard for Ruby written in Rust.
Description
It's a thin wrapper of denoland/rust-urlpattern with Magnus.
It is useful on the server side when serving different pages based on the URL (a.k.a. routing). It provides pattern matching syntax like /users/:id, similar to route parameters in Express or Path-to-RegExp. You can use it as a foundation to build your own web server or framework.
Installation
Install the gem and add to the application's Gemfile by executing:
bundle add urlpatternIf bundler is not being used to manage dependencies, install the gem by executing:
gem install urlpatternUsage
This library aims to expose an interface as close as possible to the URL Pattern Standard, but some differences are unavoidable because it is designed for Ruby, not JavaScript. For the exact details, please refer to urlpattern.rbs.
Most JavaScript examples from Chrome for Developers and MDN can be adapted to Ruby without much difficulty.
test
require "urlpattern"
pattern = URLPattern::URLPattern.new "https://example.com/admin/*"
pattern.test? "https://example.com/admin/main/" #=> true
pattern.test? "https://example.com/main/" #=> falseexec
require "urlpattern"
pattern = URLPattern::URLPattern.new pathname: "/users/:id/"
result = pattern.exec pathname: "/users/4163/"
result[:pathname][:groups][:id] #=> 4163base_url
require "urlpattern"
pattern = URLPattern::URLPattern.new "b", "https://example.com/a/"
pattern.test? "a/b", "https://example.com/" #=> true
pattern.test? "b", "https://example.com/a/" #=> true
pattern.test? pathname: "b", base_url: "https://example.com/a/" #=> trueignore_case
require "urlpattern"
pattern = URLPattern::URLPattern.new "https://example.com/test"
pattern.test? "https://example.com/test" #=> true
pattern.test? "https://example.com/TeST" #=> false
pattern = URLPattern::URLPattern.new "https://example.com/test", ignore_case: true
pattern.test? "https://example.com/test" #=> true
pattern.test? "https://example.com/TeST" #=> trueLimitations
Due to limitations in the dependency denoland/rust-urlpattern, it may not support all features specified in the standard.
Check skip in test/test_urlpattern.rb.
Development
After checking out the repo, run bin/setup to install dependencies. Then, run rake test to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and the created tag, and push the .gem file to rubygems.org.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/urlpattern/ruby-urlpattern. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.
License
The gem is available as open source under the terms of the MIT License.
Code of Conduct
Everyone interacting in the Urlpattern project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.