Class: URITemplate::RFC6570::Expression

Inherits:
Token
  • Object
show all
Extended by:
ClassMethods
Includes:
Expression
Defined in:
lib/uri_template/rfc6570/expression.rb

Direct Known Subclasses

Named, Unnamed

Defined Under Namespace

Modules: ClassMethods Classes: Basic, FormQuery, FormQueryContinuation, Fragment, Label, Named, Path, PathParameters, Reserved, Unnamed

Constant Summary collapse

PREFIX =
''.freeze
SEPARATOR =
','.freeze
PAIR_CONNECTOR =
'='.freeze
PAIR_IF_EMPTY =
true
LIST_CONNECTOR =
','.freeze
BASE_LEVEL =
1
CHARACTER_CLASS =
CHARACTER_CLASSES[:unreserved]
OPERATOR =
''

Constants included from Token

Token::EMPTY_ARRAY

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ClassMethods

generate_hash_extractor, hash_extractor, hash_extractors

Methods included from Expression

#expression?, #literal?

Methods included from Token

#ends_with_slash?, #host?, #scheme?, #size, #starts_with_slash?

Constructor Details

#initialize(vars) ⇒ Expression

Returns a new instance of Expression.



34
35
36
37
38
# File 'lib/uri_template/rfc6570/expression.rb', line 34

def initialize(vars)
  @variable_specs = vars
  @variables = vars.map(&:first)
  @variables.uniq!
end

Instance Attribute Details

#variablesObject (readonly)

Returns the value of attribute variables.



32
33
34
# File 'lib/uri_template/rfc6570/expression.rb', line 32

def variables
  @variables
end

Instance Method Details

#arityObject



63
64
65
# File 'lib/uri_template/rfc6570/expression.rb', line 63

def arity
  @variable_specs.size
end

#expand(vars) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/uri_template/rfc6570/expression.rb', line 67

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) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/uri_template/rfc6570/expression.rb', line 81

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) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/uri_template/rfc6570/expression.rb', line 112

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

#levelObject



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/uri_template/rfc6570/expression.rb', line 51

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_sObject



133
134
135
# File 'lib/uri_template/rfc6570/expression.rb', line 133

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