sumi-ruby 0.2.0 → 0.2.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f9316b4bb6bf6bef389065fc6c958137747810f368093293951a9e78534ad993
4
- data.tar.gz: 0d06594b46fe42130c8c352ae33a7893f1fb4ba5c7e7867b18ef2e05638f7676
3
+ metadata.gz: b48d59134097ed8d4ed5b68ac008461a4017cc60f6ed0b0a2339e15e7be31326
4
+ data.tar.gz: ebb9023421ccb1c1963217ed1693aaa13b814cc77772aa6b78e92e1cee24a8c7
5
5
  SHA512:
6
- metadata.gz: 4a1728eddcdb67b32ae71ccd191282c81129847b4386b0e5e37142ae33e6581e3732d9d31477f3ead5f02348ef1332e7dfcf134bcb333c661bed29c22efb4c87
7
- data.tar.gz: 6cd2df269185d77558cf8614c8eb07f9402abfffa446d863b3b634e4dda6458ffccd7b2a26588d074085ca2a37cb1ae057c92ebe441899fd37f56cc16e7fdd46
6
+ metadata.gz: dfda0e889f94a1345913978fcb7efbad43bf51b1c87e78e0e9a229dbc433a1cad8280614ced9e4e65c4a304c994e422394c370a372e55846b666cd50c899bf19
7
+ data.tar.gz: '086cef5f1cb634d4d80df31adff8a8224de8b97c26c2674deb521275f5bd6557c552ff0f52bbe346b66bee23c2be5a75742ef8e8549e08c0de86af048e2ad188'
data/README.md CHANGED
@@ -22,7 +22,19 @@ bundle install
22
22
  bundle exec sumi input.pdf -o output.pdf
23
23
  ```
24
24
 
25
- Ruby からは `Sumi::Ruby.executable` で実行ファイルの絶対パスを取得できます。
25
+ Ruby からは `Sumi.convert` で、PDF のバイト列を変換できます。
26
+ キーワード引数は sumi のオプションになります。`gray_model: "colorimetric"` は `--gray-model colorimetric`、`strict: true` は `--strict` として渡し、`false` や `nil` のオプションは渡しません。
27
+ sumi が失敗すると、sumi のエラー出力をメッセージにした `Sumi::ConversionError` を投げます。
28
+
29
+ ```ruby
30
+ require "sumi/ruby"
31
+
32
+ pdf = File.binread("input.pdf")
33
+ gray = Sumi.convert(pdf, gray_model: "colorimetric", strict: true)
34
+ File.binwrite("output.pdf", gray)
35
+ ```
36
+
37
+ ファイルを直接指定したい場合や、`--report json` の出力を受け取りたい場合は、`Sumi::Ruby.executable` で実行ファイルの絶対パスを取得して実行します。
26
38
 
27
39
  ```ruby
28
40
  require "open3"
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "open3"
4
+
5
+ module Sumi
6
+ class ConversionError < Ruby::Error; end
7
+
8
+ class << self
9
+ # Converts the PDF in `pdf` (a String of its bytes) and returns the converted PDF as a String.
10
+ #
11
+ # Each keyword becomes a sumi option: `gray_model: "colorimetric"` is passed as
12
+ # `--gray-model colorimetric`, `strict: true` as `--strict`, and `false` or `nil` leaves the
13
+ # option out. The keywords are not checked here, so options added to sumi can be used without
14
+ # updating this gem; sumi itself rejects unknown ones.
15
+ #
16
+ # Raises ConversionError with sumi's error output when sumi exits with a non-zero status.
17
+ def convert(pdf, **options)
18
+ output, error, status = Open3.capture3(Ruby.executable, "-", "-o", "-", *command_options(options),
19
+ stdin_data: pdf, binmode: true)
20
+ unless status.success?
21
+ message = error.strip
22
+ message = "sumi exited with status #{status.exitstatus}" if message.empty?
23
+ raise ConversionError, message
24
+ end
25
+
26
+ output
27
+ end
28
+
29
+ private
30
+
31
+ def command_options(options)
32
+ options.flat_map do |name, value|
33
+ flag = "--#{name.to_s.tr("_", "-")}"
34
+ case value
35
+ when true then [flag]
36
+ when false, nil then []
37
+ else [flag, value.to_s]
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -3,6 +3,6 @@
3
3
  module Sumi
4
4
  module Ruby
5
5
  # Always the same as the version of the bundled sumi CLI (`Cargo.toml`).
6
- VERSION = "0.2.0"
6
+ VERSION = "0.2.1"
7
7
  end
8
8
  end
data/lib/sumi/ruby.rb CHANGED
@@ -70,3 +70,5 @@ module Sumi
70
70
  end
71
71
  end
72
72
  end
73
+
74
+ require_relative "conversion"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sumi-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Junya Ishihara
@@ -22,6 +22,7 @@ files:
22
22
  - README.md
23
23
  - exe/sumi
24
24
  - lib/sumi-ruby.rb
25
+ - lib/sumi/conversion.rb
25
26
  - lib/sumi/ruby.rb
26
27
  - lib/sumi/ruby/platforms.rb
27
28
  - lib/sumi/ruby/version.rb