Class: URI::Builder::DSL

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#uriObject (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_fragmentObject



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

def clear_fragment
  wrap :fragment, nil
end

#clear_pathObject Also known as: root



90
91
92
# File 'lib/uri/builder.rb', line 90

def clear_path
  path "/"
end

#clear_queryObject



81
82
83
# File 'lib/uri/builder.rb', line 81

def clear_query
  wrap :query, nil
end

#clear_trailing_slashObject



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

#joinObject



103
104
105
# File 'lib/uri/builder.rb', line 103

def join(...)
  wrap :path, Path.new(@uri.path).join(...).to_s
end

#parentObject



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_sObject



111
112
113
# File 'lib/uri/builder.rb', line 111

def to_s
  uri.to_s
end

#to_strObject



115
116
117
# File 'lib/uri/builder.rb', line 115

def to_str
  uri.to_str
end

#trailing_slashObject



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