Class: EsiForRack::Node::Context

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resolver, lookup) ⇒ Context

Returns a new instance of Context.



94
95
96
97
98
99
100
101
102
# File 'lib/esi_for_rack/node.rb', line 94

def initialize(resolver, lookup)
  @resolver = resolver
  @lookup = lookup.is_a?(Array) ? lookup : [lookup]
  @include = Include.new
  @choose = Choose.new
  @vars = Vars.new
  @try = Try.new
  
end

Instance Attribute Details

#docObject (readonly)

Returns the value of attribute doc.



92
93
94
# File 'lib/esi_for_rack/node.rb', line 92

def doc
  @doc
end

#resolverObject (readonly)

Returns the value of attribute resolver.



92
93
94
# File 'lib/esi_for_rack/node.rb', line 92

def resolver
  @resolver
end

Instance Method Details

#lookup(url) ⇒ Object



104
105
106
107
108
109
110
# File 'lib/esi_for_rack/node.rb', line 104

def lookup(url)
  @lookup.each do |l|
    resolved_body = l[url]
    return resolved_body if resolved_body
  end
  nil
end

#parse(document) ⇒ Object



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

def parse(document)
  document.gsub!('esi:', 'esi_')
  document.gsub!(/(<\/?esi_[^>]*>)/, ']]>\1<![CDATA[')
  document[0,0] = %|<?xml version="1.0"?>\n<esi_root><![CDATA[|
  document << ']]></esi_root>'
  
  @doc = Nokogiri::XML(document)
  
  @doc.css('esi_comment').each do |esi_comment|
    esi_comment.replace(Nokogiri::XML::CDATA.new(doc, ''))
  end
  
  process(@doc.css('esi_root')[0])
  
  result = ''
  @doc.css('esi_root')[0].children.each do |n|
    result << n.to_str
  end
  
  result
end

#process(doc_fragment) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/esi_for_rack/node.rb', line 134

def process(doc_fragment)
  # have to go one at a time because of limitation of .css, its not a live list.
  # ps, i'll only break if i totally have to
  loop do
    should_break = true
    doc_fragment.css('esi_try,esi_choose,esi_vars,esi_include').each do |esi_node|
      case esi_node.name.to_sym
      when :esi_include
        @include.init(esi_node, self).execute_in_place!
      when :esi_choose
        @choose.init(esi_node, self).execute_in_place!
      when :esi_vars
        @vars.init(esi_node, self).execute_in_place!
      when :esi_try
        @try.init(esi_node, self).execute_in_place!
        should_break = false
        break
      end
    end
    break if should_break
  end
end