Class: Docverter::Conversion
- Inherits:
-
OpenStruct
- Object
- OpenStruct
- Docverter::Conversion
- Defined in:
- lib/docverter/conversion.rb
Overview
Public: Convert documents using Docverter
Examples
Docverter::Conversion.run("markdown", "html", "Some Content")
# => "<html><body><p>Some Content</p></body></html>"
Docverter::Conversion.run do |c|
c.from = "markdown"
c.to = "html"
c.content = "Some Content"
c.stylesheet = "stylesheet.css"
c.add_other_file "stylesheet.css"
end
# => '<html><head><link rel="stylesheet" media="print" href="stylesheet.css"></head><body><p>Some Content</p></body></html>'
Class Method Summary collapse
-
.pickup(id) ⇒ Object
Public: Pick up the converted document for a particular conversion.
-
.run(from = nil, to = nil, content = nil) ⇒ Object
Public: Run a conversion on Docverter.
-
.status(id) ⇒ Object
Public: Get the status of a particular conversion.
Instance Method Summary collapse
-
#add_input_file(path) ⇒ Object
Public: Add an input file to the conversion.
-
#add_other_file(path) ⇒ Object
Public: Add another file to the conversion.
-
#convert ⇒ Object
Public: Run the conversion through Docverter.
-
#initialize(from = nil, to = nil, content = nil) ⇒ Conversion
constructor
Public: Run a conversion on Docverter.
-
#to_h ⇒ Object
Internal: Convert this conversion into a hash.
Constructor Details
#initialize(from = nil, to = nil, content = nil) ⇒ Conversion
Public: Run a conversion on Docverter
from - The format of the input text (optional) to - The format of the output document (optional) content - The content to be converted (optional)
Can accept a block that will be passed the initialized Conversion object to allow more settings to be set before the conversion is run
Returns a new Conversion object
85 86 87 88 89 90 91 92 93 |
# File 'lib/docverter/conversion.rb', line 85 def initialize(from=nil, to=nil, content=nil) super( :from => from, :to => to, :content => content, :input_files => [], :other_files => [] ) end |
Class Method Details
.pickup(id) ⇒ Object
Public: Pick up the converted document for a particular conversion
id - The integer ID of a conversion as given by Docverter::Conversion#run in async mode
Returns the converted document
71 72 73 |
# File 'lib/docverter/conversion.rb', line 71 def self.pickup(id) Docverter.request(:get, "/pickup/#{id.to_i}") end |
.run(from = nil, to = nil, content = nil) ⇒ Object
Public: Run a conversion on Docverter
from - The format of the input text (optional) to - The format of the output document (optional) content - The content to be converted (optional)
Yields the initialized Conversion object
Returns the converted document or a status hash if a callback_url has been specified
Examples
Docverter::Conversion.run("markdown", "pdf", "Some Content")
Docverter::Conversion.run do |c|
c.from = "markdown"
c.to = "pdf"
c.add_input_file("input.md")
c.stylesheet = "stylesheet.css"
c.add_other_file "stylesheet.css"
end
47 48 49 50 51 52 53 54 55 |
# File 'lib/docverter/conversion.rb', line 47 def self.run(from=nil, to=nil, content=nil) obj = new(from, to, content) if block_given? yield obj end obj.convert end |
.status(id) ⇒ Object
Public: Get the status of a particular conversion
id - The integer ID of a conversion as given by Docverter::Conversion#run in async mode
Returns a status hash
62 63 64 |
# File 'lib/docverter/conversion.rb', line 62 def self.status(id) OkJson.decode(Docverter.request(:get, "/status/#{id.to_i}")) end |
Instance Method Details
#add_input_file(path) ⇒ Object
Public: Add an input file to the conversion
path - The path to the file to add as an input file
98 99 100 |
# File 'lib/docverter/conversion.rb', line 98 def add_input_file(path) self.input_files << File.open(path, "rb") end |
#add_other_file(path) ⇒ Object
Public: Add another file to the conversion
path - The path to the file to add
105 106 107 |
# File 'lib/docverter/conversion.rb', line 105 def add_other_file(path) self.other_files << File.open(path, "rb") end |
#convert ⇒ Object
Public: Run the conversion through Docverter
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/docverter/conversion.rb', line 110 def convert if input_files.length == 0 && content == nil raise InvalidConversionError.new("Cannot convert without an input_file or content") end unless self.content.nil? temp = Tempfile.new("temp") temp.write self.content temp.flush self.add_input_file(temp.path) self.content = nil end response = Docverter.request(:post, "/convert", self.to_h) if self.callback_url OkJson.decode(response) else response end end |
#to_h ⇒ Object
Internal: Convert this conversion into a hash
133 134 135 136 137 138 139 140 141 |
# File 'lib/docverter/conversion.rb', line 133 def to_h hash = {} self.instance_variable_get(:@table).each do |field,value| next if value.nil? hash[field] = value end hash end |