Class: WADL::Address
Overview
The Address class keeps track of the user's path through a resource graph. Values for WADL parameters may be specified at any time using the bind method. An Address cannot be turned into a URI and header set until all required parameters have been bound to values.
An Address object is built up through calls to Resource#address
Instance Attribute Summary (collapse)
-
- (Object) header_params
readonly
Returns the value of attribute header_params.
-
- (Object) headers
readonly
Returns the value of attribute headers.
-
- (Object) path_fragments
readonly
Returns the value of attribute path_fragments.
-
- (Object) path_params
readonly
Returns the value of attribute path_params.
-
- (Object) query_params
readonly
Returns the value of attribute query_params.
-
- (Object) query_vars
readonly
Returns the value of attribute query_vars.
Class Method Summary (collapse)
Instance Method Summary (collapse)
- - (Object) auth(header, value)
-
- (Object) bind!(args = {})
Binds some or all of the unbound variables in this address to values.
-
- (Object) deep_copy
Perform a deep copy.
-
- (Address) initialize(path_fragments = [], query_vars = [], headers = {}, path_params = {}, query_params = {}, header_params = {})
constructor
A new instance of Address.
- - (Object) to_s (also: #inspect)
- - (Object) uri(args = {})
Constructor Details
- (Address) initialize(path_fragments = [], query_vars = [], headers = {}, path_params = {}, query_params = {}, header_params = {})
A new instance of Address
49 50 51 52 53 54 55 |
# File 'lib/wadl/address.rb', line 49 def initialize(path_fragments = [], query_vars = [], headers = {}, path_params = {}, query_params = {}, header_params = {}) @path_fragments, @query_vars, @headers = path_fragments, query_vars, headers @path_params, @query_params, @header_params = path_params, query_params, header_params @auth = {} end |
Instance Attribute Details
- (Object) header_params (readonly)
Returns the value of attribute header_params
42 43 44 |
# File 'lib/wadl/address.rb', line 42 def header_params @header_params end |
- (Object) headers (readonly)
Returns the value of attribute headers
42 43 44 |
# File 'lib/wadl/address.rb', line 42 def headers @headers end |
- (Object) path_fragments (readonly)
Returns the value of attribute path_fragments
42 43 44 |
# File 'lib/wadl/address.rb', line 42 def path_fragments @path_fragments end |
- (Object) path_params (readonly)
Returns the value of attribute path_params
42 43 44 |
# File 'lib/wadl/address.rb', line 42 def path_params @path_params end |
- (Object) query_params (readonly)
Returns the value of attribute query_params
42 43 44 |
# File 'lib/wadl/address.rb', line 42 def query_params @query_params end |
- (Object) query_vars (readonly)
Returns the value of attribute query_vars
42 43 44 |
# File 'lib/wadl/address.rb', line 42 def query_vars @query_vars end |
Class Method Details
+ (Object) embedded_param_names(fragment)
45 46 47 |
# File 'lib/wadl/address.rb', line 45 def self.(fragment) fragment.scan(/\{(.+?)\}/).flatten end |
Instance Method Details
- (Object) auth(header, value)
154 155 156 157 |
# File 'lib/wadl/address.rb', line 154 def auth(header, value) @auth[header] = value self end |
- (Object) bind!(args = {})
Binds some or all of the unbound variables in this address to values.
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/wadl/address.rb', line 87 def bind!(args = {}) path_var_values = args[:path] || {} query_var_values = args[:query] || {} header_var_values = args[:headers] || {} @auth.each { |header, value| header_var_values[header] = value }.clear # Bind variables found in the path fragments. path_params_to_delete = [] path_fragments.each { |fragment| if fragment.respond_to?(:to_str) # This fragment is a string which might contain {} substitutions. # Make any substitutions available to the provided path variables. self.class.(fragment).each { |name| value = path_var_values[name] || path_var_values[name.to_sym] value = if param = path_params[name] path_params_to_delete << param param % value else Param.default.format(value, name) end fragment.gsub!("{#{name}}", value) } else # This fragment is an array of Param objects (style 'matrix' # or 'plain') which may be bound to strings. As substitutions # happen, the array will become a mixed array of Param objects # and strings. fragment.each_with_index { |param, i| next unless param.respond_to?(:name) name = param.name value = path_var_values[name] || path_var_values[name.to_sym] value = param % value fragment[i] = value if value path_params_to_delete << param } end } # Delete any embedded path parameters that are now bound from # our list of unbound parameters. path_params_to_delete.each { |p| path_params.delete(p.name) } # Bind query variable values to query parameters query_var_values.each { |name, value| param = query_params.delete(name.to_s) query_vars << param % value if param } # Bind header variables to header parameters header_var_values.each { |name, value| if param = header_params.delete(name.to_s) headers[name] = param % value else warn %Q{Ignoring unknown header parameter "#{name}"!} end } self end |
- (Object) deep_copy
Perform a deep copy.
58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/wadl/address.rb', line 58 def deep_copy address = Address.new( _deep_copy_array(@path_fragments), _deep_copy_array(@query_vars), _deep_copy_hash(@headers), @path_params.dup, @query_params.dup, @header_params.dup ) @auth.each { |header, value| address.auth(header, value) } address end |
- (Object) to_s Also known as: inspect
73 74 75 76 77 78 79 80 81 82 |
# File 'lib/wadl/address.rb', line 73 def to_s "Address:\n" << " Path fragments: #{@path_fragments.inspect}\n" << " Query variables: #{@query_vars.inspect}\n" << " Header variables: #{@headers.inspect}\n" << " Authorization parameters: #{@auth.inspect}\n" << " Unbound path parameters: #{@path_params.inspect}\n" << " Unbound query parameters: #{@query_params.inspect}\n" << " Unbound header parameters: #{@header_params.inspect}\n" end |
- (Object) uri(args = {})
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/wadl/address.rb', line 159 def uri(args = {}) obj, uri = deep_copy.bind!(args), '' # Build the path obj.path_fragments.flatten.each { |fragment| if fragment.respond_to?(:to_str) = self.class.(fragment) unless .empty? raise ArgumentError, %Q{Missing a value for required path parameter "#{[0]}"!} end unless fragment.empty? uri << '/' unless uri.empty? || uri =~ /\/\z/ uri << fragment end elsif fragment.required? # This is a required Param that was never bound to a value. raise ArgumentError, %Q{Missing a value for required path parameter "#{fragment.name}"!} end } # Hunt for required unbound query parameters. obj.query_params.each { |name, value| if value.required? raise ArgumentError, %Q{Missing a value for required query parameter "#{value.name}"!} end } # Hunt for required unbound header parameters. obj.header_params.each { |name, value| if value.required? raise ArgumentError, %Q{Missing a value for required header parameter "#{value.name}"!} end } URIParts.new(uri, obj.query_vars, obj.headers) end |