Class: Discodactyl::URITemplate

Inherits:
Object
  • Object
show all
Defined in:
lib/discodactyl/uri_template.rb

Overview

Basic URI templates as used in XRD. Not to be confused with tools.ietf.org/html/draft-gregorio-uritemplate

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pattern) ⇒ URITemplate

Returns a new instance of URITemplate.


6
7
8
# File 'lib/discodactyl/uri_template.rb', line 6

def initialize(pattern)
  @pattern = pattern
end

Instance Attribute Details

#patternObject

Returns the value of attribute pattern


5
6
7
# File 'lib/discodactyl/uri_template.rb', line 5

def pattern
  @pattern
end

Instance Method Details

#==(other) ⇒ Object


21
22
23
24
25
26
27
28
29
30
# File 'lib/discodactyl/uri_template.rb', line 21

def ==(other)
  return false unless other.class == self.class
  return false unless other.instance_variables == self.instance_variables
  self.instance_variables.each do |var|
    self_var = self.instance_variable_get(var)
    other_var = other.instance_variable_get(var)
    return false unless self_var.eql?(other_var)
  end
  true
end

#to_uri(params) ⇒ Object


9
10
11
12
13
14
15
16
17
18
19
# File 'lib/discodactyl/uri_template.rb', line 9

def to_uri(params)
  require 'cgi'
  uri = @pattern
  while /\{%([^}]*)\}/ =~ uri
    uri.gsub!($~[0], params[$~[1]])
  end
  while /\{([^}]*)\}/ =~ uri
    uri.gsub!($~[0], CGI::escape(params[$~[1]].to_s))
  end
  uri
end