Class: Caracal::Core::Models::IFrameModel
- Defined in:
- lib/caracal/core/models/iframe_model.rb
Overview
This class handles block options passed to the img method.
Instance Attribute Summary collapse
-
#iframe_data ⇒ Object
readonly
Returns the value of attribute iframe_data.
-
#iframe_ignorables ⇒ Object
readonly
Returns the value of attribute iframe_ignorables.
-
#iframe_namespaces ⇒ Object
readonly
Returns the value of attribute iframe_namespaces.
-
#iframe_relationships ⇒ Object
readonly
Returns the value of attribute iframe_relationships.
-
#iframe_url ⇒ Object
readonly
accessors.
Instance Method Summary collapse
-
#file ⇒ Object
GETTERS ==========================.
- #ignorables ⇒ Object
-
#initialize(options = {}, &block) ⇒ IFrameModel
constructor
initialization.
- #namespaces ⇒ Object
-
#preprocess! ⇒ Object
PROCESSING =======================.
- #relationships ⇒ Object
-
#valid? ⇒ Boolean
VALIDATION =======================.
Constructor Details
#initialize(options = {}, &block) ⇒ IFrameModel
initialization
23 24 25 |
# File 'lib/caracal/core/models/iframe_model.rb', line 23 def initialize(={}, &block) super , &block end |
Instance Attribute Details
#iframe_data ⇒ Object (readonly)
Returns the value of attribute iframe_data.
17 18 19 |
# File 'lib/caracal/core/models/iframe_model.rb', line 17 def iframe_data @iframe_data end |
#iframe_ignorables ⇒ Object (readonly)
Returns the value of attribute iframe_ignorables.
18 19 20 |
# File 'lib/caracal/core/models/iframe_model.rb', line 18 def iframe_ignorables @iframe_ignorables end |
#iframe_namespaces ⇒ Object (readonly)
Returns the value of attribute iframe_namespaces.
19 20 21 |
# File 'lib/caracal/core/models/iframe_model.rb', line 19 def iframe_namespaces @iframe_namespaces end |
#iframe_relationships ⇒ Object (readonly)
Returns the value of attribute iframe_relationships.
20 21 22 |
# File 'lib/caracal/core/models/iframe_model.rb', line 20 def iframe_relationships @iframe_relationships end |
#iframe_url ⇒ Object (readonly)
accessors
16 17 18 |
# File 'lib/caracal/core/models/iframe_model.rb', line 16 def iframe_url @iframe_url end |
Instance Method Details
#file ⇒ Object
GETTERS ==========================
89 90 91 92 93 94 95 96 97 98 99 100 |
# 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 ⇒ Object
102 103 104 |
# File 'lib/caracal/core/models/iframe_model.rb', line 102 def ignorables @iframe_ignorables || [] end |
#namespaces ⇒ Object
106 107 108 |
# File 'lib/caracal/core/models/iframe_model.rb', line 106 def namespaces @iframe_namespaces || {} end |
#preprocess! ⇒ Object
PROCESSING =======================
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# 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 ⇒ Object
110 111 112 |
# File 'lib/caracal/core/models/iframe_model.rb', line 110 def relationships @iframe_relationships || [] end |
#valid? ⇒ Boolean
VALIDATION =======================
127 128 129 130 131 |
# 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 |