Class: UriTemplate::Expression

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

Instance Method Summary collapse

Constructor Details

#initialize(tree) ⇒ Expression

Returns a new instance of Expression.



33
34
35
# File 'lib/uri_template.rb', line 33

def initialize(tree)
  @tree = tree
end

Instance Method Details

#encode_setObject



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

def encode_set
  set = Addressable::URI::CharacterClasses::UNRESERVED
  set += Addressable::URI::CharacterClasses::RESERVED if ["+", "#"].include? operator
  set
end

#expand(params = {}) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/uri_template.rb', line 37

def expand(params = {})
  out = var_list.map do |var|
    Var.new(self, var[:var]).value(params)
  end.compact

  return "" if out.empty?
  first_char + out.join(separator)
end

#first_charObject



64
65
66
67
68
69
# File 'lib/uri_template.rb', line 64

def first_char
  case operator
  when nil, "+" then ""
  else               operator
  end
end

#named?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/uri_template.rb', line 71

def named?
  [";", "?", "&"].include? operator
end

#operatorObject



50
51
52
# File 'lib/uri_template.rb', line 50

def operator
  @tree[:operator]
end

#separatorObject



54
55
56
57
58
59
60
61
62
# File 'lib/uri_template.rb', line 54

def separator
  case operator
    when nil, "+", "#" then ","
    when "."           then "."
    when "/"           then "/"
    when ";"           then ";"
    when "?", "&"      then "&"
  end
end

#str_if_emptyObject



75
76
77
78
79
80
# File 'lib/uri_template.rb', line 75

def str_if_empty
  case operator
  when "?", "&" then "="
  else ""
  end
end

#var_listObject



46
47
48
# File 'lib/uri_template.rb', line 46

def var_list
  @tree[:var_list]
end