Class: HTTP::Protocol::Reference
- Inherits:
-
Object
- Object
- HTTP::Protocol::Reference
- Defined in:
- lib/http/protocol/reference.rb
Overview
A relative reference, excluding any authority.
Instance Attribute Summary collapse
-
#fragment ⇒ Object
readonly
A fragment, the part after the ‘#’.
-
#parameters ⇒ Object
readonly
User supplied parameters that will be appended to the query part.
-
#path ⇒ Object
readonly
The path component, e.g.
-
#query_string ⇒ Object
readonly
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
- #[](parameters) ⇒ Object
- #append(buffer) ⇒ Object
- #dup(path = nil, parameters = nil, merge = true) ⇒ Object
- #fragment? ⇒ Boolean
-
#initialize(path, query_string, fragment, parameters) ⇒ Reference
constructor
A new instance of Reference.
- #parameters? ⇒ Boolean
- #query_string? ⇒ Boolean
- #to_str ⇒ Object (also: #to_s)
Constructor Details
#initialize(path, query_string, fragment, parameters) ⇒ Reference
Returns a new instance of Reference.
27 28 29 30 31 32 |
# File 'lib/http/protocol/reference.rb', line 27 def initialize(path, query_string, fragment, parameters) @path = path @query_string = query_string @fragment = fragment @parameters = parameters end |
Instance Attribute Details
#fragment ⇒ Object (readonly)
A fragment, the part after the ‘#’
57 58 59 |
# File 'lib/http/protocol/reference.rb', line 57 def fragment @fragment end |
#parameters ⇒ Object (readonly)
User supplied parameters that will be appended to the query part.
60 61 62 |
# File 'lib/http/protocol/reference.rb', line 60 def parameters @parameters end |
#path ⇒ Object (readonly)
The path component, e.g. /foo/bar/index.html
51 52 53 |
# File 'lib/http/protocol/reference.rb', line 51 def path @path end |
#query_string ⇒ Object (readonly)
The un-parsed query string, e.g. ‘x=10&y=20’
54 55 56 |
# File 'lib/http/protocol/reference.rb', line 54 def query_string @query_string end |
Class Method Details
.[](reference) ⇒ Object
34 35 36 37 38 39 40 |
# File 'lib/http/protocol/reference.rb', line 34 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`.
43 44 45 46 47 48 |
# File 'lib/http/protocol/reference.rb', line 43 def self.parse(path = '/', parameters = nil) base, fragment = path.split('#', 2) path, query_string = base.split('?', 2) self.new(path, query_string, fragment, parameters) end |
Instance Method Details
#+(other) ⇒ Object
96 97 98 99 100 101 102 103 104 105 |
# File 'lib/http/protocol/reference.rb', line 96 def + other other = self.class[other] self.class.new( (self.path, other.path), other.query_string, other.fragment, other.parameters, ) end |
#[](parameters) ⇒ Object
107 108 109 |
# File 'lib/http/protocol/reference.rb', line 107 def [] parameters self.dup(nil, parameters) end |
#append(buffer) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/http/protocol/reference.rb', line 74 def append(buffer) if query_string? buffer << URL.escape_path(@path) << '?' << @query_string 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 |
#dup(path = nil, parameters = nil, merge = true) ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/http/protocol/reference.rb', line 111 def dup(path = nil, parameters = nil, merge = true) if @parameters and merge if parameters parameters = @parameters.merge(parameters) else parameters = @parameters end end if path path = (@path, path) else path = @path end self.class.new(path, @query_string, @fragment, parameters) end |
#fragment? ⇒ Boolean
70 71 72 |
# File 'lib/http/protocol/reference.rb', line 70 def fragment? @fragment and !@fragment.empty? end |
#parameters? ⇒ Boolean
62 63 64 |
# File 'lib/http/protocol/reference.rb', line 62 def parameters? @parameters and !@parameters.empty? end |
#query_string? ⇒ Boolean
66 67 68 |
# File 'lib/http/protocol/reference.rb', line 66 def query_string? @query_string and !@query_string.empty? end |
#to_str ⇒ Object Also known as: to_s
90 91 92 |
# File 'lib/http/protocol/reference.rb', line 90 def to_str append(String.new) end |