Class: PdfTools::Tool

Inherits:
Object
  • Object
show all
Defined in:
lib/pdf_tools/tool.rb,
lib/pdf_tools/tool/merge_split.rb,
lib/pdf_tools/tool/image_to_pdf.rb

Overview

Abstract class that wraps command-line tools.

Examples:

PdfTools::Tool::ImageToPdf.new do |tool|
  tool.lk "license key"
  tool.f
  tool << "input.tif"
  tool << "output.pdf"
end

Direct Known Subclasses

ImageToPdf, MergeSplit

Defined Under Namespace

Classes: ImageToPdf, MergeSplit

Constant Summary collapse

KEY_VALUE_OPTIONS =
%w[i]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Tool

Returns a new instance of Tool.



39
40
41
42
# File 'lib/pdf_tools/tool.rb', line 39

def initialize(name)
  @name = name
  @args = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object

Any missing method will be transformed into a CLI option

Examples:

licmgr = PdfTools::Tool.new("licmgr")
licmgr.<< "info"
licmgr << "your license key"
licmgr.command #=> ["licmgr", "info", "your license key"]


128
129
130
131
132
133
# File 'lib/pdf_tools/tool.rb', line 128

def method_missing(name, *args)
  option = "-#{name.to_s.tr("_", "-")}"
  self << option
  merge!(args)
  self
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



37
38
39
# File 'lib/pdf_tools/tool.rb', line 37

def args
  @args
end

#nameObject (readonly)

Returns the value of attribute name.



37
38
39
# File 'lib/pdf_tools/tool.rb', line 37

def name
  @name
end

Class Method Details

.available_optionsObject



135
136
137
138
139
140
141
142
# File 'lib/pdf_tools/tool.rb', line 135

def self.available_options
  tool = new
  tool << "-version"

  help_page = tool.call(stderr: false)
  available_options = help_page.scan(/^\s+-[a-z-]+/).map(&:strip)
  available_options.map { |o| o[1..].tr("-", "_") }
end

.new(*args) ⇒ PdfTools::Tool, String

Execute the command.

Examples:

version = PdfTools::Tool::ImageToPdf.new { |b| b.version }
puts version

Returns:

  • (PdfTools::Tool, String)

    If no block is given, returns an instance of the tool, if block is given, returns the command output.



26
27
28
29
30
31
32
33
34
35
# File 'lib/pdf_tools/tool.rb', line 26

def self.new(*args)
  instance = super(*args)

  if block_given?
    yield instance
    instance.call
  else
    instance
  end
end

Instance Method Details

#<<(arg) ⇒ self

Append raw options.

Returns:

  • (self)


92
93
94
95
# File 'lib/pdf_tools/tool.rb', line 92

def <<(arg)
  args << arg.to_s
  self
end

#call(*args) {|Array| ... } ⇒ String

Build and execute the command.

Examples:

img2pdf = PdfTools::Tool::ImageToPdf.new
img2pdf.d(150)
img2pdf << "input.tif"
img2pdf << "output.pdf"
img2pdf.call # executes `img2pdf -d 150 input.tif output.pdf`
img2pdf = PdfTools::Tool::ImageToPdf.new

img2pdf.call do |stdout, stderr, status|
  # ...
end

Yields:

  • (Array)

    yields stdout, stderr, exit status (optional)

Returns:

  • (String)

    the output of the command



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/pdf_tools/tool.rb', line 63

def call(*args)
  options = args[-1].is_a?(Hash) ? args.pop : {}

  options[:stderr] = false if block_given?

  shell = PdfTools::Shell.new
  stdout, stderr, status = shell.run(command, options)
  yield stdout, stderr, status if block_given?

  stdout.chomp("\n")
end

#commandArray<String>

The built command as array.

Examples:

img2pdf = PdfTools::Tool::ImageToPdf.new
img2pdf.d(150)
img2pdf << "input.tif"
img2pdf << "output.pdf"
img2pdf.command #=> ["img2pdf", "-d", "150", "input.tif", "output.pdf"]

Returns:

  • (Array<String>)


85
86
87
# File 'lib/pdf_tools/tool.rb', line 85

def command
  [*name, *args]
end

#merge!(add_args) ⇒ self

Merge a list of raw options.

Returns:

  • (self)


100
101
102
103
# File 'lib/pdf_tools/tool.rb', line 100

def merge!(add_args)
  add_args.each { |arg| self << arg }
  self
end