Class: URI::Builder::DSL
- Inherits:
-
Object
- Object
- URI::Builder::DSL
- Defined in:
- lib/uri/builder.rb
Instance Attribute Summary collapse
-
#uri ⇒ Object
readonly
Returns the value of attribute uri.
Instance Method Summary collapse
- #clear_fragment ⇒ Object
- #clear_path ⇒ Object (also: #root)
- #clear_query ⇒ Object
- #clear_trailing_slash ⇒ Object
-
#initialize(uri) ⇒ DSL
constructor
A new instance of DSL.
- #join ⇒ Object
- #parent ⇒ Object
- #path(*segments) ⇒ Object
- #query(value) ⇒ Object
- #scheme(value) ⇒ Object
- #to_s ⇒ Object
- #to_str ⇒ Object
- #trailing_slash ⇒ Object
Constructor Details
#initialize(uri) ⇒ DSL
Returns a new instance of DSL.
39 40 41 |
# File 'lib/uri/builder.rb', line 39 def initialize(uri) @uri = uri.clone end |
Instance Attribute Details
#uri ⇒ Object (readonly)
Returns the value of attribute uri.
37 38 39 |
# File 'lib/uri/builder.rb', line 37 def uri @uri end |
Instance Method Details
#clear_fragment ⇒ Object
49 50 51 |
# File 'lib/uri/builder.rb', line 49 def clear_fragment wrap :fragment, nil end |
#clear_path ⇒ Object Also known as: root
90 91 92 |
# File 'lib/uri/builder.rb', line 90 def clear_path path "/" end |
#clear_query ⇒ Object
81 82 83 |
# File 'lib/uri/builder.rb', line 81 def clear_query wrap :query, nil end |
#clear_trailing_slash ⇒ Object
99 100 101 |
# File 'lib/uri/builder.rb', line 99 def clear_trailing_slash wrap :path, Path.new(@uri.path).trailing_slash(false).to_s end |
#join ⇒ Object
103 104 105 |
# File 'lib/uri/builder.rb', line 103 def join(...) wrap :path, Path.new(@uri.path).join(...).to_s end |
#parent ⇒ Object
107 108 109 |
# File 'lib/uri/builder.rb', line 107 def parent(...) wrap :path, Path.new(@uri.path).parent(...).to_s end |
#path(*segments) ⇒ Object
85 86 87 88 |
# File 'lib/uri/builder.rb', line 85 def path(*segments) # Make sure there's a leading / if a non leading / is given. wrap :path, Path.new(*segments).to_s end |
#query(value) ⇒ Object
70 71 72 73 74 75 76 77 78 79 |
# File 'lib/uri/builder.rb', line 70 def query(value) value = case value when Hash, Array build_query value else value end wrap :query, value end |
#scheme(value) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/uri/builder.rb', line 53 def scheme(value) if @uri.scheme # Handles URLs without schemes, like https://example.com/foo target_scheme = URI.scheme_list[value.upcase] args = Hash[target_scheme.component.map { |attr| [ attr, @uri.send(attr) ] }] @uri = target_scheme.build(**args) else # Handles URLs without schemes, like example.com/foo uri = URI.parse("#{value}://#{@uri.path}") (uri.component - %i[host path scheme]).each do |component| uri.send "#{component}=", @uri.send(component) end @uri = uri end self end |
#to_s ⇒ Object
111 112 113 |
# File 'lib/uri/builder.rb', line 111 def to_s uri.to_s end |
#to_str ⇒ Object
115 116 117 |
# File 'lib/uri/builder.rb', line 115 def to_str uri.to_str end |
#trailing_slash ⇒ Object
95 96 97 |
# File 'lib/uri/builder.rb', line 95 def trailing_slash wrap :path, Path.new(@uri.path).trailing_slash(true).to_s end |