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.



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

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.



94
95
96
# File 'lib/esi_for_rack/node.rb', line 94

def doc
  @doc
end

#resolverObject (readonly)

Returns the value of attribute resolver.



94
95
96
# File 'lib/esi_for_rack/node.rb', line 94

def resolver
  @resolver
end

Instance Method Details

#lookup(url) ⇒ Object



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

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

#parse(document) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/esi_for_rack/node.rb', line 114

def parse(document)
  document.gsub!('esi:', 'esi_')
  
  @doc = Nokogiri::HTML(document)
  
  @doc.css('esi_comment').each do |esi_comment|
    esi_comment.replace(Nokogiri::XML::CDATA.new(doc, ''))
  end
  
  process(@doc)
  
  @doc
end

#process(doc_fragment) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/esi_for_rack/node.rb', line 128

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|
      should_break = false
      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!
        break
      end
    end
    break if should_break
  end
end