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

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

#fragmentObject

A fragment, the part after the ‘#’



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

def fragment
  @fragment
end

#parametersObject

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

#pathObject

The path component, e.g. /foo/bar/index.html



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

def path
  @path
end

#queryObject

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(
    expand_path(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

#baseObject

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

Returns:

  • (Boolean)


76
77
78
# File 'lib/protocol/http/reference.rb', line 76

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

#freezeObject



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

Returns:

  • (Boolean)


68
69
70
# File 'lib/protocol/http/reference.rb', line 68

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

#query?Boolean

Returns:

  • (Boolean)


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

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

#to_aryObject



52
53
54
# File 'lib/protocol/http/reference.rb', line 52

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

#to_sObject



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 = expand_path(@path, path, pop)
  else
    path = @path
  end
  
  self.class.new(path, query, fragment, parameters)
end