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.
83 84 85 86 |
# File 'lib/iron/web/url.rb', line 83 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 81 |
# 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| k = k.to_s 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) + '[]=' + CGI::escape(val) end else val = v.respond_to?(:to_param) ? v.to_param : v val = val.to_s CGI::escape(k) + '=' + CGI::escape(val) end end.flatten.join('&') end str end |
Instance Method Details
#+(str) ⇒ Object
157 158 159 160 |
# File 'lib/iron/web/url.rb', line 157 def +(str) append_path(str) self end |
#absolute? ⇒ Boolean
218 219 220 |
# File 'lib/iron/web/url.rb', line 218 def absolute? !relative? end |
#add_param(k, v) ⇒ Object
Add a param value (can be called multiply for the same param key)
174 175 176 177 178 179 180 181 182 183 |
# File 'lib/iron/web/url.rb', line 174 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
151 152 153 154 155 |
# File 'lib/iron/web/url.rb', line 151 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
140 141 142 143 144 145 146 147 148 149 |
# File 'lib/iron/web/url.rb', line 140 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
135 136 137 |
# File 'lib/iron/web/url.rb', line 135 def blank? self.base.blank? end |
#clear_params ⇒ Object
Reset params
201 202 203 |
# File 'lib/iron/web/url.rb', line 201 def clear_params @params = {} end |
#empty? ⇒ Boolean
131 132 133 |
# File 'lib/iron/web/url.rb', line 131 def empty? blank? end |
#get_param(k) ⇒ Object
196 197 198 |
# File 'lib/iron/web/url.rb', line 196 def get_param(k) @params[k.to_s] end |
#has_params? ⇒ Boolean
206 207 208 |
# File 'lib/iron/web/url.rb', line 206 def has_params? @params && !@params.empty? end |
#inspect ⇒ Object
127 128 129 |
# File 'lib/iron/web/url.rb', line 127 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’.
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/iron/web/url.rb', line 223 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
239 240 241 242 243 244 245 246 |
# File 'lib/iron/web/url.rb', line 239 def make_relative unless relative? @server = nil @scheme = nil @port = nil end to_s end |
#relative? ⇒ Boolean
214 215 216 |
# File 'lib/iron/web/url.rb', line 214 def relative? @server.blank? end |
#remove_param(key_or_regex) ⇒ Object
Wipe out keyed value by string match or regex match
186 187 188 189 190 191 192 193 194 |
# File 'lib/iron/web/url.rb', line 186 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
204 |
# File 'lib/iron/web/url.rb', line 204 def reset_params ; clear_params ; end |
#secure? ⇒ Boolean
210 211 212 |
# File 'lib/iron/web/url.rb', line 210 def secure? SECURE_SCHEMES.include?(scheme) end |
#set(url) ⇒ Object
Parse and set internals from given url
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 116 |
# File 'lib/iron/web/url.rb', line 89 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
163 164 165 |
# File 'lib/iron/web/url.rb', line 163 def set_param(k, v) @params[k.to_s] = v end |
#set_params(hash) ⇒ Object
167 168 169 170 171 |
# File 'lib/iron/web/url.rb', line 167 def set_params(hash) hash.each_pair {|k,v| set_param(k, v) } end |
#to_html ⇒ Object
123 124 125 |
# File 'lib/iron/web/url.rb', line 123 def to_html to_s end |