poleica 0.9.11 → 0.9.12

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- YzM2ZTViOTU5Zjk5OWZkNDM5MjAwZGNmYzc5YTJkYTg5MjY1ZTZmMw==
4
+ MWJhYzFiNTA4OWRkNTIyZmQ0MDNiODc4YzVmN2I3ZDk5ODViZTE0YQ==
5
5
  data.tar.gz: !binary |-
6
- ZjY1YmMzYzUyYWNkNDYyYmZkYzhkYTA5OTU0OWIzMjI2Mjk2ZTc0Zg==
6
+ MWZjMGQ2OWM3OTcxZjIzN2M1NGJmNWFmM2ViZWFjZmQ2ODk4NDkyYw==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- ZmJlNmNlZjAxOWZlNDVlYTg5ZWYxZTRiZWQ1NDVjNzllMzBiYTg2MTcyY2Q4
10
- ZjczZWEwMWY3YzhjZDE0NDk2OTg3NzNiYmQ2YmRjNzVkOGRlMDUzNjU5ZTgz
11
- YzIwZWNhNTlkNzkzZjE0MWZlZDQwNjUzZTA5NWQ2ZmI0NWJhNjQ=
9
+ M2FhZGE2MjI4OTA2ZGI1MzViMmZhYWVkNGJlNDRlZTY3OGZmNmE4ZTNmZjE0
10
+ YTNkOGM1MDYxNzdhYTE5ODVmYzkxYjViNzQ2NjQzNDJmMWRlMThkY2QzZTVm
11
+ YzFmY2FkMmYxNzQ2MmFmYjQ4N2IzN2QyYzQzMjYwNzVlNmIxZjM=
12
12
  data.tar.gz: !binary |-
13
- OTQxMzg5NDBlNWI1YzUxM2NmMDM4MDg1MTVmN2ExZmViMTE4YmVkOTYzZDcy
14
- MTQyYjY2NzM5NmM4NWJiNmNkMjgzNzAwMzJjNjIwMDAwYzk0OGI2N2ZiOThi
15
- NDBmNmU5Y2UyOWYxMjA0NTg4ZWI0MGM3YzY5YWYxZGExMTdjY2M=
13
+ ZTg0YmU3MmJjZTg5MTllMTQ5MGRhYzdhZjgwMTc5Y2VjNmRiMTQ2NTA4NWI3
14
+ M2U3ODVjNzM5ODBlNWE5YjUzMjFkMzA3MjMwNDBmYWYxNmZlNzY2YmJlYzMy
15
+ Nzk3M2EwMmZlMjY1Mzc4NDZlN2RhMTY1YWY2NzM1OTg4ZDdhN2E=
data/Gemfile.lock CHANGED
@@ -17,7 +17,7 @@ GIT
17
17
  PATH
18
18
  remote: .
19
19
  specs:
20
- poleica (0.9.11)
20
+ poleica (0.9.12)
21
21
  childprocess
22
22
 
23
23
  GEM
data/README.md CHANGED
@@ -46,6 +46,17 @@ Poleica.new(file_path).to_png
46
46
  Poleica.new(file_path).to_png(width: 400, weight: 400)
