Skip to content

Commit

Permalink
Release 1.1.0
Browse files Browse the repository at this point in the history
Release 1.1.0
  • Loading branch information
leoschronicles authored Jun 15, 2017
1 parent c4c3a94 commit 275d2e4
Show file tree
Hide file tree
Showing 21 changed files with 254 additions and 29 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ Develop and automate PDF processing tasks like:
* Stamp a Watermark
* Repair PDF
* PDF to PDF/A
* Validate PDF/A
* Extract

Each one with several settings to get your desired results.

Expand Down Expand Up @@ -104,6 +106,15 @@ end
| packaged_filename | This allows you to specify the filename of the compressed file in case there is more than 1 file to be downloaded | |
| output_filename | The final name of the processed file | |

#### Methods to query after performing the **execute** API method:
* result: It has stored the last **Ilovepdf::Response**

#### Methods to query after performing the **download** API method:
* download_info: Returns a [struct](https://ruby-doc.org/core-2.2.0/Struct.html) with the following info
* :output_filename
* :output_file
* :output_filetype

### Tool attributes

All tools have specific attributes you can access and modify.
Expand Down
4 changes: 4 additions & 0 deletions lib/ilovepdf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
# Base classes
require "ilovepdf/errors"
require "ilovepdf/response"
require "ilovepdf/helper"
require "ilovepdf/ilovepdf"
require "ilovepdf/file"
require "ilovepdf/task"
Expand All @@ -20,12 +21,15 @@
require 'ilovepdf/tool/officepdf'
require 'ilovepdf/tool/pagenumber'
require 'ilovepdf/tool/pdfa'
require 'ilovepdf/tool/validate_pdfa'
require 'ilovepdf/tool/pdfjpg'
require 'ilovepdf/tool/repair'
require 'ilovepdf/tool/rotate'
require 'ilovepdf/tool/split'
require 'ilovepdf/tool/unlock'
require 'ilovepdf/tool/watermark'
require 'ilovepdf/tool/protect'
require 'ilovepdf/tool/extract'

module Ilovepdf
class << self
Expand Down
2 changes: 2 additions & 0 deletions lib/ilovepdf/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,7 @@ def initialize(valid_values)
super("Provided argument is invalid. Valid values: #{valid_values.join(', ')}")
end
end
class UnsupportedFunctionalityError < ::Ilovepdf::Error
end
end
end
13 changes: 13 additions & 0 deletions lib/ilovepdf/helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Ilovepdf
module Helper
extend self

def underscore_str(str)
str.replace(str.scan(/[A-Z][a-z]*/).join("_").downcase)
end

def camelize_str(str)
str.split('_').map(&:capitalize).join
end
end
end
11 changes: 7 additions & 4 deletions lib/ilovepdf/ilovepdf.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Ilovepdf
class Ilovepdf
attr_accessor :api_version, :token, :encrypt_key ,:debug
attr_accessor :api_version, :token, :encrypt_key, :debug, :timeout, :long_timeout

START_SERVER = 'https://api.ilovepdf.com'.freeze
API_VERSION = 'v1'.freeze
Expand All @@ -13,18 +13,21 @@ class Ilovepdf
def initialize(public_key=nil, secret_key=nil)
set_api_keys(public_key, secret_key)
api_version = API_VERSION
self.timeout = 10
self.long_timeout = nil
end

def new_task(tool_name)
task_klass = ::Ilovepdf::Tool.const_get(tool_name.to_s.capitalize) rescue false
camelized_name = Helper.camelize_str(tool_name.to_s)
task_klass = ::Ilovepdf::Tool.const_get(camelized_name) rescue false
unless task_klass
raise ::Ilovepdf::Error.new("Unknown tool '#{tool_name}'. Available tools: #{self.class.all_tool_names.to_s}")
end
task_klass.new(@public_key, @secret_key)
end

def self.all_tool_names
::Ilovepdf::Tool.constants.map(&:downcase)
::Ilovepdf::Tool.constants.map{|tool_name| Helper.underscore_str(tool_name.to_s)}
end

def self.raise_exceptions=(value)
Expand Down Expand Up @@ -62,7 +65,7 @@ def jwt
def send_request(http_method, endpoint, extra_opts={})
to_server = worker_server ? worker_server : START_SERVER

timeout_to_use = LONG_JOB_ENDPOINTS.include?(endpoint.to_sym) ? nil : 10
timeout_to_use = LONG_JOB_ENDPOINTS.include?(endpoint.to_sym) ? self.long_timeout : self.timeout
extra_opts[:body] ||= {}
extra_opts[:headers] ||= {}

Expand Down
46 changes: 32 additions & 14 deletions lib/ilovepdf/task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ class Task < Ilovepdf
attr_accessor :task_id, :tool, :packaged_filename, :output_filename,
:ignore_errors, :ignore_password, :try_pdf_repair

API_PARAMS = []
attr_reader :result

API_PARAMS = []
DOWNLOAD_INFO = [:output_filename, :output_file, :output_filetype]

def initialize(public_key, secret_key)
super(public_key, secret_key)
Expand Down Expand Up @@ -38,8 +41,29 @@ def add_file_from_url(url)
files.last
end

def download(path=nil)
download_file(path)
def download(path=nil, create_directory: false)
download_file

if path
path = Pathname.new(path).to_s if path.is_a?(Pathname)
path.chop! if path.end_with? '/'
else
path = '.'
end

destination = "#{path}/#{download_info.output_filename}"
FileUtils.mkdir_p(path) if create_directory
::File.open(destination, 'wb'){|file| file.write(download_info.output_file) }
true
end

def blob
download_file
download_info.output_file
end

def download_info
@download_info ||= Struct.new(*DOWNLOAD_INFO).new
end

# [API Methods] Actions on task
Expand All @@ -49,7 +73,7 @@ def status
end

def execute
perform_process_request
@result = perform_process_request
end

def delete!
Expand Down Expand Up @@ -87,7 +111,7 @@ def files=(new_array_of_files)
@files = new_array_of_files
end

def download_file path
def download_file
response = perform_filedownload_request
content_disposition = response.headers[:content_disposition]

Expand All @@ -98,15 +122,9 @@ def download_file path
filename = match_data[1].gsub('"', '')
end

if path
path = Pathname.new(path).to_s if path.is_a?(Pathname)
path.chop! if path.end_with? '/'
else
path = '.'
end
destination = "#{path}/#{filename}"
::File.open(destination, 'wb'){|file| file.write(response.raw_body) }
file = response.raw_body
download_info.output_filename = filename
download_info.output_file = response.raw_body
download_info.output_filetype = ::File.extname(filename)
true
end

Expand Down
18 changes: 18 additions & 0 deletions lib/ilovepdf/tool/extract.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module Ilovepdf
module Tool
class Extract < ::Ilovepdf::Task
API_PARAMS = [:detailed]
attr_accessor *API_PARAMS

def initialize(public_key, secret_key)
self.tool = :extract
super(public_key, secret_key)
end

def detailed
@detailed ||= false
end

end
end
end
4 changes: 2 additions & 2 deletions lib/ilovepdf/tool/pagenumber.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ class Pagenumber < ::Ilovepdf::Task

attr_accessor *API_PARAMS

VERTICAL_POSITION_VALUES = ['bottom', 'top']
HORIZONTAL_POSITION_VALUES = ['left', 'middle', 'right']
VERTICAL_POSITION_VALUES = ['bottom', 'middle' ,'top']
HORIZONTAL_POSITION_VALUES = ['left', 'center', 'right']
FONT_FAMILY_VALUES = [ 'Arial', 'Arial Unicode MS', 'Verdana', 'Courier',
'Times New Roman', 'Comic Sans MS',
'WenQuanYi Zen Hei', 'Lohit Marathi'
Expand Down
5 changes: 3 additions & 2 deletions lib/ilovepdf/tool/pdfa.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Ilovepdf
module Tool
class Pdfa < ::Ilovepdf::Task
API_PARAMS = [:conformance]
API_PARAMS = [:conformance, :allow_downgrade]
attr_accessor *API_PARAMS

CONFORMANCE_VALUES = ['pdfa-1b', 'pdfa-1a', 'pdfa-2b', 'pdfa-2u',
Expand All @@ -11,6 +11,7 @@ class Pdfa < ::Ilovepdf::Task
def initialize(public_key, secret_key)
self.tool = :pdfa
super(public_key, secret_key)
self.allow_downgrade = true
end

def conformance= new_val
Expand All @@ -19,7 +20,7 @@ def conformance= new_val
end

def conformance
@conformance ||= 'default'
@conformance ||= 'pdfa-2b'
end
end
end
Expand Down
31 changes: 31 additions & 0 deletions lib/ilovepdf/tool/validate_pdfa.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module Ilovepdf
module Tool
class ValidatePdfa < ::Ilovepdf::Task
API_PARAMS = [:conformance]
attr_accessor *API_PARAMS

CONFORMANCE_VALUES = ['pdfa-1b', 'pdfa-1a', 'pdfa-2b', 'pdfa-2u',
'pdfa-2a', 'pdfa-3b', 'pdfa-3u', 'pdfa-3a'
]

def initialize(public_key, secret_key)
self.tool = :validatepdfa
super(public_key, secret_key)
end

def conformance= new_val
raise Errors::ArgumentEnumError.new(CONFORMANCE_VALUES) unless CONFORMANCE_VALUES.include? new_val
@conformance = new_val
end

def conformance
@conformance ||= 'pdfa-2b'
end

private
def download_file
raise ::Ilovepdf::Errors::UnsupportedFunctionalityError.new('This tool does not download files (Check in the sample files how to use it)')
end
end
end
end
2 changes: 1 addition & 1 deletion lib/ilovepdf/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Ilovepdf
VERSION = "1.0.1"
VERSION = "1.1.0"
end
23 changes: 23 additions & 0 deletions samples/extract_advanced.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require "bundler/setup"
require 'ilovepdf'

# You can call task class directly
my_task = Ilovepdf::Tool::Extract.new("PUBLIC_KEY", "SECRET_KEY");

# File object keeps information about its server_filename and the properties you can set
file = my_task.add_file '/path/to/file/document.pdf'

# By setting this parameter to true,
# we'll have detailed contents extraction into a CSV-formatted file
my_task.detailed = true

# and set name for output file.
# the task will set the correct file extension for you.
my_task.output_filename = 'csv_filename'

# Process files
response = my_task.execute

# and finally download the file. If no path is set, it will be downloaded on your current working directory
# It will download a text file with Linux line endings
my_task.download
15 changes: 15 additions & 0 deletions samples/extract_basic.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require "bundler/setup"
require 'ilovepdf'

# You can call task class directly
my_task = Ilovepdf::Tool::Extract.new("PUBLIC_KEY", "SECRET_KEY");

# File object keeps information about its server_filename and the properties you can set
file = my_task.add_file '/path/to/file/document.pdf'

# Process files
response = my_task.execute

# and finally download the file. If no path is set, it will be downloaded on your current working directory
# It will download a text file with Linux line endings
my_task.download
25 changes: 25 additions & 0 deletions samples/imagepdf_advanced.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require "bundler/setup"
require 'ilovepdf'

# You can call task class directly
my_task = Ilovepdf::Tool::Imagepdf.new("PUBLIC_KEY", "SECRET_KEY");

# File object keeps information about its server_filename and the properties you can set
file = my_task.add_file '/path/to/file/photo.png'
file2 = my_task.add_file '/path/to/file/image.tiff'

# Merge After: Default is true. If it is false then it will download a zip file with a pdf for each image
my_task.merge_after = false

# and set name for output file.
# the task will set the correct file extension for you.
my_task.output_filename = 'pdf_filename'

# and name for splitted document (inside the zip file)
my_task.packaged_filename = 'zip_filename'

# Process files
response = my_task.execute

# and finally download the file. If no path is set, it will be downloaded on your current working directory
my_task.download
14 changes: 14 additions & 0 deletions samples/imagepdf_basic.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require "bundler/setup"
require 'ilovepdf'

# You can call task class directly
my_task = Ilovepdf::Tool::Imagepdf.new("PUBLIC_KEY", "SECRET_KEY");

# File object keeps information about its server_filename and the properties you can set
file = my_task.add_file '/path/to/file/photo.png'

# Process files
response = my_task.execute

# and finally download the file. If no path is set, it will be downloaded on your current working directory
my_task.download
5 changes: 4 additions & 1 deletion samples/pdfa_advanced.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
file = my_task.add_file '/path/to/file/document.pdf'

# Set conformance
file.conformance = 'pdfa-1b'
my_task.conformance = 'pdfa-2a'

# don't allow downgrading in case your conformance level fails
file.allow_downgrade = false

# and set name for output file.
# the task will set the correct file extension for you.
Expand Down
3 changes: 0 additions & 3 deletions samples/pdfa_basic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
# File object keeps information about its server_filename and the properties you can set
file = my_task.add_file '/path/to/file/document.pdf'

# Set conformance
file.conformance = 'pdfa-1b'

# Finally is time to process the files. i.e We'll send them to Ilovepdf servers :)
response = my_task.execute

Expand Down
Loading

0 comments on commit 275d2e4

Please sign in to comment.