Module: MsoMetadata

Defined in:
lib/mso_metadata.rb,
lib/mso_metadata/version.rb

Defined Under Namespace

Classes: Error

Constant Summary collapse

VERSION =
"0.1.0"

Class Method Summary collapse

Class Method Details

.read(office_xml_file) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/mso_metadata.rb', line 10

def self.read office_xml_file
   = {}
   = {}
  Zip::File.open(office_xml_file) do |zip_file|
    # Read the core metadata (docProps/core.xml)
    core_xml = zip_file.find_entry('docProps/core.xml')&.get_input_stream&.read
    if core_xml
      core_doc = Nokogiri::XML(core_xml)
      [:title] = core_doc.at_xpath('//dc:title')&.text
      [:subject] = core_doc.at_xpath('//dc:subject')&.text
      [:creator] = core_doc.at_xpath('//dc:creator')&.text
      [:keywords] = core_doc.at_xpath('//dc:keywords')&.text
      [:description] = core_doc.at_xpath('//dc:description')&.text
      [:lastModifiedBy] = core_doc.at_xpath('//cp:lastModifiedBy')&.text
      [:revision] = core_doc.at_xpath('//cp:revision')&.text
      [:category] = core_doc.at_xpath('//cp:category')&.text
      [:created] = core_doc.at_xpath('//dcterms:created')&.text
      [:modified] = core_doc.at_xpath('//dcterms:modified')&.text
    end

    # Read the app metadata (docProps/app.xml)
    app_xml = zip_file.find_entry('docProps/app.xml')&.get_input_stream&.read
    if app_xml
      app_doc = Nokogiri::XML(app_xml)
      [:application] = app_doc.at_xpath('//*:Application')&.text
      [:company] = app_doc.at_xpath('//*:Company')&.text
    end

    # Read the custom metadata (docProps/custom.xml)
    custom_xml = zip_file.find_entry('docProps/custom.xml')&.get_input_stream&.read
    if custom_xml
      custom_doc = Nokogiri::XML(custom_xml)
      custom_doc.xpath('//*:property').each do |property|
        value = property.text
        type_name = property.first_element_child.name
        # we convert bool and int, others are strings
        if type_name =~ /bool/
          value = value.downcase == 'true'
        elsif type_name =~ /i\d/
          value = value.to_i
        end
        [property.attr('name')] = value
      end
    end
  end
  { core: , custom:  }
end