ilovepdf 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.editorconfig +12 -0
- data/.gitignore +14 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/LICENSE +21 -0
- data/README.md +142 -0
- data/Rakefile +6 -0
- data/bin/console +17 -0
- data/bin/setup +8 -0
- data/ilovepdf.gemspec +35 -0
- data/lib/ilovepdf.rb +36 -0
- data/lib/ilovepdf/errors.rb +51 -0
- data/lib/ilovepdf/file.rb +36 -0
- data/lib/ilovepdf/ilovepdf.rb +142 -0
- data/lib/ilovepdf/response.rb +36 -0
- data/lib/ilovepdf/task.rb +195 -0
- data/lib/ilovepdf/tool/compress.rb +20 -0
- data/lib/ilovepdf/tool/imagepdf.rb +27 -0
- data/lib/ilovepdf/tool/merge.rb +13 -0
- data/lib/ilovepdf/tool/officepdf.rb +13 -0
- data/lib/ilovepdf/tool/pagenumber.rb +40 -0
- data/lib/ilovepdf/tool/pdfa.rb +26 -0
- data/lib/ilovepdf/tool/pdfjpg.rb +22 -0
- data/lib/ilovepdf/tool/protect.rb +13 -0
- data/lib/ilovepdf/tool/repair.rb +12 -0
- data/lib/ilovepdf/tool/rotate.rb +12 -0
- data/lib/ilovepdf/tool/split.rb +35 -0
- data/lib/ilovepdf/tool/unlock.rb +12 -0
- data/lib/ilovepdf/tool/watermark.rb +49 -0
- data/lib/ilovepdf/version.rb +3 -0
- data/samples/compress_advanced.rb +25 -0
- data/samples/compress_basic.rb +14 -0
- data/samples/merge_advanced.rb +22 -0
- data/samples/merge_basic.rb +18 -0
- data/samples/pdfa_advanced.rb +21 -0
- data/samples/pdfa_basic.rb +17 -0
- data/samples/repair_advanced.rb +21 -0
- data/samples/repair_basic.rb +17 -0
- data/samples/rotate_advanced.rb +21 -0
- data/samples/rotate_basic.rb +17 -0
- data/samples/split_advanced.rb +32 -0
- data/samples/split_advanced_merge.rb +30 -0
- data/samples/split_basic.rb +17 -0
- data/samples/try_catch_errors.rb +46 -0
- data/samples/unlock_advanced.rb +21 -0
- data/samples/unlock_basic.rb +17 -0
- data/samples/watermark_advanced.rb +57 -0
- data/samples/watermark_basic.rb +20 -0
- data/uploads/sample_pdf.pdf +0 -0
- metadata +209 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 63a3af9ac1c6a9c1bdcd15ae6a8710aa6fb407cf
|
4
|
+
data.tar.gz: 37d7898f34c9d1bd1b95849ae9321acf29a2fca5
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9f2027499fac6671bf9260c9df6198c310623a7fca64abf1aeba6779253d38f640d53a03321bba996061660d144e013edf756c4d4f138ea1eb310da5ab9a66c0
|
7
|
+
data.tar.gz: 8a983ecc5ca1f6cf8433c0ff5c31deb60d27620f4693cc2db2a667246497c8656187ec2d640f755a91eaa3bf806fd5d23c2132da0323ffa970ccd8f767da35a1
|
data/.editorconfig
ADDED
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2017 iLovePDF
|
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 all
|
13
|
+
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 THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,142 @@
|
|
1
|
+
iLovePDF Api - Ruby Library
|
2
|
+
--------------------------
|
3
|
+
[![GitHub version](https://badge.fury.io/gh/ilovepdf%2Filovepdf-ruby.svg)](https://badge.fury.io/gh/ilovepdf%2Filovepdf-ruby)
|
4
|
+
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
|
5
|
+
|
6
|
+
A library in ruby for [iLovePDF Api](https://developer.ilovepdf.com)
|
7
|
+
|
8
|
+
You can sign up for a iLovePDF account at https://developer.ilovepdf.com
|
9
|
+
|
10
|
+
Develop and automate PDF processing tasks like:
|
11
|
+
|
12
|
+
* Compress PDF
|
13
|
+
* Merge PDF
|
14
|
+
* Split PDF
|
15
|
+
* Convert Office to PDF
|
16
|
+
* PDF to JPG
|
17
|
+
* Images to PDF
|
18
|
+
* Add Page Numbers
|
19
|
+
* Rotate PDF
|
20
|
+
* Unlock PDF
|
21
|
+
* Protect PDF
|
22
|
+
* Stamp a Watermark
|
23
|
+
* Repair PDF
|
24
|
+
* PDF to PDF/A
|
25
|
+
|
26
|
+
Each one with several settings to get your desired results.
|
27
|
+
|
28
|
+
## Requirements
|
29
|
+
Ruby 2.0 or greater
|
30
|
+
|
31
|
+
## Installation
|
32
|
+
|
33
|
+
Add this line to your application's Gemfile:
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
gem 'ilovepdf'
|
37
|
+
```
|
38
|
+
|
39
|
+
And then execute:
|
40
|
+
|
41
|
+
$ bundle
|
42
|
+
|
43
|
+
Or install it yourself as:
|
44
|
+
|
45
|
+
$ gem install ilovepdf
|
46
|
+
|
47
|
+
## Usage
|
48
|
+
|
49
|
+
### Getting started
|
50
|
+
The quickest way to get started is to first get a set of [API keys here](https://developer.ilovepdf.com/)
|
51
|
+
and run the following code snippet:
|
52
|
+
|
53
|
+
```ruby
|
54
|
+
public_key = 'YOUR_PUBLIC_KEY'
|
55
|
+
private_key = 'YOUR_PRIVATE_KEY'
|
56
|
+
ilovepdf = Ilovepdf::Ilovepdf.new(public_key, private_key)
|
57
|
+
# Create a task with the tool you want to use:
|
58
|
+
task = ilovepdf.new_task :compress
|
59
|
+
# Add the files you want to upload...
|
60
|
+
file1 = task.add_file 'my_disk/my_example1.pdf'
|
61
|
+
file2 = task.add_file 'my_disk/my_example2.pdf'
|
62
|
+
file3 = task.add_file_from_url 'http://URL_TO_PDF'
|
63
|
+
# Once you are done uploading your files:
|
64
|
+
task.execute
|
65
|
+
task.download
|
66
|
+
```
|
67
|
+
That's it!
|
68
|
+
|
69
|
+
For a more in-depth usage, refer to the [sample codes](https://github.com/ilovepdf/ilovepdf-ruby/tree/master/samples) in this repository.
|
70
|
+
|
71
|
+
## Documentation
|
72
|
+
### HTTP API Calls
|
73
|
+
All PDF tools have the following methods that contact with the iLovePDF API:
|
74
|
+
|
75
|
+
| Method | Description | Notes |
|
76
|
+
| ------------------------|-----------------------------------------------------------|-------------------------------|
|
77
|
+
| add_file(file) | Uploads a file to iLovepdf servers | Returns boolean |
|
78
|
+
| add_file_from_url(url) | Uploads a file to iLovepdf servers using a URL | Returns boolean |
|
79
|
+
| delete_file(file) | Deletes a file from ilovepdf | Returns boolean |
|
80
|
+
| download(filepath) | Downloads the processed file | Returns boolean; No need to specify a filepath|
|
81
|
+
| status | Retrieves the current status of the task being processed | Returns Ilovepdf::Response |
|
82
|
+
| execute | Sends a request to Ilovepdf to begin processing the PDFs | Returns Ilovepdf::Response |
|
83
|
+
| delete! | Deletes the task | Returns Ilovepdf::Response |
|
84
|
+
|
85
|
+
Example:
|
86
|
+
```ruby
|
87
|
+
imagepdf_task = Ilovepdf::Tool::Imagepdf.new(public_key, secret_key)
|
88
|
+
http_response = imagepdf_task.execute
|
89
|
+
puts http_response.body
|
90
|
+
if imagepdf_task.download.success?
|
91
|
+
puts "Your file was downloaded successfully!"
|
92
|
+
end
|
93
|
+
```
|
94
|
+
|
95
|
+
### Methods common to all tools
|
96
|
+
| Method | Description | Notes |
|
97
|
+
| --------------------------------------|-----------------------------------------------------------|---------------------------------------------|
|
98
|
+
| enable_file_encryption(boolean, key) | The key will be used to decrypt the files before processing and re-encrypt them after processing | If no key provided, a random key is assigned (default: false) |
|
99
|
+
| assign_meta_value(key, value) | [More info](https://developer.ilovepdf.com/docs/api-reference) | |
|
100
|
+
| ignore_errors | [More info](https://developer.ilovepdf.com/docs/api-reference) | (default: true) |
|
101
|
+
| ignore_password | [More info](https://developer.ilovepdf.com/docs/api-reference) | (default: true) |
|
102
|
+
| try_pdf_repair | When a PDF to process fails we try to repair it automatically | (default: true) |
|
103
|
+
| packaged_filename | This allows you to specify the filename of the compressed file in case there is more than 1 file to be downloaded | |
|
104
|
+
| output_filename | The final name of the processed file | |
|
105
|
+
|
106
|
+
### Tool attributes
|
107
|
+
|
108
|
+
All tools have specific attributes you can access and modify.
|
109
|
+
If you need want to know which params are available for a specific tool via the code, you can by looking at the **API_PARAMS** of that particular tool.
|
110
|
+
For example for the **Image to PDF** tool:
|
111
|
+
```ruby
|
112
|
+
puts Ilovepdf::Tool::Imagepdf::API_PARAMS
|
113
|
+
# => [:orientation, :margin, :pagesize]
|
114
|
+
```
|
115
|
+
|
116
|
+
To instantiate a Compress tool task directly do:
|
117
|
+
```ruby
|
118
|
+
compress_task = Ilovepdf::Tool::Compress.new(public_key, secret_key)
|
119
|
+
```
|
120
|
+
|
121
|
+
### Handling errors
|
122
|
+
|
123
|
+
Whenever there is an API Error in one of the endpoints, you can try to capture it the following way:
|
124
|
+
|
125
|
+
```ruby
|
126
|
+
begin
|
127
|
+
compress_task = Ilovepdf::Tool::Compress.new(public_key, secret_key)
|
128
|
+
compress_task.execute # Oops, this raises an error! I forgot to upload a file!
|
129
|
+
compress_taks.download
|
130
|
+
rescue Ilovepdf::ApiError => e
|
131
|
+
# Let's check what went wrong with the API call:
|
132
|
+
puts e.http_response.body
|
133
|
+
end
|
134
|
+
```
|
135
|
+
|
136
|
+
For a more complete example of error handling check [samples/try_catch_errors.rb](https://github.com/ilovepdf/ilovepdf-ruby/blob/master/samples/try_catch_errors.rb)
|
137
|
+
|
138
|
+
Please see https://developer.ilovepdf.com/docs for up-to-date documentation.
|
139
|
+
|
140
|
+
## License
|
141
|
+
|
142
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require 'byebug'
|
5
|
+
require "addressable/uri"
|
6
|
+
require "addressable/template"
|
7
|
+
require "ilovepdf"
|
8
|
+
|
9
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
10
|
+
# with your gem easier. You can also use a different console, if you like.
|
11
|
+
|
12
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
13
|
+
# require "pry"
|
14
|
+
# Pry.start
|
15
|
+
|
16
|
+
require "irb"
|
17
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/ilovepdf.gemspec
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'ilovepdf/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "ilovepdf"
|
8
|
+
spec.version = Ilovepdf::VERSION
|
9
|
+
spec.authors = ["Leonardo Collazo"]
|
10
|
+
spec.email = ["leonardo.chronicles@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{A library in Ruby for iLovePDF Api}
|
13
|
+
spec.description = %q{Develop and automate PDF processing tasks like Compress PDF, Merge PDF, Split PDF, convert Office to PDF, PDF to JPG, Images to PDF, add Page Numbers, Rotate PDF, Unlock PDF, stamp a Watermark and Repair PDF. Each one with several settings to get your desired results.}
|
14
|
+
spec.homepage = "https://github.com/ilovepdf/ilovepdf-ruby"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
18
|
+
f.match(%r{^(test|spec|features)/})
|
19
|
+
end
|
20
|
+
spec.bindir = "exe"
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
|
+
spec.require_paths = ["lib"]
|
23
|
+
spec.required_ruby_version = '>= 2.0'
|
24
|
+
|
25
|
+
spec.add_runtime_dependency 'rest-client', '~> 2'
|
26
|
+
spec.add_runtime_dependency 'jwt', '~> 1.5'
|
27
|
+
|
28
|
+
|
29
|
+
spec.add_development_dependency "bundler", "~> 1.14"
|
30
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
31
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
32
|
+
spec.add_development_dependency "byebug", "~> 9"
|
33
|
+
spec.add_development_dependency "webmock", "~> 3"
|
34
|
+
spec.add_development_dependency "addressable", "~> 2.5"
|
35
|
+
end
|
data/lib/ilovepdf.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
require "ilovepdf/version"
|
2
|
+
require 'pathname'
|
3
|
+
require 'securerandom'
|
4
|
+
|
5
|
+
# 3rd party gems
|
6
|
+
require 'jwt'
|
7
|
+
require 'rest-client'
|
8
|
+
|
9
|
+
# Base classes
|
10
|
+
require "ilovepdf/errors"
|
11
|
+
require "ilovepdf/response"
|
12
|
+
require "ilovepdf/ilovepdf"
|
13
|
+
require "ilovepdf/file"
|
14
|
+
require "ilovepdf/task"
|
15
|
+
|
16
|
+
# Load tool classes
|
17
|
+
require 'ilovepdf/tool/compress'
|
18
|
+
require 'ilovepdf/tool/imagepdf'
|
19
|
+
require 'ilovepdf/tool/merge'
|
20
|
+
require 'ilovepdf/tool/officepdf'
|
21
|
+
require 'ilovepdf/tool/pagenumber'
|
22
|
+
require 'ilovepdf/tool/pdfa'
|
23
|
+
require 'ilovepdf/tool/pdfjpg'
|
24
|
+
require 'ilovepdf/tool/repair'
|
25
|
+
require 'ilovepdf/tool/rotate'
|
26
|
+
require 'ilovepdf/tool/split'
|
27
|
+
require 'ilovepdf/tool/unlock'
|
28
|
+
require 'ilovepdf/tool/watermark'
|
29
|
+
|
30
|
+
module Ilovepdf
|
31
|
+
class << self
|
32
|
+
def root
|
33
|
+
@root_path ||= Pathname.new(::File.dirname(::File.expand_path('../', __FILE__)))
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
module Ilovepdf
|
2
|
+
# Base error classes
|
3
|
+
class Error < StandardError
|
4
|
+
end
|
5
|
+
|
6
|
+
class ApiError < Error
|
7
|
+
attr_accessor :http_response
|
8
|
+
def initialize(http_response, custom_msg: nil)
|
9
|
+
msg_to_use = custom_msg ? custom_msg : extract_error_text(http_response)
|
10
|
+
super(msg_to_use)
|
11
|
+
self.http_response = http_response
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
def extract_error_text(http_response)
|
16
|
+
r_body = http_response.body
|
17
|
+
if r_body['name']
|
18
|
+
title = r_body['name']
|
19
|
+
msg = r_body['message']
|
20
|
+
elsif r_body['error']
|
21
|
+
title = r_body['error']['type']
|
22
|
+
msg = r_body['error']['message']
|
23
|
+
if r_body['error']['param']
|
24
|
+
msg << " Details: " + r_body['error']['param'].to_s
|
25
|
+
end
|
26
|
+
else
|
27
|
+
title = 'An error ocurred'
|
28
|
+
msg = 'Check the response from the server'
|
29
|
+
end
|
30
|
+
"[#{title}] #{msg}"
|
31
|
+
end
|
32
|
+
end
|
33
|
+
module Errors
|
34
|
+
# Endpoint-specific errors
|
35
|
+
class AuthError < ::Ilovepdf::ApiError
|
36
|
+
end
|
37
|
+
class ProcessError < ::Ilovepdf::ApiError
|
38
|
+
end
|
39
|
+
class StartError < ::Ilovepdf::ApiError
|
40
|
+
end
|
41
|
+
class UploadError < ::Ilovepdf::ApiError
|
42
|
+
end
|
43
|
+
|
44
|
+
# Library errors
|
45
|
+
class ArgumentEnumError < ::ArgumentError
|
46
|
+
def initialize(valid_values)
|
47
|
+
super("Provided argument is invalid. Valid values: #{valid_values.join(', ')}")
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module Ilovepdf
|
2
|
+
class File
|
3
|
+
attr_accessor :server_filename, :filename, :password
|
4
|
+
attr_reader :rotate
|
5
|
+
|
6
|
+
ROTATION_VALUES = [0, 90, 180, 270]
|
7
|
+
|
8
|
+
def initialize(server_filename, filename)
|
9
|
+
self.server_filename = server_filename
|
10
|
+
self.filename = filename
|
11
|
+
@deleted = false
|
12
|
+
end
|
13
|
+
|
14
|
+
def file_options
|
15
|
+
{
|
16
|
+
server_filename: self.server_filename,
|
17
|
+
filename: self.filename,
|
18
|
+
rotate: self.rotate,
|
19
|
+
password: self.password
|
20
|
+
}
|
21
|
+
end
|
22
|
+
|
23
|
+
def rotate= degrees
|
24
|
+
raise Errors::ArgumentEnumError.new(ROTATION_VALUES) unless ROTATION_VALUES.include? degrees
|
25
|
+
@rotate = degrees
|
26
|
+
end
|
27
|
+
|
28
|
+
def mark_as_deleted
|
29
|
+
@deleted = true
|
30
|
+
end
|
31
|
+
|
32
|
+
def deleted?
|
33
|
+
@deleted
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,142 @@
|
|
1
|
+
module Ilovepdf
|
2
|
+
class Ilovepdf
|
3
|
+
attr_accessor :api_version, :token, :encrypt_key ,:debug
|
4
|
+
|
5
|
+
START_SERVER = 'https://api.ilovepdf.com'.freeze
|
6
|
+
API_VERSION = 'v1'.freeze
|
7
|
+
TOKEN_ALGORITHM = 'HS256'.freeze
|
8
|
+
ALL_ENDPOINTS = [:start, :upload, :process, :download, :task].freeze
|
9
|
+
LONG_JOB_ENDPOINTS = [:process, :upload, :download].freeze
|
10
|
+
|
11
|
+
TIME_DELAY = 5400
|
12
|
+
|
13
|
+
def initialize(public_key=nil, secret_key=nil)
|
14
|
+
set_api_keys(public_key, secret_key)
|
15
|
+
api_version = API_VERSION
|
16
|
+
end
|
17
|
+
|
18
|
+
def new_task(tool_name)
|
19
|
+
task_klass = ::Ilovepdf::Tool.const_get(tool_name.to_s.capitalize) rescue false
|
20
|
+
unless task_klass
|
21
|
+
raise ::Ilovepdf::Error.new("Unknown tool '#{tool_name}'. Available tools: #{self.class.all_tool_names.to_s}")
|
22
|
+
end
|
23
|
+
task_klass.new(@public_key, @secret_key)
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.all_tool_names
|
27
|
+
::Ilovepdf::Tool.constants.map(&:downcase)
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.raise_exceptions=(value)
|
31
|
+
@raise_exceptions = value
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.raise_exceptions?
|
35
|
+
@raise_exceptions.eql?(true)
|
36
|
+
end
|
37
|
+
|
38
|
+
def set_api_keys(public_key, secret_key)
|
39
|
+
@public_key = public_key
|
40
|
+
@secret_key = secret_key
|
41
|
+
end
|
42
|
+
|
43
|
+
def enable_file_encryption(enable, new_encrypt_key = nil)
|
44
|
+
if enable
|
45
|
+
if new_encrypt_key && ![16, 24, 32].include?(new_encrypt_key.size)
|
46
|
+
raise ArgumentError.new("Encryption key must be 16, 24 or 32 characters long")
|
47
|
+
end
|
48
|
+
self.encrypt_key = new_encrypt_key || SecureRandom.hex(16)
|
49
|
+
else
|
50
|
+
self.encrypt_key = nil
|
51
|
+
end
|
52
|
+
nil
|
53
|
+
end
|
54
|
+
|
55
|
+
private
|
56
|
+
|
57
|
+
def jwt
|
58
|
+
raise Error.new('You must provide a set of API keys') unless api_keys_present?
|
59
|
+
@token = JWT.encode jwt_token_payload, @secret_key, token_algorithm
|
60
|
+
end
|
61
|
+
|
62
|
+
def send_request(http_method, endpoint, extra_opts={})
|
63
|
+
to_server = worker_server ? worker_server : START_SERVER
|
64
|
+
|
65
|
+
timeout_to_use = LONG_JOB_ENDPOINTS.include?(endpoint.to_sym) ? nil : 10
|
66
|
+
extra_opts[:body] ||= {}
|
67
|
+
extra_opts[:headers] ||= {}
|
68
|
+
|
69
|
+
extra_opts[:headers].merge!({
|
70
|
+
'Accept' => 'application/json',
|
71
|
+
'Authorization' => "Bearer " + jwt
|
72
|
+
})
|
73
|
+
|
74
|
+
extra_opts[:body][:debug] = true if self.debug
|
75
|
+
|
76
|
+
request_uri = to_server + "/v1/#{endpoint}"
|
77
|
+
begin
|
78
|
+
rest_response = RestClient::Request.execute( method: http_method.to_sym, url: request_uri, timeout: timeout_to_use,
|
79
|
+
headers: extra_opts[:headers], payload: extra_opts[:body]
|
80
|
+
)
|
81
|
+
response = Response.new(rest_response)
|
82
|
+
rescue RestClient::Unauthorized => e
|
83
|
+
raise Errors::AuthError.new(Response.new(e.response))
|
84
|
+
rescue RestClient::Exception => e
|
85
|
+
raise klass_error_for(endpoint).new(Response.new(e.response))
|
86
|
+
end
|
87
|
+
|
88
|
+
response
|
89
|
+
end
|
90
|
+
|
91
|
+
def api_keys_present?
|
92
|
+
!@public_key.nil? && !@secret_key.nil?
|
93
|
+
end
|
94
|
+
|
95
|
+
def klass_error_for(endpoint)
|
96
|
+
error_klass = ::Ilovepdf::Errors.const_get("#{endpoint.to_s.capitalize}Error") rescue false
|
97
|
+
error_klass = ::Ilovepdf::ApiError if !error_klass # use generic ApiError
|
98
|
+
error_klass
|
99
|
+
end
|
100
|
+
|
101
|
+
def query_task_status(new_server, task_id)
|
102
|
+
old_server = worker_server
|
103
|
+
worker_server = new_server
|
104
|
+
begin
|
105
|
+
response = send_request('get', 'task/' + task_id.to_s)
|
106
|
+
ensure
|
107
|
+
worker_server = old_server
|
108
|
+
end
|
109
|
+
response
|
110
|
+
end
|
111
|
+
|
112
|
+
def jwt_token_payload
|
113
|
+
current_time = Time.now.to_i
|
114
|
+
params = {
|
115
|
+
iss: '', # hostInfo
|
116
|
+
aud: '', # hostInfo
|
117
|
+
iat: current_time - TIME_DELAY, # add some "delay"
|
118
|
+
nbf: current_time - TIME_DELAY, # add some "delay"
|
119
|
+
exp: current_time + 3600 + TIME_DELAY, # add some "delay"
|
120
|
+
jti: @public_key
|
121
|
+
}
|
122
|
+
|
123
|
+
params[:file_encryption_key] = self.encrypt_key if is_file_encrypted?
|
124
|
+
params
|
125
|
+
end
|
126
|
+
|
127
|
+
def worker_server; @worker_server; end
|
128
|
+
def worker_server= new_server; @worker_server = new_server; end
|
129
|
+
|
130
|
+
def is_file_encrypted?
|
131
|
+
!self.encrypt_key.nil?
|
132
|
+
end
|
133
|
+
|
134
|
+
def token_algorithm
|
135
|
+
TOKEN_ALGORITHM
|
136
|
+
end
|
137
|
+
|
138
|
+
def token
|
139
|
+
@token
|
140
|
+
end
|
141
|
+
end # Ilovepdf
|
142
|
+
end
|