Class: Docxout

Inherits:
Object
  • Object
show all
Defined in:
lib/newsman/docx_output.rb

Overview

This class represents a report output in DOCX (MS Word) format. It builds a minimal, valid OOXML package by hand (no external docx-builder gem), so every run of text is explicitly styled with Times New Roman, 12pt.

Constant Summary collapse

CONTENT_TYPES =

Content types part, required by every OOXML package.

<<~XML
  <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  <Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types"><Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/><Default Extension="xml" ContentType="application/xml"/><Override PartName="/word/document.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml"/></Types>
XML
RELS =

Package-level relationships, pointing at the main document part.

<<~XML
  <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  <Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="word/document.xml"/></Relationships>
XML
FONT =
'Times New Roman'
SIZE =

Word expresses font size in half-points, so 12pt is 24.

'24'

Instance Method Summary collapse

Constructor Details

#initialize(root = '.') ⇒ Docxout

Returns a new instance of Docxout.



46
47
48
# File 'lib/newsman/docx_output.rb', line 46

def initialize(root = '.')
  @root = root
end

Instance Method Details

#filename(reporter, model) ⇒ Object



62
63
64
65
66
# File 'lib/newsman/docx_output.rb', line 62

def filename(reporter, model)
  date = Time.new.strftime('%d.%m.%Y')
  model = model.gsub('.', '-')
  "#{date}.#{reporter}.#{model}.docx"
end


50
51
52
53
54
55
56
57
58
59
60
# File 'lib/newsman/docx_output.rb', line 50

def print(report, reporter, model)
  puts "Create a docx file in a directory #{@root}"
  path = File.join(@root, filename(reporter, model))
  Zip::File.open(path, create: true) do |zip|
    zip.get_output_stream('[Content_Types].xml') { |f| f.write(CONTENT_TYPES) }
    zip.get_output_stream('_rels/.rels') { |f| f.write(RELS) }
    zip.get_output_stream('word/document.xml') { |f| f.write(document_xml(report)) }
  end
  puts "Report was successfully printed to a #{path}"
  File.basename(path)
end