office_converter 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +8 -8
 - data/lib/office_converter.rb +13 -10
 - data/lib/office_converter/version.rb +1 -1
 - metadata +1 -1
 
    
        checksums.yaml
    CHANGED
    
    | 
         @@ -1,15 +1,15 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            ---
         
     | 
| 
       2 
2 
     | 
    
         
             
            !binary "U0hBMQ==":
         
     | 
| 
       3 
3 
     | 
    
         
             
              metadata.gz: !binary |-
         
     | 
| 
       4 
     | 
    
         
            -
                 
     | 
| 
      
 4 
     | 
    
         
            +
                MDE2ZTBhZDNkZjNiZTVmN2U0ZjNmYjc5NjUxNWYwNjdhMmFiM2QzMg==
         
     | 
| 
       5 
5 
     | 
    
         
             
              data.tar.gz: !binary |-
         
     | 
| 
       6 
     | 
    
         
            -
                 
     | 
| 
      
 6 
     | 
    
         
            +
                MTc5M2QwYWFiNjY5ZjY0ODU5OTI5MzcyMGU5ZjQxZjNmOGFhNThmYQ==
         
     | 
| 
       7 
7 
     | 
    
         
             
            SHA512:
         
     | 
| 
       8 
8 
     | 
    
         
             
              metadata.gz: !binary |-
         
     | 
| 
       9 
     | 
    
         
            -
                 
     | 
| 
       10 
     | 
    
         
            -
                 
     | 
| 
       11 
     | 
    
         
            -
                 
     | 
| 
      
 9 
     | 
    
         
            +
                NGM3ZGI4YTg4ZmU2YzgzMDFiMGQ1MjllNjY4N2MxZDgzN2EyMjNlNWRlOWJm
         
     | 
| 
      
 10 
     | 
    
         
            +
                YTEyNDQ0MzQzMTUwMTI3MjI4MDE5OWYxZTk1MTI5NWUxZGVkMWE0NGYzZTFl
         
     | 
| 
      
 11 
     | 
    
         
            +
                N2U3MTY5NjY5NGEwYzQ3NTM1YmVjYTNhNjQyZjBlYzRlYmEzZGE=
         
     | 
| 
       12 
12 
     | 
    
         
             
              data.tar.gz: !binary |-
         
     | 
| 
       13 
     | 
    
         
            -
                 
     | 
| 
       14 
     | 
    
         
            -
                 
     | 
| 
       15 
     | 
    
         
            -
                 
     | 
| 
      
 13 
     | 
    
         
            +
                YWY1ZjMxZWI2NGM4N2M5ZTE1MTBlODQ4ZmRlMDQzYWVmYzUwNmZjZDQ0MTky
         
     | 
| 
      
 14 
     | 
    
         
            +
                MmQwZmYwNTZlZDUxMmNiNGVjMmFjNTYwNDFkM2M2MDZkODkwYWE1MTFjMmNm
         
     | 
| 
      
 15 
     | 
    
         
            +
                YTYxZThkNjBjZTlkNTRlYjdiMzBlNzY2N2E2MTBmM2RmZjFjMDg=
         
     | 
    
        data/lib/office_converter.rb
    CHANGED
    
    | 
         @@ -1,17 +1,24 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            require "office_converter/version"
         
     | 
| 
      
 2 
     | 
    
         
            +
            require "uri"
         
     | 
| 
      
 3 
     | 
    
         
            +
            require "net/http"
         
     | 
| 
      
 4 
     | 
    
         
            +
            require "tmpdir"
         
     | 
| 
      
 5 
     | 
    
         
            +
            require "spoon"
         
     | 
| 
       2 
6 
     | 
    
         | 
| 
       3 
7 
     | 
    
         
             
            module OfficeConverter
         
     | 
| 
       4 
     | 
    
         
            -
             
     | 
| 
       5 
     | 
    
         
            -
             
     | 
| 
      
 8 
     | 
    
         
            +
            =begin
         
     | 
| 
      
 9 
     | 
    
         
            +
              source -> .docx file to be converted
         
     | 
| 
      
 10 
     | 
    
         
            +
              target_dir -> directory where file will pe persisted
         
     | 
