Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tempdir option #34

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/active_pdftk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
require 'active_pdftk/field'
require 'active_pdftk/form'
require 'active_pdftk/fdf'
require 'active_pdftk/xfdf'
require 'active_pdftk/xfdf'
15 changes: 12 additions & 3 deletions lib/active_pdftk/call.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ def initialize(args = {})
#
# As you know command line programs take often a bunch of arguments, in a very specific syntax. that is our case.
# In order to call pdftk in a easy way, we build a DSL, it has a hash patern, with four keys : input, operation, output, options.
# an additional key +:path+ can be set to specify the path to the pdftk binary.
# There are two additional keys. +:path+ can be set to specify the path to the pdftk binary.
# +tmpdir+ can be set to specify the path of the temp directory.
# :input => some_input, :operation => some_operation, :output => some_output, :options => some_options
#
# ===Input
Expand Down Expand Up @@ -184,7 +185,7 @@ def pdftk(dsl_statements = {})
dsl_statements = @default_statements.merge(dsl_statements)
cmd = "#{@default_statements[:path]} #{set_cmd(dsl_statements)}"
if dsl_statements[:operation].to_s.match(/burst|unpack_files/)
cmd.insert(0, "cd #{Dir.tmpdir} && ")
cmd.insert(0, "cd #{tmpdir} && ")
end
Open3.popen3(cmd) do |stdin, stdout, stderr|
if @input
Expand All @@ -196,7 +197,7 @@ def pdftk(dsl_statements = {})
raise(CommandError, {:stderr => @error, :cmd => cmd}) unless (@error = stderr.read).empty?
end
if dsl_statements[:operation].to_s.match(/burst|unpack_files/) && dsl_statements[:output].nil?
Dir.tmpdir
tmpdir
else
@output
end
Expand Down Expand Up @@ -295,6 +296,14 @@ def pdftk_version
%x{#{@default_statements[:path]} --version 2>&1}.scan(/pdftk (\S*) a Handy Tool/).join
end

# Return the directory to use as the temp directory
#
# @return [String]
#
def tmpdir
@default_statements[:tmpdir] || Dir.tmpdir
end

# Return the path of the pdftk library if it can be located.
#
# @return [String, nil] return nil if the library cannot be found on the system
Expand Down
15 changes: 15 additions & 0 deletions spec/active_pdftk/call_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,18 @@
before :all do
options = {}
options[:path] = ENV['path'] unless ENV['path'].nil?
options[:tmpdir] = '/custom_temp'
@pdftk = ActivePdftk::Call.new(options)
end

it "should set the path (not nil)"do
@pdftk.default_statements[:path].should_not be_nil
end

it "should set the tmpdir" do
@pdftk.default_statements[:tmpdir].should == '/custom_temp'
end

it "should check the ENV vars" do
unless ENV['path'].nil? || ENV['version'].nil?
ENV['path'].should_not be_nil
Expand Down Expand Up @@ -255,4 +260,14 @@
end
end
end

context "#tmpdir" do
before :all do
@pdftk = ActivePdftk::Call.new
end

it "should use the system tmpdir when a tmpdir is not explicity set" do
@pdftk.tmpdir.should == Dir.tmpdir
end
end
end
4 changes: 2 additions & 2 deletions spec/active_pdftk/wrapper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ def map_output_type(output_specified)

it "should pass the defaults statements to the call instance." do
path = ActivePdftk::Call.new.locate_pdftk
@pdftk_opt = ActivePdftk::Wrapper.new(:path => path, :operation => {:fill_form => 'a.fdf'}, :options => { :flatten => false, :owner_pw => 'bar', :user_pw => 'baz', :encrypt => :'40bit'})
@pdftk_opt.default_statements.should == {:path => path, :operation => {:fill_form => 'a.fdf'}, :options => { :flatten => false, :owner_pw => 'bar', :user_pw => 'baz', :encrypt => :'40bit'}}
@pdftk_opt = ActivePdftk::Wrapper.new(:path => path, :tmpdir => '/other_tmp', :operation => {:fill_form => 'a.fdf'}, :options => { :flatten => false, :owner_pw => 'bar', :user_pw => 'baz', :encrypt => :'40bit'})
@pdftk_opt.default_statements.should == {:path => path, :tmpdir => '/other_tmp', :operation => {:fill_form => 'a.fdf'}, :options => { :flatten => false, :owner_pw => 'bar', :user_pw => 'baz', :encrypt => :'40bit'}}
end
end

Expand Down
3 changes: 2 additions & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require File.expand_path('../../lib/active_pdftk', __FILE__)

require 'rspec'
require 'digest'

Dir[File.expand_path(File.join(File.dirname(__FILE__),'support','**','*.rb'))].each {|f| require f}

Expand All @@ -13,4 +14,4 @@

config.filter_run :focus => true
config.run_all_when_everything_filtered = true
end
end