class URITemplate::RFC6570::Expression

@private

Constants

BASE_LEVEL
CHARACTER_CLASS
COMMA
LIST_CONNECTOR
OPERATOR
PAIR_CONNECTOR
PAIR_IF_EMPTY
PREFIX
SEPARATOR
SPLITTER

Attributes

variables[R]

Public Class Methods

new(vars) click to toggle source
# File lib/uri_template/rfc6570/expression.rb, line 33
def initialize(vars)
  @variable_specs = vars
  @variables = vars.map(&:first)
  @variables.uniq!
end

Public Instance Methods

arity() click to toggle source
# File lib/uri_template/rfc6570/expression.rb, line 62
def arity
  @variable_specs.size
end
expand( vars ) click to toggle source
# File lib/uri_template/rfc6570/expression.rb, line 66
def expand( vars )
  result = []
  @variable_specs.each do | var, expand , max_length |
    if Utils.def? vars[var]
      result.push(*expand_one(var, vars[var], expand, max_length))
    end
  end
  if result.any?
    return (self.class::PREFIX + result.join(self.class::SEPARATOR))
  else
    return ''
  end
end
expand_partial( vars ) click to toggle source
# File lib/uri_template/rfc6570/expression.rb, line 80
def expand_partial( vars )
  result = []
  follow_up = self.class::FOLLOW_UP
  var_specs = []
  @variable_specs.each do | var, expand , max_length |
    if vars.key? var
      unless var_specs.none?
        result.push( follow_up.new( var_specs ) )
        var_specs = []
      end
      unless result.none?
        result.push( Literal.new(self.class::SEPARATOR) )
      end
      one = Array(expand_one(var, vars[var], expand, max_length))
      result.push( Literal.new(one.join(self.class::SEPARATOR)))
    end
    var_specs << [var,expand,max_length]
  end
  if result.none?
    # no literal was emitted so far
    return [ self ]
  end
  unless self.class::PREFIX.empty? || empty_literals?( result )
    result.unshift( Literal.new(self.class::PREFIX) )
  end
  if var_specs.size != 0
    result.push( follow_up.new( var_specs ) )
  end
  return result
end
extract(position,matched) click to toggle source
# File lib/uri_template/rfc6570/expression.rb, line 111
def extract(position,matched)
  name, expand, max_length = @variable_specs[position]
  if matched.nil?
    return [[ name , extracted_nil ]]
  end
  if expand
    it = URITemplate::RegexpEnumerator.new(self.class.hash_extractor(max_length), :rest => :raise)
    if position == 0
      matched = "#{self.class::SEPARATOR}#{matched}"
    end
    splitted = it.each(matched)\
      .map do |match|
        raise match.inspect if match.kind_of? String
        [ decode(match[1]), decode(match[2], false) ]
      end
    return after_expand(name, splitted)
  end

  return [ [ name, decode( matched ) ] ]
end
level() click to toggle source
# File lib/uri_template/rfc6570/expression.rb, line 50
def level
  if @variable_specs.none?{|_,expand,ml| expand || (ml > 0) }
    if @variable_specs.size == 1
      return self.class::BASE_LEVEL
    else
      return 3
    end
  else
    return 4
  end
end
to_s() click to toggle source
# File lib/uri_template/rfc6570/expression.rb, line 132
def to_s
  return '{' + self.class::OPERATOR + @variable_specs.map{|name,expand,max_length| name + (expand ? '*': '') + (max_length > 0 ? (':' + max_length.to_s) : '') }.join(',') + '}'
end

Protected Instance Methods

cut(str,chars) click to toggle source
# File lib/uri_template/rfc6570/expression.rb, line 243
def cut(str,chars)
  if chars > 0
    md = Regexp.compile("\\A#{self.class::CHARACTER_CLASS[:class]}{0,#{chars.to_s}}", Utils::KCODE_UTF8).match(str)
    return md[0]
  else
    return str
  end
end
decode(x, split = true) click to toggle source
# File lib/uri_template/rfc6570/expression.rb, line 216
def decode(x, split = true)
  if x.nil?
    return extracted_nil
  elsif split
    result = []
    # Add a comma if the last character is a comma
    # seems weird but is more compatible than changing
    # the regex.
    x += COMMA if x[-1..-1] == COMMA
    URITemplate::RegexpEnumerator.new(SPLITTER, :rest => :raise).each(x) do |match|
      if match[1]
        next if match[1].size == 1
        result << match[1][0..-3]
      elsif match[2]
        result << unescape(match[2])
      end
    end
    case(result.size)
      when 0 then ''
      when 1 then result.first
      else result
    end
  else
    unescape(x)
  end
end
empty_literals?( list ) click to toggle source
# File lib/uri_template/rfc6570/expression.rb, line 282
def empty_literals?( list )
  list.none?{|x| x.kind_of?(Literal) && !x.to_s.empty? }
end
escape(x) click to toggle source
# File lib/uri_template/rfc6570/expression.rb, line 200
def escape(x)
  Utils.escape_url(Utils.object_to_param(x))
end
pair(key, value, max_length = 0, &block) click to toggle source
# File lib/uri_template/rfc6570/expression.rb, line 252
def pair(key, value, max_length = 0, &block)
  ek = key
  if block
    ev = value.map(&block).join(self.class::LIST_CONNECTOR) 
  else
    ev = escape(value)
  end
  if !self.class::PAIR_IF_EMPTY and ev.size == 0
    return ek
  else
    return ek + self.class::PAIR_CONNECTOR + cut( ev, max_length )
  end
end
regex_builder() click to toggle source
# File lib/uri_template/rfc6570/expression.rb, line 208
def regex_builder
  self.class.regex_builder
end
transform_array(name, ary, expand , max_length) click to toggle source
# File lib/uri_template/rfc6570/expression.rb, line 274
def transform_array(name, ary, expand , max_length)
  if expand
    ary.map{|value| self_pair(name,value) }
  else
    [ self_pair(name, ary, max_length){|value| escape(value) } ]
  end
end
transform_hash(name, hsh, expand , max_length) click to toggle source
# File lib/uri_template/rfc6570/expression.rb, line 266
def transform_hash(name, hsh, expand , max_length)
  if expand
    hsh.map{|key,value| pair(escape(key),value) }
  else
    [ self_pair(name,hsh, max_length ){|key,value| escape(key)+self.class::LIST_CONNECTOR+escape(value)} ]
  end
end
unescape(x) click to toggle source
# File lib/uri_template/rfc6570/expression.rb, line 204
def unescape(x)
  Utils.unescape_url(x)
end

Private Instance Methods

expand_one( name, value, expand, max_length) click to toggle source
# File lib/uri_template/rfc6570/expression.rb, line 138
def expand_one( name, value, expand, max_length)
  if value.kind_of?(Hash) or Utils.pair_array?(value)
    return transform_hash(name, value, expand, max_length)
  elsif value.kind_of? Array
    return transform_array(name, value, expand, max_length)
  else
    return self_pair(name, value, max_length)
  end
end
extracted_nil() click to toggle source
# File lib/uri_template/rfc6570/expression.rb, line 152
def extracted_nil
  nil
end
length_limited?(max_length) click to toggle source
# File lib/uri_template/rfc6570/expression.rb, line 148
def length_limited?(max_length)
  max_length > 0
end