Class: DocverterServer::Manifest

Inherits:
Object
  • Object
show all
Defined in:
lib/docverter-server/manifest.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Manifest

Returns a new instance of Manifest.



23
24
25
26
# File 'lib/docverter-server/manifest.rb', line 23

def initialize(options={})
  @options = options
  @options['input_files'] ||= []
end

Class Method Details

.load_file(filename) ⇒ Object



13
14
15
16
17
# File 'lib/docverter-server/manifest.rb', line 13

def self.load_file(filename)
  File.open(filename) do |f|
    self.load_stream(f)
  end
end

.load_stream(stream) ⇒ Object



19
20
21
# File 'lib/docverter-server/manifest.rb', line 19

def self.load_stream(stream)
  self.new(YAML.load(stream))
end

Instance Method Details

#[](key) ⇒ Object



28
29
30
# File 'lib/docverter-server/manifest.rb', line 28

def [](key)
  @options[key]
end

#[]=(key, val) ⇒ Object



32
33
34
# File 'lib/docverter-server/manifest.rb', line 32

def []=(key, val)
  @options[key] = val
end

#command_optionsObject



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/docverter-server/manifest.rb', line 50

def command_options
  options = @options.dup
  input_files = options.delete('input_files')
  raise "No input files provided!" unless input_files.length > 0

  @pdf_page_size = options.delete('pdf_page_size')

  if options['to'] == 'pdf'
    _ = options.delete 'to'
    @pdf = true
  end

  command_options = []

  @test_mode = options.delete('test_mode')

  options.each do |k,v|

    raise InvalidManifestError.new("Invalid option: #{k}") unless k.match(/^[a-z0-9-]+/)
    
    option_key = k.to_s.gsub('_', '-')
    [v].flatten.each do |option_val|
      raise InvalidManifestError.new("Invalid option value: #{option_val}") unless option_val.to_s.match(/^[a-zA-Z0-9._-]+/)
      if option_val.is_a?(TrueClass) || option_val == 'true'
        command_options << "--#{option_key}"
      else
        command_options << "--#{option_key}=#{option_val}"
      end
    end
  end

  command_options += [input_files].flatten.compact

  command_options
end

#pdfObject



5
6
7
# File 'lib/docverter-server/manifest.rb', line 5

def pdf
  @pdf
end

#pdf_page_sizeObject



9
10
11
# File 'lib/docverter-server/manifest.rb', line 9

def pdf_page_size
  @pdf_page_size
end

#test_mode?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/docverter-server/manifest.rb', line 46

def test_mode?
  @test_mode
end

#validate!(dir) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/docverter-server/manifest.rb', line 86

def validate!(dir)
  raise InvalidManifestError.new("No input files found") unless @options['input_files'] && @options['input_files'].length > 0

  Dir.chdir(dir) do
    @options['input_files'].each do |filename|
      raise InvalidManifestError.new("Invalid input file: #{filename} not found") unless File.exists?(filename)
      raise InvalidManifestError.new("Invalid input file: #{filename} cannot start with /") if filename.strip[0] == '/'
    end

    raise InvalidManifestError.new("'from' key required") unless @options['from']
    raise InvalidManifestError.new("'to' key required") unless @options['to']

    raise InvalidManifestError.new("Not a valid 'from' type") unless
      DocverterServer::ConversionTypes.valid_input?(@options['from'])

    raise InvalidManifestError.new("Not a valid 'to' type") unless
      DocverterServer::ConversionTypes.valid_output?(@options['to'])
  end
end

#write(filename) ⇒ Object



36
37
38
39
40
# File 'lib/docverter-server/manifest.rb', line 36

def write(filename)
  File.open(filename, "w+") do |f|
    write_to_stream(f)
  end
end

#write_to_stream(stream) ⇒ Object



42
43
44
# File 'lib/docverter-server/manifest.rb', line 42

def write_to_stream(stream)
  stream.write YAML.dump(@options)
end