Module: Protocol::URL

Defined in:
lib/protocol/url/version.rb,
lib/protocol/url.rb,
lib/protocol/url/path.rb,
lib/protocol/url/pattern.rb,
lib/protocol/url/absolute.rb,
lib/protocol/url/encoding.rb,
lib/protocol/url/relative.rb,
lib/protocol/url/reference.rb

Defined Under Namespace

Modules: Encoding, Path Classes: Absolute, Reference, Relative

Constant Summary collapse

PATTERN =

RFC 3986 URI pattern with named capture groups. Matches: [scheme:][//authority][path][?query][#fragment] Rejects strings containing whitespace or control characters (matching standard URI behavior).

%r{
	\A
	(?:(?<scheme>[a-z][a-z0-9+.-]*):)?      # scheme (optional)
	(?://(?<authority>[^/?#\s]*))?          # authority (optional, without //, no whitespace)
	(?<path>[^?#\s]*)                       # path (no whitespace)
	(?:\?(?<query>[^#\s]*))?                # query (optional, no whitespace)
	(?:\#(?<fragment>[^\s]*))?              # fragment (optional, no whitespace)
	\z
}ix
VERSION =
"0.5.0"

Class Method Summary collapse

Class Method Details

.[](value) ⇒ Object

Coerce a value into an appropriate URL type (Absolute or Relative).



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/protocol/url.rb', line 19

def self.[](value)
	case value
	when String
		if match = value.match(PATTERN)
			scheme = match[:scheme]
			authority = match[:authority]
			path = match[:path]
			query = match[:query]
			fragment = match[:fragment]
			
			# If we have a scheme or authority, it's an absolute URL
			if scheme || authority
				Absolute.new(scheme, authority, path, query, fragment)
			else
				# No scheme or authority, treat as relative:
				Relative.new(path, query, fragment)
			end
		else
			raise ArgumentError, "Invalid URL (contains whitespace or control characters): #{value.inspect}"
		end
	when Relative
		value
	when nil
		nil
	else
		raise ArgumentError, "Cannot coerce #{value.inspect} to URL!"
	end
end