Class: Protocol::HTTP::Reference
- Inherits:
-
Object
- Object
- Protocol::HTTP::Reference
- 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
-
#fragment ⇒ Object
A fragment, the part after the ‘#’.
-
#parameters ⇒ Object
User supplied parameters that will be appended to the query part.
-
#path ⇒ Object
The path component, e.g.
-
#query ⇒ Object
The un-parsed query string, e.g.
Class Method Summary collapse
- .[](reference) ⇒ Object
-
.parse(path = '/', parameters = nil) ⇒ Object
Generate a reference from a path and user parameters.
Instance Method Summary collapse
-
#+(other) ⇒ Object
Merges two references as specified by RFC2396, similar to ‘URI.join`.
- #<=>(other) ⇒ Object
- #append(buffer) ⇒ Object
-
#base ⇒ Object
Just the base path, without any query string, parameters or fragment.
- #fragment? ⇒ Boolean
- #freeze ⇒ Object
-
#initialize(path = '/', query = nil, fragment = nil, parameters = nil) ⇒ Reference
constructor
A new instance of Reference.
- #parameters? ⇒ Boolean
- #query? ⇒ Boolean
- #to_ary ⇒ Object
- #to_s ⇒ Object
-
#with(path: nil, parameters: nil, fragment: @fragment, pop: false, merge: true) ⇒ Object
Update the reference with the given path, parameters and fragment.
Constructor Details
#initialize(path = '/', query = nil, fragment = nil, parameters = nil) ⇒ Reference
Returns a new instance of Reference.
22 23 24 25 26 27 |
# File 'lib/protocol/http/reference.rb', line 22 def initialize(path = '/', query = nil, fragment = nil, parameters = nil) @path = path @query = query @fragment = fragment @parameters = parameters end |
Instance Attribute Details
#fragment ⇒ Object
A fragment, the part after the ‘#’
36 37 38 |
# File 'lib/protocol/http/reference.rb', line 36 def fragment @fragment end |
#parameters ⇒ Object
User supplied parameters that will be appended to the query part.
39 40 41 |
# File 'lib/protocol/http/reference.rb', line 39 def parameters @parameters end |
#path ⇒ Object
The path component, e.g. /foo/bar/index.html
30 31 32 |
# File 'lib/protocol/http/reference.rb', line 30 def path @path end |
#query ⇒ Object
The un-parsed query string, e.g. ‘x=10&y=20’
33 34 35 |
# File 'lib/protocol/http/reference.rb', line 33 def query @query end |
Class Method Details
.[](reference) ⇒ Object
60 61 62 63 64 65 66 |
# File 'lib/protocol/http/reference.rb', line 60 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`.
101 102 103 104 105 106 107 108 109 110 |
# File 'lib/protocol/http/reference.rb', line 101 def + other other = self.class[other] self.class.new( (self.path, other.path, true), other.query, other.fragment, other.parameters, ) end |
#<=>(other) ⇒ Object
56 57 58 |
# File 'lib/protocol/http/reference.rb', line 56 def <=> other to_ary <=> other.to_ary end |
#append(buffer) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/protocol/http/reference.rb', line 80 def append(buffer) 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.
113 114 115 |
# File 'lib/protocol/http/reference.rb', line 113 def base self.class.new(@path, nil, nil, nil) end |
#fragment? ⇒ Boolean
76 77 78 |
# File 'lib/protocol/http/reference.rb', line 76 def fragment? @fragment and !@fragment.empty? end |
#freeze ⇒ Object
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/protocol/http/reference.rb', line 41 def freeze return self if frozen? @path.freeze @query.freeze @fragment.freeze @parameters.freeze super end |
#parameters? ⇒ Boolean
68 69 70 |
# File 'lib/protocol/http/reference.rb', line 68 def parameters? @parameters and !@parameters.empty? end |
#query? ⇒ Boolean
72 73 74 |
# File 'lib/protocol/http/reference.rb', line 72 def query? @query and !@query.empty? end |
#to_ary ⇒ Object
52 53 54 |
# File 'lib/protocol/http/reference.rb', line 52 def to_ary [@path, @query, @fragment, @parameters] end |
#to_s ⇒ Object
96 97 98 |
# File 'lib/protocol/http/reference.rb', line 96 def to_s append(String.new) end |
#with(path: nil, parameters: nil, fragment: @fragment, pop: false, merge: true) ⇒ Object
Update the reference with the given path, parameters and fragment.
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/protocol/http/reference.rb', line 123 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 = (@path, path, pop) else path = @path end self.class.new(path, query, fragment, parameters) end |