Class Wrest::UriTemplate

  1. lib/wrest/uri_template.rb
Parent: Object

Methods

public class

  1. new

public instance

  1. ==
  2. []
  3. to_uri

Attributes

uri_pattern [R]

Public class methods

new (uri_pattern, options = {})
[show source]
    # File lib/wrest/uri_template.rb, line 13
13:     def initialize(uri_pattern, options = {})
14:       @uri_pattern = uri_pattern.clone
15:       @options = options.clone
16:     end

Public instance methods

== (other)
[show source]
    # File lib/wrest/uri_template.rb, line 54
54:     def ==(other)
55:       return false if other.class != self.class
56:       return other.uri_pattern == self.uri_pattern 
57:     end
[] (path)
[show source]
    # File lib/wrest/uri_template.rb, line 50
50:     def [](path)
51:       UriTemplate.new(File.join(uri_pattern, path))
52:     end
to_uri (options = {})

Builds a new Wrest::Uri from this uri template by replacing the keys in the options that match with the corressponding values.

Example: template = UriTemplate.new(“coathangers.com/:resource/:id.:format“) template.to_uri(:resource => ‘shen_coins’, :id => 5, :format => :json)

> #localhost:3000/shen_coins/5.json>>

This feature can also be used to handle HTTP authentication where the username and password needs changing at runtime. However, this approach will fail if the password contains characters like ^ and @.

Note that beacuse because both HTTP Auth and UriTemplate use ’:’ as a delimiter, the pattern does look slightly weird, but it still works. Example: template = UriTemplate.new(“:username::password@coathangers.com/:resource/:id.:format“) template.to_uri(

:user => 'kaiwren',
:password => 'fupuppies',
:resource => 'portal',
:id => '1'

)

=> #<Wrest::Uri:0x18e0bec @uri=#<URI::HTTP:0x18e09a8 URL:http://kaiwren:fupuppies@coathangers.com/portal/1>>
[show source]
    # File lib/wrest/uri_template.rb, line 42
42:     def to_uri(options = {})
43:      merged_options = @options.merge(options) 
44:       Wrest::Uri.new(merged_options.inject(uri_pattern.clone) do |uri_string, tuple| 
45:         key, value = tuple
46:         uri_string.gsub(":#{key.to_s}", value.to_s)
47:       end , @options)
48:     end