Class: URL
- Inherits:
-
Struct
- Object
- Struct
- URL
- Defined in:
- lib/activesp/url.rb
Instance Attribute Summary collapse
-
#fragment ⇒ Object
Returns the value of attribute fragment.
-
#host ⇒ Object
Returns the value of attribute host.
-
#path ⇒ Object
Returns the value of attribute path.
-
#port ⇒ Object
Returns the value of attribute port.
-
#protocol ⇒ Object
Returns the value of attribute protocol.
-
#query ⇒ Object
Returns the value of attribute query.
Class Method Summary collapse
- .construct_query(hash) ⇒ Object
- .escape(s) ⇒ Object
- .parse(url) ⇒ Object
- .parse_query(qs, d = '&;') ⇒ Object
- .unescape(s) ⇒ Object
Instance Method Summary collapse
Instance Attribute Details
#fragment ⇒ Object
Returns the value of attribute fragment
44 45 46 |
# File 'lib/activesp/url.rb', line 44 def fragment @fragment end |
#host ⇒ Object
Returns the value of attribute host
44 45 46 |
# File 'lib/activesp/url.rb', line 44 def host @host end |
#path ⇒ Object
Returns the value of attribute path
44 45 46 |
# File 'lib/activesp/url.rb', line 44 def path @path end |
#port ⇒ Object
Returns the value of attribute port
44 45 46 |
# File 'lib/activesp/url.rb', line 44 def port @port end |
#protocol ⇒ Object
Returns the value of attribute protocol
44 45 46 |
# File 'lib/activesp/url.rb', line 44 def protocol @protocol end |
#query ⇒ Object
Returns the value of attribute query
44 45 46 |
# File 'lib/activesp/url.rb', line 44 def query @query end |
Class Method Details
.construct_query(hash) ⇒ Object
132 133 134 |
# File 'lib/activesp/url.rb', line 132 def self.construct_query(hash) hash.map { |k, v| "%s=%s" % [k, escape(v)] }.join('&') end |
.escape(s) ⇒ Object
109 110 111 112 113 |
# File 'lib/activesp/url.rb', line 109 def self.escape(s) s.to_s.gsub(/([^a-zA-Z0-9_.-]+)/n) do '%' + $1.unpack('H2' * $1.size).join('%').upcase end end |
.parse(url) ⇒ Object
46 47 48 49 50 51 52 |
# File 'lib/activesp/url.rb', line 46 def self.parse(url) if /^(?:([^:\/?#]+):)?(?:\/\/([^\/?#:]*)(?::(\d+))?)?([^?#]*)(?:\?([^#]*))?(?:#(.*))?$/ === url.strip new($1 ? $1.downcase : nil, $2 ? $2.downcase : nil, $3 ? $3.to_i : nil, $4, $5, $6) else nil end end |
.parse_query(qs, d = '&;') ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/activesp/url.rb', line 115 def self.parse_query(qs, d = '&;') params = {} (qs || '').split(/[&;] */n).inject(params) do |h, p| k, v = unescape(p).split('=', 2) if cur = params[k] if Array === cur params[k] << v else params[k] = [cur, v] end else params[k] = v end end params end |
.unescape(s) ⇒ Object
103 104 105 106 107 |
# File 'lib/activesp/url.rb', line 103 def self.unescape(s) s.gsub(/((?:%[0-9a-fA-F]{2})+)/n) do [$1.delete('%')].pack('H*') end end |
Instance Method Details
#authority ⇒ Object
58 59 60 |
# File 'lib/activesp/url.rb', line 58 def "%s%s" % [host, (!port || port == (protocol == "http" ? 80 : 443)) ? "" : ":#{port}"] end |
#complete ⇒ Object
96 97 98 99 100 101 |
# File 'lib/activesp/url.rb', line 96 def complete self.protocol ||= "http" self.port ||= self.protocol == "http" ? 80 : 443 self.path = "/" if self.path.empty? self end |
#full_path ⇒ Object
62 63 64 65 66 67 |
# File 'lib/activesp/url.rb', line 62 def full_path result = path.dup result << "?" << query if query result << "#" << fragment if fragment result end |
#join(url) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/activesp/url.rb', line 69 def join(url) url = URL(url) if url if url.protocol == protocol url.protocol = nil end unless url.protocol url.protocol = protocol unless url.host url.host = host url.port = port if url.path.empty? url.path = path unless url.query url.query = query end else url.path = join_url_paths(url.path, path) end end end url.complete else nil end end |
#to_s ⇒ Object
54 55 56 |
# File 'lib/activesp/url.rb', line 54 def to_s "%s://%s%s" % [protocol, , full_path] end |