Class: Iri

Inherits:
Object
  • Object
show all
Defined in:
lib/iri.rb

Overview

It is a simple URI builder.

require 'iri'
url = Iri.new('http://google.com/')
  .add(q: 'books about OOP', limit: 50)
  .del(:q) // remove this query parameter
  .del('limit') // remove this one too
  .over(q: 'books about tennis', limit: 10) // replace these params
  .scheme('https')
  .host('localhost')
  .port('443')
  .to_s

For more information read README file.

Author

Yegor Bugayenko ([email protected])

Copyright

Copyright © 2019-2020 Yegor Bugayenko

License

MIT

Defined Under Namespace

Classes: InvalidURI

Instance Method Summary collapse

Constructor Details

#initialize(uri = '', safe: true) ⇒ Iri

Makes a new object.

You can even ignore the argument, which will produce an empty URI.

By default, this class will never throw any exceptions, even if your URI is not valid. It will just assume that the URI is“/”. However, you can turn this mode off, by specifying safe as FALSE.


58
59
60
61
# File 'lib/iri.rb', line 58

def initialize(uri = '', safe: true)
  @uri = uri
  @safe = safe
end

Instance Method Details

#add(hash) ⇒ Object

Add a few query arguments.

For example:

Iri.new('https://google.com').add(q: 'test', limit: 10)

You can add many of them and they will all be present in the resulting URI, even if their names are the same. In order to make sure you have only one instance of a query argument, use del first:

Iri.new('https://google.com').del(:q).add(q: 'test')

85
86
87
88
89
90
91
92
# File 'lib/iri.rb', line 85

def add(hash)
  modify_query do |params|
    hash.each do |k, v|
      params[k.to_s] = [] unless params[k.to_s]
      params[k.to_s] << v
    end
  end
end

#append(part) ⇒ Object

Append something new to the path.

For example:

Iri.new('https://google.com/a/b?q=test').append('/hello')

The result will contain “google.com/a/b/hello?q=test”.


185
186
187
188
189
190
# File 'lib/iri.rb', line 185

def append(part)
  modify do |c|
    tail = (c.path.end_with?('/') ? '' : '/') + CGI.escape(part.to_s)
    c.path = c.path + tail
  end
end

#cut(path = '/') ⇒ Object

Remove the entire path+query+fragment part.

For example:

Iri.new('https://google.com/a/b?q=test').cut('/hello')

The result will contain “google.com/hello”.


170
171
172
173
174
175
176
# File 'lib/iri.rb', line 170

def cut(path = '/')
  modify do |c|
    c.query = nil
    c.path = path
    c.fragment = nil
  end
end

#del(*keys) ⇒ Object

Delete a few query arguments.

For example:

Iri.new('https://google.com?q=test').del(:q)

100
101
102
103
104
105
106
# File 'lib/iri.rb', line 100

def del(*keys)
  modify_query do |params|
    keys.each do |k|
      params.delete(k.to_s)
    end
  end
end

#fragment(val) ⇒ Object

Replace the fragment part of the URI.


150
151
152
153
154
# File 'lib/iri.rb', line 150

def fragment(val)
  modify do |c|
    c.fragment = val.to_s
  end
end

#host(val) ⇒ Object

Replace the host.


129
130
131
132
133
# File 'lib/iri.rb', line 129

def host(val)
  modify do |c|
    c.host = val
  end
end

#over(hash) ⇒ Object

Replace query argument(s).

Iri.new('https://google.com?q=test').over(q: 'hey you!')

112
113
114
115
116
117
118
119
# File 'lib/iri.rb', line 112

def over(hash)
  modify_query do |params|
    hash.each do |k, v|
      params[k.to_s] = [] unless params[k]
      params[k.to_s] = [v]
    end
  end
end

#path(val) ⇒ Object

Replace the path part of the URI.


143
144
145
146
147
# File 'lib/iri.rb', line 143

def path(val)
  modify do |c|
    c.path = val
  end
end

#port(val) ⇒ Object

Replace the port.


136
137
138
139
140
# File 'lib/iri.rb', line 136

def port(val)
  modify do |c|
    c.port = val
  end
end

#query(val) ⇒ Object

Replace the query part of the URI.


157
158
159
160
161
# File 'lib/iri.rb', line 157

def query(val)
  modify do |c|
    c.query = val
  end
end

#scheme(val) ⇒ Object

Replace the scheme.


122
123
124
125
126
# File 'lib/iri.rb', line 122

def scheme(val)
  modify do |c|
    c.scheme = val
  end
end

#to_sObject

Convert it to a string.


64
65
66
# File 'lib/iri.rb', line 64

def to_s
  @uri.to_s
end

#to_uriObject

Convert it to an object of class URI.


69
70
71
# File 'lib/iri.rb', line 69

def to_uri
  the_uri.clone
end