The class Iri
helps you build a URI and then modify its
parts via a simple fluent interface:
require 'iri'
url = Iri.new('http://google.com/')
.append('find').append('me')
.add(q: 'books about OOP', limit: 50)
.del(:q)
.del('limit', 'speed')
.over(q: 'books about tennis', limit: 10)
.scheme('https')
.host('localhost')
.port('443')
.fragment('page-4')
.query('a=1&b=2')
.path('/new/path')
.cut('/q')
.to_s
The full list of methods is here.
Install it:
$ gem install iri
Or add this to your Gemfile
:
gem 'iri'
Pay attention, it is not a parser. The only functionality this gem provides
is building URIs.
It is very convenient to use inside
HAML, for example:
- iri = Iri.new(request.url)
%a{href: iri.over(offset: offset + 10)} Next Page
%a{href: iri.over(offset: offset - 10)} Previous Page
Of course, it's better to create the iri
object only once per request
and re-use it where you need. It's immutable, so you won't have any
side-effects.
PS. See how I use it in this Sinatra web app: yegor256/0rsk.
How to contribute
Read these guidelines.
Make sure you build is green before you contribute
your pull request. You will need to have Ruby 2.3+ and
Bundler installed. Then:
$ bundle update
$ bundle exec rake
If it's clean and you don't see any error messages, submit your pull request.