Class: Async::HTTP::Reference

Inherits:
Object
  • Object
show all
Defined in:
lib/async/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, fragment, parameters) ⇒ Reference

Returns a new instance of Reference.



25
26
27
28
29
30
# File 'lib/async/http/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

#fragmentObject (readonly)

A fragment, the part after the ‘#’



55
56
57
# File 'lib/async/http/reference.rb', line 55

def fragment
  @fragment
end

#parametersObject (readonly)

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



58
59
60
# File 'lib/async/http/reference.rb', line 58

def parameters
  @parameters
end

#pathObject (readonly)

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



49
50
51
# File 'lib/async/http/reference.rb', line 49

def path
  @path
end

#query_stringObject (readonly)

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



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

def query_string
  @query_string
end

Class Method Details

.[](reference) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/async/http/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/async/http/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



82
83
84
85
86
87
88
89
90
91
# File 'lib/async/http/reference.rb', line 82

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



93
94
95
# File 'lib/async/http/reference.rb', line 93

def [] parameters
  self.dup(nil, parameters)
end

#append(buffer) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/async/http/reference.rb', line 60

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



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/async/http/reference.rb', line 97

def dup(path = nil, parameters = nil)
  if parameters and @parameters
    parameters = @parameters.merge(parameters)
  else
    parameters = @parameters
  end
  
  if path
    path = @path + '/' + path
  else
    path = @path
  end
  
  self.class.new(path, @query_string, @fragment, parameters)
end

#to_strObject Also known as: to_s



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

def to_str
  append(String.new)
end