Class: Doze::URITemplate::Variable

Inherits:
Doze::URITemplate show all
Defined in:
lib/doze/uri_template.rb

Direct Known Subclasses

QuadHexBytesVariable

Constant Summary collapse

DEFAULT_REGEXP =
"[^\/.,;?]+"
BYTESIZE_METHOD =

String#size under Ruby 1.8 and String#bytesize under 1.9.

''.respond_to?(:bytesize) ? 'bytesize' : 'size'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Doze::URITemplate

#+, #anchored_regexp, compile, #compile_expand!, #inspect, #match, #match_with_trailing, #parts, #start_anchored_regexp, #with_prefix

Constructor Details

#initialize(name, regexp = DEFAULT_REGEXP) ⇒ Variable

Returns a new instance of Variable.



75
76
77
# File 'lib/doze/uri_template.rb', line 75

def initialize(name, regexp=DEFAULT_REGEXP)
  @name = name; @regexp = regexp
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



73
74
75
# File 'lib/doze/uri_template.rb', line 73

def name
  @name
end

Instance Method Details

#expand(vars) ⇒ Object



89
90
91
# File 'lib/doze/uri_template.rb', line 89

def expand(vars)
  Doze::Utils.escape(vars[@name].to_s)
end

#expand_code_fragmentObject

inlines Doze::Utils.escape (optimised from Rack::Utils.escape) with further effort to avoid an extra method call for bytesize 1.9 compat.



110
111
112
# File 'lib/doze/uri_template.rb', line 110

def expand_code_fragment
  "\#{vars[#{@name.inspect}].to_s.gsub(/([^a-zA-Z0-9_.-]+)/n) {'%'+$1.unpack('H2'*$1.#{BYTESIZE_METHOD}).join('%').upcase}}"
end

#partially_expand(vars) ⇒ Object



93
94
95
96
97
98
99
# File 'lib/doze/uri_template.rb', line 93

def partially_expand(vars)
  if vars.has_key?(@name)
    String.new(expand(vars))
  else
    self
  end
end

#regexp_fragmentObject



79
80
81
# File 'lib/doze/uri_template.rb', line 79

def regexp_fragment
  "(#{@regexp})"
end

#to_sObject



83
84
85
# File 'lib/doze/uri_template.rb', line 83

def to_s
  "{#{@name}}"
end

#translate_captured_string(string) ⇒ Object



101
102
103
104
# File 'lib/doze/uri_template.rb', line 101

def translate_captured_string(string)
  # inlines Doze::Utils.unescape, but with gsub! rather than gsub since this is faster and the matched string is throwaway
  string.gsub!(/((?:%[0-9a-fA-F]{2})+)/n) {[$1.delete('%')].pack('H*')}; string
end

#variablesObject



87
# File 'lib/doze/uri_template.rb', line 87

def variables; [self]; end