iri

0.8.0last stable release 1 year ago
Complexity Score
Low
Open Issues
N/A
Dependent Projects
5
Weekly Downloadsglobal
167

License

  • MIT
    • Yesattribution
    • Permissivelinking
    • Permissivedistribution
    • Permissivemodification
    • Nopatent grant
    • Yesprivate use
    • Permissivesublicensing
    • Notrademark grant

Downloads

Readme

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') # -> http://google.com/find/me
  .add(q: 'books about OOP', limit: 50) # -> ?q=books+about+OOP&limit=50
  .del(:q) # remove this query parameter
  .del('limit', 'speed') # also remove these two
  .over(q: 'books about tennis', limit: 10) # replace these params
  .scheme('https') # replace 'http' with 'https'
  .host('localhost') # replace the host name
  .port('443') # replace the port
  .fragment('page-4') # replaces the fragment part of the URI, after the '#'
  .query('a=1&b=2') # replaces the entire query part of the URI
  .path('/new/path') # replace the path of the URI, leaving the query untouched
  .cut('/q') # replace everything after the host and port
  .to_s # convert it to a string

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.

Dependencies

No runtime dependency information found for this package.

CVE IssuesActive
0
Scorecards Score
No Data
Test Coverage
No Data
Follows Semver
Yes
Github Stars
114
Dependenciestotal
0
DependenciesOutdated
0
DependenciesDeprecated
0
Threat Modelling
No Data
Repo Audits
No Data

Learn how to distribute iri in your own private RubyGems registry

gem install iri
Processing...
Done

12 Releases

RubyGems on Cloudsmith

Getting started with RubyGems on Cloudsmith is fast and easy.