Class: URITemplate::RFC6570
- Inherits:
-
Object
- Object
- URITemplate::RFC6570
- Extended by:
- Forwardable, ClassMethods
- Includes:
- URITemplate
- Defined in:
- lib/uri_template/rfc6570.rb,
lib/uri_template/rfc6570/expression.rb,
lib/uri_template/rfc6570/regex_builder.rb
Overview
Most specs and examples refer to this class directly, because they are acutally refering to this specific implementation. If you just want uri templates, you should rather use the methods on URITemplate to create templates since they will select an implementation.
A uri template which should comply with the rfc 6570 ( tools.ietf.org/html/rfc6570 ).
Defined Under Namespace
Modules: ClassMethods Classes: Invalid, InvalidValue, RegexBuilder
Constant Summary
- TYPE =
:rfc6570
- NO_PROCESSING =
Specifies that no processing should be done upon extraction.
[]
- CONVERT_VALUES =
Specifies that the extracted values should be processed.
[:convert_values]
- CONVERT_RESULT =
Specifies that the extracted variable list should be processed.
[:convert_result]
- DEFAULT_PROCESSING =
Default processing. Means: convert values and the list itself.
CONVERT_VALUES + CONVERT_RESULT
Constants included from URITemplate
HOST_REGEX, SCHEME_REGEX, URI_SPLIT, VERSIONS
Instance Attribute Summary (collapse)
-
- (Object) options
readonly
Returns the value of attribute options.
Instance Method Summary (collapse)
-
- (Object) ===(uri)
Alias for to_r.=== .
-
- (Object) expand(variables = {})
Expands the template with the given variables.
-
- (Object) extract(uri_or_match, post_processing = DEFAULT_PROCESSING)
Extracts variables from a uri ( given as string ) or an instance of MatchData ( which was matched by the regexp of this template. The actual result depends on the value of post_processing. This argument specifies whether pair arrays should be converted to hashes..
-
- (Object) extract_simple(uri_or_match)
Extracts variables without any proccessing.
-
- (RFC6570) initialize(pattern_or_tokens, options = {})
constructor
A new instance of RFC6570.
-
- (Object) level
Returns the level of this template according to the rfc 6570 ( tools.ietf.org/html/rfc6570#section-1.2 ).
-
- (Object) match(uri) { ... }
Alias for to_r.match .
-
- (Object) to_r
Compiles this template into a regular expression which can be used to test whether a given uri matches this template.
-
- (Object) tokens
Returns an array containing a the template tokens.
-
- (Object) type
The type of this template.
Methods included from ClassMethods
Methods included from URITemplate
apply, coerce, coerce_first_arg, #concat, convert, #eq, #host?, new, #path_concat, #pattern, #relative?, resolve_class, #scheme?, #static_characters, try_convert, #variables
Constructor Details
- (RFC6570) initialize(pattern_or_tokens, options = {})
A new instance of RFC6570
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 |
# File 'lib/uri_template/rfc6570.rb', line 265 def initialize(pattern_or_tokens,={}) @options = .dup.freeze if pattern_or_tokens.kind_of? String @pattern = pattern_or_tokens.dup @pattern.freeze unless @options[:lazy] self.tokens end elsif pattern_or_tokens.kind_of? Array @tokens = pattern_or_tokens.dup @tokens.freeze else raise ArgumentError, "Expected to receive a pattern string, but got #{pattern_or_tokens.inspect}" end end |
Instance Attribute Details
- (Object) options (readonly)
Returns the value of attribute options
260 261 262 |
# File 'lib/uri_template/rfc6570.rb', line 260 def @options end |
Instance Method Details
- (Object) ===(uri)
Alias for to_r.=== . Tests whether this template matches a given uri.
384 |
# File 'lib/uri_template/rfc6570.rb', line 384 def_delegators :to_r, :=== |
- (Object) expand(variables = {})
All keys of the supplied hash should be strings as anything else won't be recognised.
There are neither default values for variables nor will anything be raised if a variable is missing. Please read the spec if you want to know how undefined variables are handled.
Expands the template with the given variables. The expansion should be compatible to uritemplate spec rfc 6570 ( tools.ietf.org/html/rfc6570 ).
|
# File 'lib/uri_template/rfc6570.rb', line 281
|
- (Object) extract(uri_or_match, post_processing = DEFAULT_PROCESSING)
Don't expect that an extraction can fully recover the expanded variables. Extract rather generates a variable list which should expand to the uri from which it were extracted. In general the following equation should hold true:
a_tpl.( a_tpl.extract( an_uri ) ) == an_uri
The current implementation drops duplicated variables instead of checking them.
Extracts variables from a uri ( given as string ) or an instance of MatchData ( which was matched by the regexp of this template. The actual result depends on the value of post_processing. This argument specifies whether pair arrays should be converted to hashes.
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 |
# File 'lib/uri_template/rfc6570.rb', line 349 def extract(uri_or_match, post_processing = DEFAULT_PROCESSING ) if uri_or_match.kind_of? String m = self.to_r.match(uri_or_match) elsif uri_or_match.kind_of?(MatchData) if uri_or_match.respond_to?(:regexp) and uri_or_match.regexp != self.to_r raise ArgumentError, "Trying to extract variables from MatchData which was not generated by this template." end m = uri_or_match elsif uri_or_match.nil? return nil else raise ArgumentError, "Expected to receive a String or a MatchData, but got #{uri_or_match.inspect}." end if m.nil? return nil else result = extract_matchdata(m, post_processing) if block_given? return yield result end return result end end |
- (Object) extract_simple(uri_or_match)
Extracts variables without any proccessing. This is equivalent to #extract with options NO_PROCESSING.
377 378 379 |
# File 'lib/uri_template/rfc6570.rb', line 377 def extract_simple(uri_or_match) extract( uri_or_match, NO_PROCESSING ) end |
- (Object) level
Returns the level of this template according to the rfc 6570 ( tools.ietf.org/html/rfc6570#section-1.2 ). Higher level means higher complexity. Basically this is defined as:
-
Level 1: no operators, one variable per expansion, no variable modifiers
-
Level 2: '+' and '#' operators, one variable per expansion, no variable modifiers
-
Level 3: all operators, multiple variables per expansion, no variable modifiers
-
Level 4: all operators, multiple variables per expansion, all variable modifiers
Templates of lower levels might be convertible to other formats while templates of higher levels might be incompatible. Level 1 for example should be convertible to any other format since it just contains simple expansions.
423 424 425 |
# File 'lib/uri_template/rfc6570.rb', line 423 def level tokens.map(&:level).max end |
- (Object) match(uri) { ... }
Alias for to_r.match . Matches this template against the given uri.
390 |
# File 'lib/uri_template/rfc6570.rb', line 390 def_delegators :to_r, :match |
- (Object) to_r
Compiles this template into a regular expression which can be used to test whether a given uri matches this template. This template is also used for #===.
305 306 307 308 309 310 311 312 |
# File 'lib/uri_template/rfc6570.rb', line 305 def to_r @regexp ||= begin source = tokens.map(&:to_r_source) source.unshift('\A') source.push('\z') Regexp.new( source.join, Utils::KCODE_UTF8) end end |
- (Object) tokens
Returns an array containing a the template tokens.
428 429 430 |
# File 'lib/uri_template/rfc6570.rb', line 428 def tokens @tokens ||= tokenize! end |
- (Object) type
The type of this template.
400 401 402 |
# File 'lib/uri_template/rfc6570.rb', line 400 def type self.class::TYPE end |