gs-ruby 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 710d42cff4fab03289dec94206bb93fa01e1cc95
4
+ data.tar.gz: 5852d0e75787dc52e2b0adc69abb2e2ab1ccbdd7
5
+ SHA512:
6
+ metadata.gz: 4641486100cf060cea46ab15848f9765284c9bd23f74dcccb1f5fd324a0173395eb28e1360121cb10e5485c05960a17fae29642958503430a7bed3ae1b9f2e08
7
+ data.tar.gz: dffd54e534ded3783bbcc776b73a5879618a762265c9efc7845f3fae6d1f0c0ad73a7f0a8351c141d284a7a16c20cbcaf712a095f4281a1098402bbf5042a491
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.rubocop.yml ADDED
@@ -0,0 +1,11 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.4
3
+
4
+ Style/FileName:
5
+ Exclude:
6
+ - 'lib/gs-ruby.rb'
7
+ - 'spec/gs-ruby_spec.rb'
8
+
9
+ Metrics/BlockLength:
10
+ Exclude:
11
+ - 'spec/**/*'
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.0
5
+ before_install: gem install bundler -v 1.13.7
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+ source 'https://rubygems.org'
3
+
4
+ ruby '2.4.0'
5
+
6
+ # Specify your gem's dependencies in gs-ruby.gemspec
7
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Lawrance Shepstone
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,81 @@
1
+ # GS Ruby
2
+
3
+ Simple wrapper for the [Ghostscript](https://www.ghostscript.com/) command – it's assumed that you have the `gs` command installed.
4
+
5
+ **Note:** This was developed against version _9.20_ of the Ghostscript command and is a work-in-progress. No doubt there are some issues (perhaps even major ones – namespacing, for instance?).
6
+
7
+ For more detailed documentation, take a look at the source docs – http://www.rubydoc.info/github/lshepstone/gs-ruby
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'gs-ruby'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install gs-ruby
24
+
25
+ ## Usage
26
+
27
+ Typical usage might look like:
28
+
29
+ ```ruby
30
+ GS.run('input.ps', GS::OUTPUT_FILE => 'output.pdf')
31
+ ```
32
+
33
+ Or using a block to work with the command before it's run:
34
+
35
+ ```ruby
36
+ GS.run('input.ps') do |command|
37
+ command.option(GS::OUTPUT_FILE, 'output.pdf')
38
+ end
39
+ ```
40
+
41
+ #### Configuration
42
+
43
+ Global configuration is possible:
44
+
45
+ ```ruby
46
+ GS.configure do |config|
47
+ # ...
48
+ end
49
+ ```
50
+
51
+ #### Logging
52
+
53
+ By default all output is logged to `$stdout`, but the logger can be configured:
54
+
55
+ ```ruby
56
+ # For a single command instance.
57
+ GS.run('input.ps') do |command|
58
+ # command.logger =
59
+ end
60
+
61
+ # For all command instances.
62
+ GS.configure do |config|
63
+ # config.logger =
64
+ end
65
+ ```
66
+
67
+ ## Development
68
+
69
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
70
+
71
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
72
+
73
+ ## Contributing
74
+
75
+ Bug reports and pull requests are welcome on GitHub at https://github.com/lshepstone/gs-ruby.
76
+
77
+ ## License
78
+
79
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
80
+
81
+ Copyright (c) 2017 Lawrance Shepstone
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+ require 'bundler/gem_tasks'
3
+ require 'rspec/core/rake_task'
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'gs-ruby'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start
data/bin/rake ADDED
@@ -0,0 +1,17 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+ #
4
+ # This file was generated by Bundler.
5
+ #
6
+ # The application 'rake' is installed as part of a gem, and
7
+ # this file is here to facilitate running it.
8
+ #
9
+
10
+ require 'pathname'
11
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile',
12
+ Pathname.new(__FILE__).realpath)
13
+
14
+ require 'rubygems'
15
+ require 'bundler/setup'
16
+
17
+ load Gem.bin_path('rake', 'rake')
data/bin/rspec ADDED
@@ -0,0 +1,17 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+ #
4
+ # This file was generated by Bundler.
5
+ #
6
+ # The application 'rspec' is installed as part of a gem, and
7
+ # this file is here to facilitate running it.
8
+ #
9
+
10
+ require 'pathname'
11
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile',
12
+ Pathname.new(__FILE__).realpath)
13
+
14
+ require 'rubygems'
15
+ require 'bundler/setup'
16
+
17
+ load Gem.bin_path('rspec-core', 'rspec')
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/gs-ruby.gemspec ADDED
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ # frozen_string_literal: true
3
+ lib = File.expand_path('../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'gs-ruby/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'gs-ruby'
9
+ spec.version = GS::VERSION
10
+ spec.authors = ['Lawrance Shepstone']
11
+ spec.email = ['lawrance.shepstone@gmail.com']
12
+
13
+ spec.summary = 'GS-Ruby'
14
+ spec.description = 'Simple wrapper for the Ghostscript command'
15
+ spec.homepage = 'https://github.com/lshepstone/gs-ruby'
16
+ spec.license = 'MIT'
17
+
18
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
19
+ f.match(%r{^(test|spec|features)/})
20
+ end
21
+ spec.bindir = 'exe'
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ['lib']
24
+
25
+ spec.add_development_dependency 'bundler', '~> 1.13'
26
+ spec.add_development_dependency 'rake', '~> 10.0'
27
+ spec.add_development_dependency 'rspec', '~> 3.0'
28
+ spec.add_development_dependency 'rubocop', '~> 0'
29
+ spec.add_development_dependency 'rubocop-rspec', '~> 0'
30
+ end
data/lib/gs-ruby.rb ADDED
@@ -0,0 +1,91 @@
1
+ # frozen_string_literal: true
2
+ require 'gs-ruby/command'
3
+ require 'gs-ruby/configuration'
4
+ require 'gs-ruby/options'
5
+ require 'gs-ruby/version'
6
+
7
+ # GS Ruby is a simple wrapper for the Ghostscript command - it's assumed
8
+ # that you have the +gs+ command already installed.
9
+ #
10
+ # == Usage
11
+ #
12
+ # Typical usage might look like:
13
+ #
14
+ # GS.run('input.ps', GS::OUTPUT_FILE => 'output.pdf')
15
+ #
16
+ # Or using a block to work with the command before it's run:
17
+ #
18
+ # GS.run('input.ps') do |command|
19
+ # command.option(GS::OUTPUT_FILE, 'output.pdf')
20
+ # end
21
+ #
22
+ # == Configuration
23
+ #
24
+ # Global configuration is possible:
25
+ #
26
+ # GS.configure do |config|
27
+ # config.bin_path = '/path/to/gs'
28
+ # end
29
+ #
30
+ # == Logging
31
+ #
32
+ # By default all output is logged to +$stdout+, but the logger can be
33
+ # configured:
34
+ #
35
+ # # For a single command instance.
36
+ # GS.run('input.ps') do |command|
37
+ # command.logger = # ...
38
+ # end
39
+ #
40
+ # # For all command instances.
41
+ # GS.configure do |config|
42
+ # config.logger = # ...
43
+ # end
44
+ #
45
+ # @see GS.run
46
+ # @see GS.configure
47
+ module GS
48
+ include Options
49
+
50
+ module_function
51
+
52
+ # Gets the configuration object.
53
+ #
54
+ # If none was set, a new configuration object is instantiated and returned.
55
+ #
56
+ # @return [Configuration] the configuration object
57
+ def configuration
58
+ @configuration ||= Configuration.new
59
+ end
60
+
61
+ # Allows for configuring the library using a block.
62
+ #
63
+ # @example Configuration using a block
64
+ # GS.configure do |config|
65
+ # # ...
66
+ # end
67
+ def configure
68
+ yield(configuration) if block_given?
69
+ end
70
+
71
+ # Instantiates a new command object with any provided options and runs it
72
+ # with the provided input.
73
+ #
74
+ # @example Run a new command with options
75
+ # GS.run('input.ps', GS::OUTPUT_FILE => 'output.pdf')
76
+ #
77
+ # @example Configure and run a new command
78
+ # GS.run('path/to/input/file.ps') do |command|
79
+ # command.option(GS::OUTPUT_FILE, 'output.pdf')
80
+ # end
81
+ #
82
+ # @return [Process::Status] describing the result of running the command
83
+ #
84
+ # @see Command#option
85
+ # @see Command#run
86
+ def run(input, options = {})
87
+ command = Command.new(options: options)
88
+ yield(command) if block_given?
89
+ command.run(input)
90
+ end
91
+ end
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+ require 'open3'
3
+
4
+ module GS
5
+ # Command object for running a command with various options.
6
+ #
7
+ # * This is usually not instantiated directly, but rather by way of
8
+ # calling +GS.run+.
9
+ class Command
10
+ attr_reader :configuration,
11
+ :options
12
+
13
+ attr_accessor :logger
14
+
15
+ # Initializes a new command instance.
16
+ #
17
+ # * If no configuration object is passed or is +nil+, the value of
18
+ # +GS.configuration+ is used as the configuration object
19
+ #
20
+ # * Any options passed are merged with +GS.configuration.default_options+
21
+ # so that any option can be deleted if necessary
22
+ #
23
+ # * If no logger object is passed or is +nil+, the value of
24
+ # +GS.configuration.logger+ is used as the logger object
25
+ #
26
+ # @param [Configuration] configuration the configuration object
27
+ # @param [Hash] options the key-value pairs of command options
28
+ # @param [Logger] logger the logger object
29
+ def initialize(configuration: nil, options: {}, logger: nil)
30
+ @configuration = configuration || GS.configuration
31
+ @options = @configuration.default_options.merge(options)
32
+ @logger = logger || @configuration.logger
33
+ end
34
+
35
+ # Sets a command option with an optional value.
36
+ #
37
+ # * If a value is passed, the resulting switch will be in the form
38
+ # +-sName=Value+. If no value is passed, the resulting switch will be
39
+ # in the form +-dName+.
40
+ #
41
+ # @param name [String] the name of the option
42
+ # @param value [Object] the value of the option
43
+ def option(name, value = nil)
44
+ options[name] = value
45
+ end
46
+
47
+ # Executes the command with the specified input.
48
+ #
49
+ # @param [String] inputs the input arguments to the command
50
+ #
51
+ # @return [Process::Status] describing the result of running the command
52
+ def run(inputs)
53
+ command = "#{configuration.bin_path} #{build_switches} #{inputs}"
54
+
55
+ status = nil
56
+ logger.info(command)
57
+ Open3.popen2e(command) do |_, stdout_and_stderr, wait_thr|
58
+ logger.info(stdout_and_stderr.read)
59
+ status = wait_thr.value
60
+ end
61
+
62
+ status
63
+ end
64
+
65
+ private
66
+
67
+ def build_switches
68
+ options
69
+ .map { |(n, v)| v ? "-s#{n}=#{v}" : "-d#{n}" }
70
+ .join(' ')
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,68 @@
1
+ # frozen_string_literal: true
2
+ require 'logger'
3
+
4
+ module GS
5
+ # A configuration object used to define the path to the Ghostscript command,
6
+ # any default options and the logger that should be used.
7
+ #
8
+ # * This is usually not instantiated directly, but rather by way of
9
+ # calling +GS.configure+.
10
+ class Configuration
11
+ attr_writer :bin_path,
12
+ :default_options,
13
+ :logger
14
+
15
+ # Gets the bin path to the Ghostscript command.
16
+ #
17
+ # * If no path has been set, it returns +'gs'+ as the default.
18
+ #
19
+ # @return [String] the path to the Ghostscript command
20
+ def bin_path
21
+ @bin_path ||= 'gs'
22
+ end
23
+
24
+ # Gets the default options.
25
+ #
26
+ # * If no default options have been set, it returns a set of options
27
+ # where +GS::BATCH+ is set by default.
28
+ #
29
+ # @example Configuring a default option that requires a value
30
+ # GS.configure do |config|
31
+ # # Results in the switch -sProcessColorModel=DeviceCMYK
32
+ # config.default_options[GS::PROCESS_COLOR_MODEL] = 'DeviceCMYK'
33
+ # end
34
+ #
35
+ # @example Configuring a default option that does not require a value
36
+ # GS.configure do |config|
37
+ # # Results in the switch -dNOPAUSE
38
+ # config.default_options[GS::NO_PAUSE] = nil
39
+ # end
40
+ #
41
+ # @example Removing a default option that already exists
42
+ # GS.configure do |config|
43
+ # # Currently, GS::BATCH is the only option configured by default.
44
+ # # Not sure why you might want to remove it, but if you do, simply
45
+ # # delete it from the options Hash - same goes for any other option.
46
+ # config.default_options.delete(GS::BATCH)
47
+ # end
48
+ #
49
+ # @return [Hash] the default options and their values
50
+ #
51
+ # @see Command#option
52
+ def default_options
53
+ @default_options ||= { GS::BATCH => nil }
54
+ end
55
+
56
+ # Gets the logger object.
57
+ #
58
+ # * If no logger has been set, it instantiates and returns a new logger
59
+ # configured to log to +$stdout+.
60
+ #
61
+ # @return [Logger] the logger instance used for logging
62
+ def logger
63
+ @logger ||= Logger.new($stdout).tap do |logger|
64
+ logger.progname = bin_path
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+ module GS
3
+ module Options
4
+ PDFA = 'PDFA'
5
+ BATCH = 'BATCH'
6
+ NO_PAUSE = 'NOPAUSE'
7
+ DEVICE = 'DEVICE'
8
+ PROCESS_COLOR_MODEL = 'ProcessColorModel'
9
+ PDFA_COMPATIBILITY_POLICY = 'PDFACompatibilityPolicy'
10
+ OUTPUT_FILE = 'OutputFile'
11
+ end
12
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+ module GS
3
+ VERSION = '0.1.0'
4
+ end
metadata ADDED
@@ -0,0 +1,132 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gs-ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Lawrance Shepstone
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-03-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.13'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop-rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Simple wrapper for the Ghostscript command
84
+ email:
85
+ - lawrance.shepstone@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".rubocop.yml"
93
+ - ".travis.yml"
94
+ - Gemfile
95
+ - LICENSE.txt
96
+ - README.md
97
+ - Rakefile
98
+ - bin/console
99
+ - bin/rake
100
+ - bin/rspec
101
+ - bin/setup
102
+ - gs-ruby.gemspec
103
+ - lib/gs-ruby.rb
104
+ - lib/gs-ruby/command.rb
105
+ - lib/gs-ruby/configuration.rb
106
+ - lib/gs-ruby/options.rb
107
+ - lib/gs-ruby/version.rb
108
+ homepage: https://github.com/lshepstone/gs-ruby
109
+ licenses:
110
+ - MIT
111
+ metadata: {}
112
+ post_install_message:
113
+ rdoc_options: []
114
+ require_paths:
115
+ - lib
116
+ required_ruby_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ required_rubygems_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ requirements: []
127
+ rubyforge_project:
128
+ rubygems_version: 2.6.8
129
+ signing_key:
130
+ specification_version: 4
131
+ summary: GS-Ruby
132
+ test_files: []