Class: ESI::Tag::Base

Inherits:
Object
  • Object
show all
Includes:
Log
Defined in:
lib/esi/tag/base.rb

Direct Known Subclasses

Container, Include, Invalidate

Constant Summary collapse

Validate =
['try','attempt','except','include','invalidate']

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Log

#log, #log_debug, #log_error, #log_request, #msg

Constructor Details

#initialize(router, headers, http_params, name, attrs, cache) ⇒ Base

Returns a new instance of Base.



13
14
15
16
17
18
19
20
# File 'lib/esi/tag/base.rb', line 13

def initialize(router,headers,http_params,name,attrs,cache)
  @router = router
  @headers = headers
  @http_params = http_params
  @attributes = attrs
  @cache = cache
  @name = name
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



9
10
11
# File 'lib/esi/tag/base.rb', line 9

def attributes
  @attributes
end

#cacheObject (readonly)

Returns the value of attribute cache.



9
10
11
# File 'lib/esi/tag/base.rb', line 9

def cache
  @cache
end

#childrenObject (readonly)

Returns the value of attribute children.



9
10
11
# File 'lib/esi/tag/base.rb', line 9

def children
  @children
end

#closedObject (readonly)

Returns the value of attribute closed.



9
10
11
# File 'lib/esi/tag/base.rb', line 9

def closed
  @closed
end

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/esi/tag/base.rb', line 9

def name
  @name
end

Class Method Details

.create(router, headers, http_params, tag_name, attrs, cache) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/esi/tag/base.rb', line 22

def self.create(router,headers,http_params,tag_name,attrs,cache)
  raise "Unsupport ESI tag: #{tag_name}" unless Validate.include?(tag_name)
  eval(tag_name.sub(/esi:/,'').capitalize).new(router,headers,http_params,tag_name,attrs,cache)
rescue => e
  log_debug "Failed while creating tag: #{tag_name}, with error: #{e.message}"
  raise e
end

Instance Method Details

#buffer(output, inner_html) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/esi/tag/base.rb', line 30

def buffer( output, inner_html )
  if output.respond_to?("<<")
    output << inner_html
  else
    output.call inner_html
  end
end

#close(output, options = {}) ⇒ Object



38
39
# File 'lib/esi/tag/base.rb', line 38

def close( output, options = {} )
end

#prepare_url_vars(url) ⇒ Object

:startdoc: scans the fragment url, replacing occurrances of $(VAR{ .. with the given value read from the HTTP Request object :enddoc:



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/esi/tag/base.rb', line 45

def prepare_url_vars(url)
  # scan url  for $(VAR{
  # using the regex look for url vars that get set via http params
  # for each var extract the value and store it in the operations array
  operations = []
  url.scan(/\$\([0-9a-zA-Z_{}]+\)/x) do|var|
    # extract the var name
    name = var.gsub(/\$\(/,'').gsub(/\{.*$/,'')
    key = var.gsub(/^.*\{/,'').gsub(/\}.*$/,'')
    value = Mongrel::HttpRequest.query_parse(@headers[name])[key]
    operations << {:sub => var, :with => (value||"")}
  end
  # apply each operation to the url
  operations.each { |op| url.gsub!(op[:sub],op[:with]) }
  url
end