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) ⇒ 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.
25 26 27 28 29 30 |
# File 'lib/http/protocol/reference.rb', line 25 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 ‘#’
55 56 57 |
# File 'lib/http/protocol/reference.rb', line 55 def fragment @fragment end |
#parameters ⇒ Object (readonly)
User supplied parameters that will be appended to the query part.
58 59 60 |
# File 'lib/http/protocol/reference.rb', line 58 def parameters @parameters end |
#path ⇒ Object (readonly)
The path component, e.g. /foo/bar/index.html
49 50 51 |
# File 'lib/http/protocol/reference.rb', line 49 def path @path end |
#query_string ⇒ Object (readonly)
The un-parsed query string, e.g. ‘x=10&y=20’
52 53 54 |
# File 'lib/http/protocol/reference.rb', line 52 def query_string @query_string end |
Class Method Details
.[](reference) ⇒ Object
32 33 34 35 36 37 38 |
# File 'lib/http/protocol/reference.rb', line 32 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`.
41 42 43 44 45 46 |
# File 'lib/http/protocol/reference.rb', line 41 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
94 95 96 97 98 99 100 101 102 103 |
# File 'lib/http/protocol/reference.rb', line 94 def + other other = self.class[other] self.class.new( (self.path, other.path), other.query_string, other.fragment, other.parameters, ) end |
#[](parameters) ⇒ Object
105 106 107 |
# File 'lib/http/protocol/reference.rb', line 105 def [] parameters self.dup(nil, parameters) end |
#append(buffer) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/http/protocol/reference.rb', line 72 def append(buffer) if query_string? buffer << escape_path(@path) << '?' << @query_string buffer << '&' << encode(@parameters) if parameters? else buffer << escape_path(@path) buffer << '?' << encode(@parameters) if parameters? end if fragment? buffer << '#' << escape(@fragment) end return buffer end |
#dup(path = nil, parameters = nil) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/http/protocol/reference.rb', line 109 def dup(path = nil, parameters = nil) if @parameters 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
68 69 70 |
# File 'lib/http/protocol/reference.rb', line 68 def fragment? @fragment and !@fragment.empty? end |
#parameters? ⇒ Boolean
60 61 62 |
# File 'lib/http/protocol/reference.rb', line 60 def parameters? @parameters and !@parameters.empty? end |
#query_string? ⇒ Boolean
64 65 66 |
# File 'lib/http/protocol/reference.rb', line 64 def query_string? @query_string and !@query_string.empty? end |
#to_str ⇒ Object Also known as: to_s
88 89 90 |
# File 'lib/http/protocol/reference.rb', line 88 def to_str append(String.new) end |