Class: Protocol::HTTP::Reference

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

Overview

A relative reference, excluding any authority.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, query_string = nil, fragment = nil, parameters = nil) ⇒ Reference

Returns a new instance of Reference.



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

def initialize(path, query_string = nil, fragment = nil, parameters = nil)
  @path = path
  @query_string = query_string
  @fragment = fragment
  @parameters = parameters
end

Instance Attribute Details

#fragmentObject (readonly)

A fragment, the part after the ‘#’



57
58
59
# File 'lib/protocol/http/reference.rb', line 57

def fragment
  @fragment
end

#parametersObject (readonly)

User supplied parameters that will be appended to the query part.



60
61
62
# File 'lib/protocol/http/reference.rb', line 60

def parameters
  @parameters
end

#pathObject (readonly)

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



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

def path
  @path
end

#query_stringObject (readonly)

The un-parsed query string, e.g. ‘x=10&y=20’



54
55
56
# File 'lib/protocol/http/reference.rb', line 54

def query_string
  @query_string
end

Class Method Details

.[](reference) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/protocol/http/reference.rb', line 42

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`.



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

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/protocol/http/reference.rb', line 96

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

#[](parameters) ⇒ Object



107
108
109
# File 'lib/protocol/http/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/protocol/http/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/protocol/http/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 = expand_path(@path, path)
  else
    path = @path
  end
  
  self.class.new(path, @query_string, @fragment, parameters)
end

#fragment?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/protocol/http/reference.rb', line 70

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

#parameters?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/protocol/http/reference.rb', line 62

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

#query_string?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/protocol/http/reference.rb', line 66

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

#to_strObject Also known as: to_s



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

def to_str
  append(String.new)
end