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 Yegor Bugayenko

License

MIT

Instance Method Summary collapse

Constructor Details

#initialize(uri) ⇒ Iri

Makes a new object.


49
50
51
# File 'lib/iri.rb', line 49

def initialize(uri)
  @uri = URI(uri)
end

Instance Method Details

#add(hash) ⇒ Object


61
62
63
64
65
66
67
68
# File 'lib/iri.rb', line 61

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

#cut(path = '/') ⇒ Object

Remove the entire path+query+fragment part.


124
125
126
127
128
129
130
# File 'lib/iri.rb', line 124

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

#del(*keys) ⇒ Object


70
71
72
73
74
75
76
# File 'lib/iri.rb', line 70

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

#host(val) ⇒ Object

Replace the host.


96
97
98
99
100
# File 'lib/iri.rb', line 96

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

#over(hash) ⇒ Object

Replace the query argument(s).


79
80
81
82
83
84
85
86
# File 'lib/iri.rb', line 79

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.


110
111
112
113
114
# File 'lib/iri.rb', line 110

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

#port(val) ⇒ Object

Replace the port.


103
104
105
106
107
# File 'lib/iri.rb', line 103

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

#query(val) ⇒ Object

Replace the query part of the URI.


117
118
119
120
121
# File 'lib/iri.rb', line 117

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

#scheme(val) ⇒ Object

Replace the scheme.


89
90
91
92
93
# File 'lib/iri.rb', line 89

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

#to_sObject


53
54
55
# File 'lib/iri.rb', line 53

def to_s
  @uri.to_s
end

#to_uriObject


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

def to_uri
  @uri.clone
end