| 
      
 11 
     | 
    
         
            +
            =end
         
     | 
| 
      
 12 
     | 
    
         
            +
              def self.convert(source, target_dir, soffice_command = nil, convert_to = nil)
         
     | 
| 
      
 13 
     | 
    
         
            +
                Converter.new(source, target_dir, soffice_command, convert_to).convert
         
     | 
| 
       6 
14 
     | 
    
         
             
              end
         
     | 
| 
       7 
15 
     | 
    
         | 
| 
       8 
16 
     | 
    
         
             
              class Converter
         
     | 
| 
       9 
17 
     | 
    
         
             
                attr_accessor :soffice_command
         
     | 
| 
       10 
18 
     | 
    
         | 
| 
       11 
     | 
    
         
            -
                def initialize(source,  
     | 
| 
      
 19 
     | 
    
         
            +
                def initialize(source, target_dir, soffice_command = nil, convert_to = nil)
         
     | 
| 
       12 
20 
     | 
    
         
             
                  @source = source
         
     | 
| 
       13 
     | 
    
         
            -
                  @ 
     | 
| 
       14 
     | 
    
         
            -
                  @target_path = Dir.tmpdir
         
     | 
| 
      
 21 
     | 
    
         
            +
                  @target_dir = target_dir
         
     | 
| 
       15 
22 
     | 
    
         
             
                  @soffice_command = soffice_command
         
     | 
| 
       16 
23 
     | 
    
         
             
                  @convert_to = convert_to || "pdf"
         
     | 
| 
       17 
24 
     | 
    
         
             
                  determine_soffice_command
         
     | 
| 
         @@ -25,11 +32,9 @@ module OfficeConverter 
     | 
|
| 
       25 
32 
     | 
    
         
             
                def convert
         
     | 
| 
       26 
33 
     | 
    
         
             
                  orig_stdout = $stdout.clone
         
     | 
| 
       27 
34 
     | 
    
         
             
                  $stdout.reopen File.new('/dev/null', 'w')
         
     | 
| 
       28 
     | 
    
         
            -
                  pid = Spoon.spawnp("HOME=/tmp" 
     | 
| 
      
 35 
     | 
    
         
            +
                  pid = Spoon.spawnp("export HOME=/tmp && ",@soffice_command, "--headless", "--convert-to", @convert_to, @source, "--outdir", @target_dir)
         
     | 
| 
       29 
36 
     | 
    
         
             
                  Process.waitpid(pid)
         
     | 
| 
       30 
37 
     | 
    
         
             
                  $stdout.reopen orig_stdout
         
     | 
| 
       31 
     | 
    
         
            -
                  target_tmp_file = "#{@target_path}/#{File.basename(@source, ".*")}.#{File.basename(@convert_to, ":*")}"
         
     | 
| 
       32 
     | 
    
         
            -
                  FileUtils.cp target_tmp_file, @target
         
     | 
| 
       33 
38 
     | 
    
         
             
                end
         
     | 
| 
       34 
39 
     | 
    
         | 
| 
       35 
40 
     | 
    
         
             
                private
         
     | 
| 
         @@ -55,8 +60,6 @@ module OfficeConverter 
     | 
|
| 
       55 
60 
     | 
    
         | 
| 
       56 
61 
     | 
    
         
             
                def check_source_type
         
     | 
| 
       57 
62 
     | 
    
         
             
                  return if File.exists?(@source) && !File.directory?(@source) #file
         
     | 
| 
       58 
     | 
    
         
            -
                  return if URI(@source).scheme == "http" && Net::HTTP.get_response(URI(@source)).is_a?(Net::HTTPSuccess) #http
         
     | 
| 
       59 
     | 
    
         
            -
                  return if URI(@source).scheme == "https" && Net::HTTP.get_response(URI(@source)).is_a?(Net::HTTPSuccess) #https
         
     | 
| 
       60 
63 
     | 
    
         
             
                  raise IOError, "Source (#{@source}) is neither a file nor an URL."
         
     | 
| 
       61 
64 
     | 
    
         
             
                end
         
     | 
| 
       62 
65 
     | 
    
         
             
              end
         
     |