cloud_convert_merge_pdf 0.0.0
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 +7 -0
- data/README.md +50 -0
- data/lib/cloud_convert_merge_pdf.rb +19 -0
- data/lib/cloud_convert_merge_pdf/configuration.rb +20 -0
- data/lib/cloud_convert_merge_pdf/merge.rb +97 -0
- data/lib/cloud_convert_merge_pdf/version.rb +8 -0
- data/test/test_merge.rb +16 -0
- metadata +105 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 34f833e62dc61b2a8eaa74ba5af47dff3127af23
|
4
|
+
data.tar.gz: b83ca855a363a6eb29bc51a471b9da62dd4f6fb0
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: cd0ab6a556f3437a07ea511f99b5c6b78016111f8266600b674f8aafb25934adab6758f196b1f3b10b65f11dd363ec2076812f017bc9dcc9d2617ba26530c609
|
7
|
+
data.tar.gz: 023c1b23b1818151ebf5ba86dbf88ecc0d9dbf4eb88b58a9e6bf86a88bccd04de12aa3dd91721a28a8ebb9d02b0da4c688c56d008e7ac17506e9681d20a6c26d
|
data/README.md
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
# CloudConvertMergePDF
|
2
|
+
|
3
|
+
CloudConvertMergePDF is a ruby wrapper for the [Cloud Convert](https://cloudconvert.com/api/combine) combine files to pdf API. The CloudConvert combine to pdf API merge files of any supported file type to a single PDF file.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'cloud_convert_merge_pdf'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
## Basic Usage
|
18
|
+
|
19
|
+
Add the following to an initializer file.
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
CloudConvertMergePdf.configure do |config|
|
23
|
+
config.api_key = "YOUR CLOUD CONVERT API KEY"
|
24
|
+
end
|
25
|
+
```
|
26
|
+
If you need a file object for the combined pdf:
|
27
|
+
```ruby
|
28
|
+
CloudConvertMergePdf::Merge.new(files).call
|
29
|
+
```
|
30
|
+
If you need the url for the combined pdf:
|
31
|
+
```ruby
|
32
|
+
CloudConvertMergePdf::Merge.new(files, false).call
|
33
|
+
```
|
34
|
+
|
35
|
+
## Basic Example
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
files = ['http://www.example.com/1.pdf', 'http://www.example.com/2.pdf']
|
39
|
+
merge_obj = CloudConvertMergePdf::Merge.new(files)
|
40
|
+
out_file = @client.call # This will return a Tempfile object for the combined pdf
|
41
|
+
out_file.unlink # Its a good apporach to unlink the tempfile than leaving it for the garbage collector
|
42
|
+
```
|
43
|
+
|
44
|
+
## Contributing
|
45
|
+
|
46
|
+
Please feel free to contribute to the repository.
|
47
|
+
|
48
|
+
## License
|
49
|
+
|
50
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'cloud_convert_merge_pdf/version'
|
2
|
+
require 'cloud_convert_merge_pdf/configuration'
|
3
|
+
require 'cloud_convert_merge_pdf/merge'
|
4
|
+
|
5
|
+
require 'faraday'
|
6
|
+
require 'json'
|
7
|
+
|
8
|
+
#
|
9
|
+
# CloudConvertMergePdf
|
10
|
+
#
|
11
|
+
# @author sufinsha
|
12
|
+
#
|
13
|
+
module CloudConvertMergePdf
|
14
|
+
CLOUD_CONVERT_ENDPOINT = 'https://api.cloudconvert.org/'.freeze
|
15
|
+
|
16
|
+
API_KEY_ERROR = 'API Key cant be blank!'.freeze
|
17
|
+
|
18
|
+
CloudConvertMergePdf.configure
|
19
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
#
|
2
|
+
# CloudConvertMergePdf
|
3
|
+
#
|
4
|
+
# @author sufinsha
|
5
|
+
#
|
6
|
+
module CloudConvertMergePdf
|
7
|
+
# Configuration
|
8
|
+
class Configuration
|
9
|
+
attr_accessor :api_key
|
10
|
+
end
|
11
|
+
|
12
|
+
class << self
|
13
|
+
attr_accessor :configuration
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.configure
|
17
|
+
self.configuration ||= Configuration.new
|
18
|
+
yield(configuration) if block_given?
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,97 @@
|
|
1
|
+
require 'tempfile'
|
2
|
+
require 'securerandom'
|
3
|
+
|
4
|
+
module CloudConvertMergePdf
|
5
|
+
#
|
6
|
+
# CloudConvertMergePdf::Merge
|
7
|
+
#
|
8
|
+
# @author sufinsha
|
9
|
+
#
|
10
|
+
class Merge
|
11
|
+
attr_reader :download_url
|
12
|
+
|
13
|
+
def initialize(files, download = true)
|
14
|
+
@connection = Faraday.new(
|
15
|
+
url: CloudConvertMergePdf::CLOUD_CONVERT_ENDPOINT
|
16
|
+
)
|
17
|
+
@files = files
|
18
|
+
@can_download = download
|
19
|
+
end
|
20
|
+
|
21
|
+
def call
|
22
|
+
api_key = CloudConvertMergePdf.configuration.api_key
|
23
|
+
return CloudConvertMergePdf::API_KEY_ERROR if api_key.nil?
|
24
|
+
|
25
|
+
fetch_process_url
|
26
|
+
process
|
27
|
+
|
28
|
+
download
|
29
|
+
end
|
30
|
+
|
31
|
+
def fetch_process_url
|
32
|
+
response = @connection.post '/process', process_url_params
|
33
|
+
|
34
|
+
parsed_response = parse_response(response.body)
|
35
|
+
|
36
|
+
@process_url = parsed_response['url']
|
37
|
+
end
|
38
|
+
|
39
|
+
def process
|
40
|
+
response = @connection.post @process_url, process_params
|
41
|
+
parsed_response = parse_response(response.body)
|
42
|
+
|
43
|
+
return nil if parsed_response['output'].nil?
|
44
|
+
|
45
|
+
@download_url = parsed_response['output']['url']
|
46
|
+
end
|
47
|
+
|
48
|
+
def download
|
49
|
+
response = @connection.get @download_url
|
50
|
+
|
51
|
+
return parse_response(response.body) if response.status != 200
|
52
|
+
|
53
|
+
return clean_url(@download_url) unless @can_download
|
54
|
+
|
55
|
+
combined_pdf(response.body)
|
56
|
+
end
|
57
|
+
|
58
|
+
private
|
59
|
+
|
60
|
+
def process_url_params
|
61
|
+
{
|
62
|
+
'apikey' => CloudConvertMergePdf.configuration.api_key,
|
63
|
+
'mode' => 'combine',
|
64
|
+
'inputformat' => 'pdf',
|
65
|
+
'outputformat' => 'pdf'
|
66
|
+
}
|
67
|
+
end
|
68
|
+
|
69
|
+
def process_params
|
70
|
+
{
|
71
|
+
'mode' => 'combine',
|
72
|
+
'input' => 'download',
|
73
|
+
'files' => @files,
|
74
|
+
'outputformat' => 'pdf',
|
75
|
+
'wait' => true
|
76
|
+
}
|
77
|
+
end
|
78
|
+
|
79
|
+
def parse_response(response)
|
80
|
+
JSON.parse(response)
|
81
|
+
end
|
82
|
+
|
83
|
+
def combined_pdf(file_body)
|
84
|
+
file = Tempfile.new("#{SecureRandom.uuid}.pdf")
|
85
|
+
file.puts file_body
|
86
|
+
file.close
|
87
|
+
|
88
|
+
file
|
89
|
+
end
|
90
|
+
|
91
|
+
def clean_url(url)
|
92
|
+
return '' if url.nil? || url.empty?
|
93
|
+
|
94
|
+
url.gsub(%r{^\/\/}, '')
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
data/test/test_merge.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'minitest/autorun'
|
2
|
+
require_relative '../lib/cloud_convert_merge_pdf'
|
3
|
+
|
4
|
+
class TestMerge < Minitest::Test
|
5
|
+
FILES = [
|
6
|
+
'https://cloudconvert.com/assets/11e13801/testfiles/pdfexample1.pdf',
|
7
|
+
'https://cloudconvert.com/assets/11e13801/testfiles/pdfexample2.pdf'
|
8
|
+
].freeze
|
9
|
+
|
10
|
+
def test_nil_api_key
|
11
|
+
assert_equal(
|
12
|
+
CloudConvertMergePdf::Merge.new([]).call,
|
13
|
+
'API Key cant be blank!'
|
14
|
+
)
|
15
|
+
end
|
16
|
+
end
|
metadata
ADDED
@@ -0,0 +1,105 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cloud_convert_merge_pdf
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Sufinsha
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-06-01 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.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: minitest
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 4.7.3
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 4.7.3
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: faraday
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: json
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: Ruby Wrapper for CloudConvert Combine files to PDF API.
|
70
|
+
email: sufinsha@gmail.com
|
71
|
+
executables: []
|
72
|
+
extensions: []
|
73
|
+
extra_rdoc_files: []
|
74
|
+
files:
|
75
|
+
- README.md
|
76
|
+
- lib/cloud_convert_merge_pdf.rb
|
77
|
+
- lib/cloud_convert_merge_pdf/configuration.rb
|
78
|
+
- lib/cloud_convert_merge_pdf/merge.rb
|
79
|
+
- lib/cloud_convert_merge_pdf/version.rb
|
80
|
+
- test/test_merge.rb
|
81
|
+
homepage: https://github.com/72pulses/CloudConvertMergePdf
|
82
|
+
licenses:
|
83
|
+
- MIT
|
84
|
+
metadata: {}
|
85
|
+
post_install_message:
|
86
|
+
rdoc_options: []
|
87
|
+
require_paths:
|
88
|
+
- lib
|
89
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
90
|
+
requirements:
|
91
|
+
- - ">="
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
95
|
+
requirements:
|
96
|
+
- - ">="
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '0'
|
99
|
+
requirements: []
|
100
|
+
rubyforge_project:
|
101
|
+
rubygems_version: 2.4.3
|
102
|
+
signing_key:
|
103
|
+
specification_version: 4
|
104
|
+
summary: Ruby Wrapper for CloudConvert Merge PDF API
|
105
|
+
test_files: []
|