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 +4 -4
- data/README.md +13 -1
- data/lib/sumi/conversion.rb +42 -0
- data/lib/sumi/ruby/version.rb +1 -1
- data/lib/sumi/ruby.rb +2 -0
- metadata +2 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: b48d59134097ed8d4ed5b68ac008461a4017cc60f6ed0b0a2339e15e7be31326
|
|
4
|
+
data.tar.gz: ebb9023421ccb1c1963217ed1693aaa13b814cc77772aa6b78e92e1cee24a8c7
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
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
|
|
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
|
data/lib/sumi/ruby/version.rb
CHANGED
data/lib/sumi/ruby.rb
CHANGED
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.
|
|
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
|