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



94
95
96
97
98
99
100
101
102
103
# File 'lib/async/http/reference.rb', line 94

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



105
106
107
# File 'lib/async/http/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/async/http/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/async/http/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

Returns:

  • (Boolean)


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

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

#parameters?Boolean

Returns:

  • (Boolean)


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

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

#query_string?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/async/http/reference.rb', line 64

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

#to_strObject Also known as: to_s



88
89
90
# File 'lib/async/http/reference.rb', line 88

def to_str
  append(String.new)
end