Class: Url
Constant Summary collapse
- SECURE_SCHEMES =
['https', 'sftp', 'ssh'].freeze
- SECURE_SCHEME_MAPPING =
{ 'http' => 'https', 'ftp' => 'sftp' }.freeze
- SCHEME_DEFAULT_PORTS =
{ 'ftp' => 21, 'ssh' => 22, 'smtp' => 25, 'http' => 80, 'pop3' => 110, 'pop' => 110, 'sftp' => 115, 'imap' => 143, 'https' => 443, 'ssl' => 443, 'irc' => 531, 'imaps' => 993, 'pop3s' => 995, 'pops' => 995 }.freeze
Instance Attribute Summary collapse
-
#fragment ⇒ Object
Returns the value of attribute fragment.
-
#params ⇒ Object
Returns the value of attribute params.
-
#path ⇒ Object
Returns the value of attribute path.
-
#port ⇒ Object
Returns the value of attribute port.
-
#scheme ⇒ Object
Returns the value of attribute scheme.
-
#server ⇒ Object
Returns the value of attribute server.
Class Method Summary collapse
-
.build(base, params = {}, fragment = nil) ⇒ Object
Construct a full URL from pieces.
- .default_scheme ⇒ Object
- .default_scheme=(scheme) ⇒ Object
-
.default_server ⇒ Object
Return default domain for urls, used to convert relative urls to absolute.
- .default_server=(server) ⇒ Object
-
.parse(str) ⇒ Object
Returns a new Url from the given string.
-
.to_param_string(params) ⇒ Object
Construct a param string from key/value pairs.
Instance Method Summary collapse
- #+(str) ⇒ Object
- #absolute? ⇒ Boolean
-
#add_param(k, v) ⇒ Object
Add a param value (can be called multiply for the same param key).
- #append_path(str, escape = false) ⇒ Object
-
#base ⇒ Object
Returns the full start of the url, minus params and fragment.
- #blank? ⇒ Boolean
-
#clear_params ⇒ Object
Reset params.
- #empty? ⇒ Boolean
- #get_param(k) ⇒ Object
- #has_params? ⇒ Boolean
-
#initialize(str = nil) ⇒ Url
constructor
A new instance of Url.
- #inspect ⇒ Object
-
#make_absolute(secure = false, server = nil) ⇒ Object
Ensure url contains a server + scheme section, eg converts ‘/foo’ into ‘example.com/foo’.
- #make_relative ⇒ Object
- #relative? ⇒ Boolean
-
#remove_param(key_or_regex) ⇒ Object
Wipe out keyed value by string match or regex match.
- #reset_params ⇒ Object
- #secure? ⇒ Boolean
-
#set(url) ⇒ Object
Parse and set internals from given url.
-
#set_param(k, v) ⇒ Object
Override current param val or set if none.
- #set_params(hash) ⇒ Object
- #to_html ⇒ Object
-
#to_s ⇒ Object
Makee the url.
Constructor Details
#initialize(str = nil) ⇒ Url
Returns a new instance of Url.
82 83 84 85 |
# File 'lib/iron/web/url.rb', line 82 def initialize(str = nil) @scheme = @port = @server = nil set(str) end |
Instance Attribute Details
#fragment ⇒ Object
Returns the value of attribute fragment.
5 6 7 |
# File 'lib/iron/web/url.rb', line 5 def fragment @fragment end |
#params ⇒ Object
Returns the value of attribute params.
5 6 7 |
# File 'lib/iron/web/url.rb', line 5 def params @params end |
#path ⇒ Object
Returns the value of attribute path.
5 6 7 |
# File 'lib/iron/web/url.rb', line 5 def path @path end |
#port ⇒ Object
Returns the value of attribute port.
5 6 7 |
# File 'lib/iron/web/url.rb', line 5 def port @port end |
#scheme ⇒ Object
Returns the value of attribute scheme.
5 6 7 |
# File 'lib/iron/web/url.rb', line 5 def scheme @scheme end |
#server ⇒ Object
Returns the value of attribute server.
5 6 7 |
# File 'lib/iron/web/url.rb', line 5 def server @server end |
Class Method Details
.build(base, params = {}, fragment = nil) ⇒ Object
Construct a full URL from pieces
50 51 52 53 54 |
# File 'lib/iron/web/url.rb', line 50 def self.build(base, params = {}, fragment = nil) url = base + to_param_string(params) url += '#' + fragment unless fragment.blank? url end |
.default_scheme ⇒ Object
41 42 43 |
# File 'lib/iron/web/url.rb', line 41 def self.default_scheme @default_scheme || 'http' end |
.default_scheme=(scheme) ⇒ Object
45 46 47 |
# File 'lib/iron/web/url.rb', line 45 def self.default_scheme=(scheme) @default_scheme = scheme end |
.default_server ⇒ Object
Return default domain for urls, used to convert relative urls to absolute
33 34 35 |
# File 'lib/iron/web/url.rb', line 33 def self.default_server @default_server end |
.default_server=(server) ⇒ Object
37 38 39 |
# File 'lib/iron/web/url.rb', line 37 def self.default_server=(server) @default_server = server end |
.parse(str) ⇒ Object
Returns a new Url from the given string
27 28 29 |
# File 'lib/iron/web/url.rb', line 27 def self.parse(str) Url.new(str) end |
.to_param_string(params) ⇒ Object
Construct a param string from key/value pairs
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/iron/web/url.rb', line 57 def self.to_param_string(params) str = '' # Remove blank/nil keys params.delete_if {|k,v| v.to_s.blank? || k.to_s.blank?} # Convert to param string unless params.empty? str += '?' str += params.collect do |k,v| if v.is_a?(Array) k = k.gsub('[]','') v.collect do |vs| val = vs.respond_to?(:to_param) ? vs.to_param : vs val = val.to_s CGI::escape(k.to_s) + '[]=' + CGI::escape(val) end else val = v.respond_to?(:to_param) ? v.to_param : v val = val.to_s CGI::escape(k.to_s) + '=' + CGI::escape(val) end end.flatten.join('&') end str end |
Instance Method Details
#+(str) ⇒ Object
156 157 158 159 |
# File 'lib/iron/web/url.rb', line 156 def +(str) append_path(str) self end |
#absolute? ⇒ Boolean
217 218 219 |
# File 'lib/iron/web/url.rb', line 217 def absolute? !relative? end |
#add_param(k, v) ⇒ Object
Add a param value (can be called multiply for the same param key)
173 174 175 176 177 178 179 180 181 182 |
# File 'lib/iron/web/url.rb', line 173 def add_param(k, v) k = k.to_s oldval = @params[k] if oldval @params[k] = oldval.is_a?(Array) ? oldval + [v] : [oldval, v] @params[k].flatten! else @params[k] = v end end |
#append_path(str, escape = false) ⇒ Object
150 151 152 153 154 |
# File 'lib/iron/web/url.rb', line 150 def append_path(str, escape = false) str = str.to_s @path ||= '' @path += escape ? CGI::escape(str).gsub('+', '%20') : str end |
#base ⇒ Object
Returns the full start of the url, minus params and fragment
139 140 141 142 143 144 145 146 147 148 |
# File 'lib/iron/web/url.rb', line 139 def base val = '' unless @server.blank? val = (@scheme || Url::default_scheme) + '://' + @server val += ':' + @port.to_s unless @port.to_s.blank? end p = (@path || '') p = '/' + p unless p.blank? || p.starts_with?('/') val + p end |
#blank? ⇒ Boolean
134 135 136 |
# File 'lib/iron/web/url.rb', line 134 def blank? self.base.blank? end |
#clear_params ⇒ Object
Reset params
200 201 202 |
# File 'lib/iron/web/url.rb', line 200 def clear_params @params = {} end |
#empty? ⇒ Boolean
130 131 132 |
# File 'lib/iron/web/url.rb', line 130 def empty? blank? end |
#get_param(k) ⇒ Object
195 196 197 |
# File 'lib/iron/web/url.rb', line 195 def get_param(k) @params[k.to_s] end |
#has_params? ⇒ Boolean
205 206 207 |
# File 'lib/iron/web/url.rb', line 205 def has_params? @params && !@params.empty? end |
#inspect ⇒ Object
126 127 128 |
# File 'lib/iron/web/url.rb', line 126 def inspect to_s end |
#make_absolute(secure = false, server = nil) ⇒ Object
Ensure url contains a server + scheme section, eg converts ‘/foo’ into ‘example.com/foo’.
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
# File 'lib/iron/web/url.rb', line 222 def make_absolute(secure = false, server = nil) unless absolute? && secure? == secure raise 'No default server set for Url#make_absolute' unless server || @server || Url.default_server @server ||= (server || Url.default_server) unless @scheme @scheme = Url.default_scheme @port = nil end if secure raise "No secure scheme for scheme #{@scheme} in Url#make_absolute" unless SECURE_SCHEME_MAPPING[@scheme] @scheme = SECURE_SCHEME_MAPPING[@scheme] end end to_s end |
#make_relative ⇒ Object
238 239 240 241 242 243 244 245 |
# File 'lib/iron/web/url.rb', line 238 def make_relative unless relative? @server = nil @scheme = nil @port = nil end to_s end |
#relative? ⇒ Boolean
213 214 215 |
# File 'lib/iron/web/url.rb', line 213 def relative? @server.blank? end |
#remove_param(key_or_regex) ⇒ Object
Wipe out keyed value by string match or regex match
185 186 187 188 189 190 191 192 193 |
# File 'lib/iron/web/url.rb', line 185 def remove_param(key_or_regex) @params.delete_if do |k, v| if key_or_regex.is_a?(Regexp) k.match(key_or_regex) else k == key_or_regex.to_s end end end |
#reset_params ⇒ Object
203 |
# File 'lib/iron/web/url.rb', line 203 def reset_params ; clear_params ; end |
#secure? ⇒ Boolean
209 210 211 |
# File 'lib/iron/web/url.rb', line 209 def secure? SECURE_SCHEMES.include?(scheme) end |
#set(url) ⇒ Object
Parse and set internals from given url
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/iron/web/url.rb', line 88 def set(url) # Decompose into major components url = (url || '').strip base, params, @fragment = url.extract(/^([^\?#]*)\??([^#]*)#?(.*)$/) # Parse out base base ||= '' if base.match(/^[a-z\+]*:\/\//) @scheme, @server, ignore, @port, @path = base.extract(/^([a-z]*):\/\/([a-z0-9\-_\.]+)(:([0-9]+))?(\/.*)?/i) @path ||= '' @port = @port.blank? ? nil : @port.to_i else @path = base end # Parse out params @params = {} params.split('&').each do |p| k, v = p.split('=') if k && v if k.ends_with?('[]') add_param(CGI::unescape(k.gsub('[]','')), [CGI::unescape(v)]) else add_param(CGI::unescape(k), CGI::unescape(v)) end end end end |
#set_param(k, v) ⇒ Object
Override current param val or set if none
162 163 164 |
# File 'lib/iron/web/url.rb', line 162 def set_param(k, v) @params[k.to_s] = v end |
#set_params(hash) ⇒ Object
166 167 168 169 170 |
# File 'lib/iron/web/url.rb', line 166 def set_params(hash) hash.each_pair {|k,v| set_param(k, v) } end |
#to_html ⇒ Object
122 123 124 |
# File 'lib/iron/web/url.rb', line 122 def to_html to_s end |