Class: Tim::UserKeysFilter

Inherits:
Object
  • Object
show all
Defined in:
app/filters/tim/user_keys_filter.rb

Constant Summary collapse

@@user_keys =
YAML.load(File.read(File.join(Tim::Engine.root, "config", "user_keys.yml")))

Class Method Summary collapse

Class Method Details

.before(controller) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
# File 'app/filters/tim/user_keys_filter.rb', line 9

def self.before(controller)
  if controller.request.format == "application/xml"
    begin
      @controller = controller
      resource_name = @controller.controller_name.singularize.to_sym
      set_template_params(resource_name)
      @controller.params[resource_name] = replace_user_keys(@controller.params[resource_name], @@user_keys[resource_name])
    rescue => e
      @controller.head :unprocessable_entity
    end
  end
end

.replace_user_keys(map, user_keys) ⇒ Object

Replaces all instances of user keys with those defined in @@user_keys Supports N levels of nested maps.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/filters/tim/user_keys_filter.rb', line 24

def self.replace_user_keys(map, user_keys)
  modified_map = map.clone
  map.each_pair do |key, value|
    # Sets the full template XML in the template.xml attribute
    if key == "template" || key == :template
      modified_map[key] = { :xml => template_xml }
    end

    if user_keys.has_key? key
      if map[key].instance_of? Hash
        # FIXME Using recursion in this way increases heap memory usage.
        # If we decide to stick with this approach a more memory efficient
        # implementation should be considered.
        modified_map[key] = replace_user_keys(map[key], user_keys)
      elsif map[key].instance_of? Array
        modified_map[key] = map[key].collect { |v| replace_user_keys(v, user_keys)}
      end
      modified_map[user_keys[key]] = modified_map.delete(key)
    end
  end
  modified_map
end

.set_template_params(resource_name) ⇒ Object

We are storing raw XML in the templates.xml attributes. Therefore we must retrieve the raw XML from the request body.



49
50
51
52
53
# File 'app/filters/tim/user_keys_filter.rb', line 49

def self.set_template_params(resource_name)
  if resource_name == :template
    @controller.params[:template] = { :xml => template_xml }
  end
end

.template_xmlObject



55
56
57
# File 'app/filters/tim/user_keys_filter.rb', line 55

def self.template_xml
  ::Nokogiri::XML(@controller.request.body.read).xpath("//template").to_xml
end