47
47
  ```
48
48
 
49
+ ## Configuration
50
+
51
+ Example :
52
+
53
+ ```
54
+ Poleica.configure do |config|
55
+ config.timeout = 2 # time in secs
56
+ config.libre_office[:bin_paths][:linux] = '/usr/local/bin/gm'
57
+ config.graphics_magick[:bin_paths][:osx] = '/Applications/OpenOffice.app/Contents/MacOS/soffice.bin'
58
+ ```
59
+
49
60
  ## Options
50
61
 
51
62
  - weight : pixels number
@@ -2,12 +2,34 @@
2
2
  module Poleica
3
3
  # Configuration class
4
4
  class Configuration
5
- attr_accessor :timeout
5
+ attr_accessor :timeout, :libre_office, :graphics_magick
6
6
 
7
7
  def initialize
8
- self.timeout = 120
8
+ self.timeout = 120
9
+ self.libre_office = init_libre_office_config
10
+ self.graphics_magick = init_graphics_magick_config
11
+ end
12
+
13
+ def init_graphics_magick_config
14
+ {
15
+ bin_paths:
16
+ {
17
+ linux: '/usr/local/bin/gm',
18
+ osx: '/usr/local/bin/gm'
19
+ }
20
+ }
9
21
  end
10
- end
22
+
23
+ def init_libre_office_config
24
+ {
25
+ bin_paths:
26
+ {
27
+ linux: '/usr/lib/libreoffice/program/soffice.bin',
28
+ osx: '/Applications/LibreOffice.app/Contents/MacOS/soffice.bin'
29
+ }
30
+ }
31
+ end
32
+ end # class Configuration
11
33
 
12
34
  def self.configure
13
35
  yield(configuration) if block_given?
@@ -16,4 +38,8 @@ module Poleica
16
38
  def self.configuration
17
39
  @configuration ||= Configuration.new
18
40
  end
41
+
42
+ def self.reset_configuration
43
+ @configuration = Configuration.new
44
+ end
19
45
  end # module Poleica
@@ -6,11 +6,6 @@ module Poleica
6
6
  class GraphicsMagick
7
7
  include Poleica::Converters::Utils
8
8
 
9
- BIN_PATHS = {
10
- linux: '/usr/local/bin/gm',
11
- osx: '/usr/local/bin/gm'
12
- }
13
-
14
9
  COMPATIBLE_TYPES = [
15
10
  Types::Image,
16
11
  Types::PDF
@@ -7,11 +7,6 @@ module Poleica
7
7
  class LibreOffice
8
8
  include Poleica::Converters::Utils
9
9
 
10
- BIN_PATHS = {
11
- linux: '/usr/lib/libreoffice/program/soffice.bin',
12
- osx: '/Applications/LibreOffice.app/Contents/MacOS/soffice.bin'
13
- }
14
-
15
10
  COMPATIBLE_TYPES = [
16
11
  Types::Document
17
12
  ]
@@ -6,6 +6,7 @@ module Poleica
6
6
  module Converters
7
7
  # An Utility module for the converters needs to be include
8
8
  module Utils
9
+
9
10
  HOST_OS ||= (defined?('RbConfig') ? RbConfig : Config)::CONFIG['host_os']
10
11
 
11
12
  def windows?
@@ -25,13 +26,21 @@ module Poleica
25
26
  end
26
27
 
27
28
  def bin_path
28
- bin_path_key = :BIN_PATHS # rubocop:disable SymbolName
29
- bin_paths = self.class.const_get(bin_path_key)
30
- path = bin_paths[host_os] || bin_paths[:linux]
29
+ converter = :"#{underscorize(self.class)}"
30
+ configuration = Poleica.configuration.send(converter)
31
+ bin_paths = configuration[:bin_paths]
32
+ path = bin_paths[host_os] || bin_paths[:linux]
31
33
  raise "#{self.class} not found @ #{path}" unless File.exists?(path)
32
34
  path
33
35
  end
34
36
 
37
+ def underscorize(camel_cased_word)
38
+ word = camel_cased_word.to_s
39
+ word = word[((word.rindex('::') + 2) || 0)..-1]
40
+ word.gsub!(/([A-Z]+)/, '_\1').gsub!(/^_/, '').downcase!
41
+ word
42
+ end
43
+
35
44
  module_function
36
45
 
37
46
  def extract_extension_and_options(method, args = [])
@@ -41,17 +50,25 @@ module Poleica
41
50
  [extension, options]
42
51
  end
43
52
 
44
- def exec_with_timeout(bin, args = [], timeout = nil, is_stdout = true)
45
- args = Array(args).map(&:to_s)
46
- timeout ||= Poleica.configuration.timeout
47
- null = IO.new(IO.sysopen('/dev/null', 'w'), 'w')
48
- process = ChildProcess.build(bin, *args).start
49
- process.io.stdout = process.io.stderr = null
50
- process.poll_for_exit(timeout)
53
+ def exec_with_timeout(bin, args = [], timeout = nil, no_stdout = true)
54
+ args = Array(args).map(&:to_s)
55
+ timeout ||= Poleica.configuration.timeout
56
+ process = ChildProcess.build(bin, *args)
57
+ set_process_stdout(process, no_stdout)
58
+ process.start
59
+ timeout ? process.poll_for_exit(timeout) : process.wait
51
60
  rescue ChildProcess::TimeoutError => e
52
- process.stop && raise(e)
53
- ensure
54
- null && null.close
61
+ process.stop
62
+ raise(Poleica::TimeoutError.new(e.message))
63
+ end
64
+
65
+ def set_process_stdout(process, no_stdout)
66
+ if no_stdout
67
+ null = IO.new(IO.sysopen('/dev/null', 'w'), 'w')
68
+ process.io.stdout = process.io.stderr = null
69
+ else
70
+ process.io.inherit!
71
+ end
55
72
  end
56
73
  end # module Utils
57
74
  end # module Converters
@@ -0,0 +1,4 @@
1
+ # -*- encoding: utf-8 -*-
2
+ module Poleica
3
+ class TimeoutError < StandardError; end
4
+ end # module Poleica
@@ -1,4 +1,4 @@
1
1
  # -*- encoding: utf-8 -*-
2
2
  module Poleica
3
- VERSION ||= '0.9.11'
3
+ VERSION ||= '0.9.12'
4
4
  end # module Poleica
data/lib/poleica.rb CHANGED
@@ -8,6 +8,7 @@ end # module Poleica
8
8
 
9
9
  require 'poleica/version'
10
10
  require 'poleica/configuration'
11
+ require 'poleica/errors'
11
12
 
12
13
  require 'poleica/types/pdf'
13
14
  require 'poleica/types/image'
@@ -11,4 +11,28 @@ class ConfigurationTest < Minitest::Test
11
11
  ensure
12
12
  Poleica.configure { |config| config.timeout = 120 }
13
13
  end
14
- end
14
+
15
+ def test_graphics_magick_configuration
16
+ Poleica.configure do |config|
17
+ config.graphics_magick[:bin_paths] = { linux: '', osx: '' }
18
+ end
19
+ expected_message = 'Poleica::Converters::GraphicsMagick not found @ '
20
+ assert_raises(RuntimeError, expected_message) do
21
+ Poleica.new('test/support/files/example.pdf').to_png
22
+ end
23
+ ensure
24
+ Poleica.reset_configuration
25
+ end
26
+
27
+ def test_libre_office_configuration
28
+ Poleica.configure do |config|
29
+ config.libre_office[:bin_paths] = { linux: '', osx: '' }
30
+ end
31
+ expected_message = 'Poleica::Converters::LibreOffice not found @ '
32
+ assert_raises(RuntimeError, expected_message) do
33
+ Poleica.new('test/support/files/example.doc').to_pdf
34
+ end
35
+ ensure
36
+ Poleica.reset_configuration
37
+ end
38
+ end # class ConfigurationTest
@@ -9,6 +9,12 @@ class UtilsTest < Minitest::Test
9
9
  assert(duration < 2)
10
10
  end
11
11
 
12
+ def test_exec_with_timeout_raise_an_exception
13
+ assert_raises(Poleica::TimeoutError) do
14
+ Poleica::Converters::Utils.exec_with_timeout('sleep', 2, 0.1)
15
+ end
16
+ end
17
+
12
18
  def test_exec_with_timeout_kill
13
19
  assert(`ps -a | grep soffice.bin | grep -v grep`.empty?)
14
20
  Poleica.configure { |conf| conf.timeout = 0.001 }
data/test/test_helper.rb CHANGED
@@ -35,4 +35,4 @@ module Support
35
35
  path_without_extension(original_file_path) +
36
36
  "-#{digest}.#{converted_extension}"
37
37
  end
38
- end
38
+ end # module Support
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: poleica
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.11
4
+ version: 0.9.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - Antoine Lyset
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-09-06 00:00:00.000000000 Z
11
+ date: 2013-09-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  requirement: !ruby/object:Gem::Requirement
@@ -76,6 +76,7 @@ files:
76
76
  - lib/poleica/converters/graphics_magick.rb
77
77
  - lib/poleica/converters/libre_office.rb
78
78
  - lib/poleica/converters/utils.rb
79
+ - lib/poleica/errors.rb
79
80
  - lib/poleica/pathable.rb
80
81
  - lib/poleica/polei.rb
81
82
  - lib/poleica/types/all.rb