class Caracal::Core::Models::IFrameModel

This class handles block options passed to the img method.

Attributes

iframe_data[R]
iframe_ignorables[R]
iframe_namespaces[R]
iframe_relationships[R]
iframe_url[R]

accessors

Public Class Methods

new(options={}, &block) click to toggle source

initialization

Calls superclass method Caracal::Core::Models::BaseModel::new
# File lib/caracal/core/models/iframe_model.rb, line 23
def initialize(options={}, &block)
  super options, &block
end

Public Instance Methods

file() click to toggle source
GETTERS ==========================
# File lib/caracal/core/models/iframe_model.rb, line 89
def file
  @file ||= begin
    if iframe_url.nil?
      file = Tempfile.new('iframe')
      file.write iframe_data
      file.rewind
    else
      file = open(iframe_url)
    end
    file
  end
end
ignorables() click to toggle source
# File lib/caracal/core/models/iframe_model.rb, line 102
def ignorables
  @iframe_ignorables || []
end
namespaces() click to toggle source
# File lib/caracal/core/models/iframe_model.rb, line 106
def namespaces
  @iframe_namespaces || {}
end
preprocess!() click to toggle source
PROCESSING =======================
# File lib/caracal/core/models/iframe_model.rb, line 34
def preprocess!
  ::Zip::File.open(file) do |zip|
    # locate relationships xml
    entry      = zip.glob('word/_rels/document.xml.rels').first
    content    = entry.get_input_stream.read
    rel_xml    = Nokogiri::XML(content)

    # locate document xml
    entry      = zip.glob('word/document.xml').first
    content    = entry.get_input_stream.read
    doc_xml    = Nokogiri::XML(content)

    # master nodesets
    rel_nodes = rel_xml.children.first.children
    doc_root  = doc_xml.at_xpath('//w:document')
    pic_nodes = doc_xml.xpath('//pic:pic', { pic: 'http://schemas.openxmlformats.org/drawingml/2006/picture' })

    # namespaces
    @iframe_namespaces = doc_root.namespaces

    # ignorable namespaces
    if a = doc_root.attributes['Ignorable']
      @iframe_ignorables = a.value.split(/\s+/)
    end

    # relationships
    media_map = rel_nodes.reduce({}) do |hash, node|
      type = node.at_xpath('@Type').value
      if type.slice(-5, 5) == 'image'
        id   = node.at_xpath('@Id').value
        path = "word/#{ node.at_xpath('@Target').value }"
        hash[id] = path
      end
      hash
    end
    @iframe_relationships = pic_nodes.reduce([]) do |array, node|
      r_node  = node.children[1].children[0]
      r_id    = r_node.attributes['embed'].value.to_s
      r_media = media_map[r_id]

      p_node  = node.children[0].children[0]
      p_id    = p_node.attributes['id'].to_s.to_i
      p_name  = p_node.attributes['name'].to_s
      p_data  = zip.glob(r_media).first.get_input_stream.read

      # register relationship
      array << { id: r_id, type: 'image', target: p_name, data: p_data }
      array
    end
  end
end
relationships() click to toggle source
# File lib/caracal/core/models/iframe_model.rb, line 110
def relationships
  @iframe_relationships || []
end
valid?() click to toggle source
VALIDATION =======================
# File lib/caracal/core/models/iframe_model.rb, line 127
def valid?
  vals = option_keys.map { |m| send("iframe_#{ m }") }.compact
  vals = vals.reject { |v| v.size == 0 }
  vals.size > 0
end

Private Instance Methods

option_keys() click to toggle source
# File lib/caracal/core/models/iframe_model.rb, line 140
def option_keys
  [:url, :data]
end