Class: Protocol::HTTP::Reference

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/protocol/http/reference.rb

Overview

A relative reference, excluding any authority. The path part of an HTTP request.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = "/", query = nil, fragment = nil, parameters = nil) ⇒ Reference

Initialize the reference.



28
29
30
31
32
33
# File 'lib/protocol/http/reference.rb', line 28

def initialize(path = "/", query = nil, fragment = nil, parameters = nil)
	@path = path
	@query = query
	@fragment = fragment
	@parameters = parameters
end

Instance Attribute Details

#fragment ⇒ Object

Returns the value of attribute fragment.



42
43
44
# File 'lib/protocol/http/reference.rb', line 42

def fragment
  @fragment
end

#parameters ⇒ Object

Returns the value of attribute parameters.



45
46
47
# File 'lib/protocol/http/reference.rb', line 45

def parameters
  @parameters
end

#path ⇒ Object

Returns the value of attribute path.



36
37
38
# File 'lib/protocol/http/reference.rb', line 36

def path
  @path
end

#query ⇒ Object

Returns the value of attribute query.



39
40
41
# File 'lib/protocol/http/reference.rb', line 39

def query
  @query
end

#The fragment, the part after the '#'.(fragment, thepartafterthe'#'.) ⇒ Object (readonly)



42
# File 'lib/protocol/http/reference.rb', line 42

attr_accessor :fragment

#The path component, e.g. `/foo/bar/index.html`.(pathcomponent, e.g.`/foo/bar/index.html`.) ⇒ Object (readonly)



36
# File 'lib/protocol/http/reference.rb', line 36

attr_accessor :path

#The un-parsed query string, e.g. 'x=10&y=20'.(un-parsedquerystring, e.g.'x = 10&y=20'.) ⇒ Object (readonly)



39
# File 'lib/protocol/http/reference.rb', line 39

attr_accessor :query

Class Method Details

.[](reference) ⇒ Object

Type-cast a reference.



80
81
82
83
84
85
86
# File 'lib/protocol/http/reference.rb', line 80

def self.[] reference
	if reference.is_a? self
		return reference
	else
		return self.parse(reference)
	end
end

.parse(path = "/", parameters = nil) ⇒ Object

Generate a reference from a path and user parameters. The path may contain a #fragment or ?query=parameters.



15
16
17
18
19
20
# File 'lib/protocol/http/reference.rb', line 15

def self.parse(path = "/", parameters = nil)
	base, fragment = path.split("#", 2)
	path, query = base.split("?", 2)
	
	self.new(path, query, fragment, parameters)
end

Instance Method Details

#+(other) ⇒ Object

Merges two references as specified by RFC2396, similar to URI.join.



128
129
130
131
132
133
134
135
136
137
# File 'lib/protocol/http/reference.rb', line 128

def + other
	other = self.class[other]
	
	self.class.new(
		expand_path(self.path, other.path, true),
		other.query,
		other.fragment,
		other.parameters,
	)
end

#<=>(other) ⇒ Object

Compare two references.



72
73
74
# File 'lib/protocol/http/reference.rb', line 72

def <=> other
	to_ary <=> other.to_ary
end

#append(buffer = String.new) ⇒ Object

Append the reference to the given buffer.



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/protocol/http/reference.rb', line 104

def append(buffer = String.new)
	if query?
		buffer << URL.escape_path(@path) << "?" << @query
		buffer << "&" << URL.encode(@parameters) if parameters?
	else
		buffer << URL.escape_path(@path)
		buffer << "?" << URL.encode(@parameters) if parameters?
	end
	
	if fragment?
		buffer << "#" << URL.escape(@fragment)
	end
	
	return buffer
end

#base ⇒ Object

Just the base path, without any query string, parameters or fragment.



140
141
142
# File 'lib/protocol/http/reference.rb', line 140

def base
	self.class.new(@path, nil, nil, nil)
end

#fragment? ⇒ Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/protocol/http/reference.rb', line 99

def fragment?
	@fragment and !@fragment.empty?
end

#freeze ⇒ Object

Freeze the reference.



50
51
52
53
54
55
56
57
58
59
# File 'lib/protocol/http/reference.rb', line 50

def freeze
	return self if frozen?
	
	@path.freeze
	@query.freeze
	@fragment.freeze
	@parameters.freeze
	
	super
end

#parameters? ⇒ Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/protocol/http/reference.rb', line 89

def parameters?
	@parameters and !@parameters.empty?
end

#query? ⇒ Boolean

Returns:

  • (Boolean)


94
95
96
# File 'lib/protocol/http/reference.rb', line 94

def query?
	@query and !@query.empty?
end

#to_ary ⇒ Object

Implicit conversion to an array.



64
65
66
# File 'lib/protocol/http/reference.rb', line 64

def to_ary
	[@path, @query, @fragment, @parameters]
end

#to_s ⇒ Object

Convert the reference to a string, e.g. /foo/bar/index.html?x=10&y=20#section



123
124
125
# File 'lib/protocol/http/reference.rb', line 123

def to_s
	append
end

#User supplied parameters that will be appended to the query part.=(suppliedparametersthatwillbeappendedtothequerypart. = (value)) ⇒ Object



45
# File 'lib/protocol/http/reference.rb', line 45

attr_accessor :parameters

#with(path: nil, parameters: nil, fragment: @fragment, pop: false, merge: true) ⇒ Object

Update the reference with the given path, parameters and fragment.



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/protocol/http/reference.rb', line 150

def with(path: nil, parameters: nil, fragment: @fragment, pop: false, merge: true)
	if @parameters
		if parameters and merge
			parameters = @parameters.merge(parameters)
		else
			parameters = @parameters
		end
	end
	
	if @query and !merge
		query = nil
	else
		query = @query
	end
	
	if path
		path = expand_path(@path, path, pop)
	else
		path = @path
	end
	
	self.class.new(path, query, fragment, parameters)
end