pdfire 1.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/LICENSE +21 -0
- data/README.md +32 -0
- data/lib/bytes_result.rb +15 -0
- data/lib/client.rb +104 -0
- data/lib/conversion.rb +30 -0
- data/lib/conversion_params.rb +115 -0
- data/lib/errors.rb +45 -0
- data/lib/merge_params.rb +29 -0
- data/lib/result.rb +7 -0
- metadata +52 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 4d3727f14b8734acd147be9b85d18a9c10f34ed34ee1766a607f91f813de491e
|
|
4
|
+
data.tar.gz: db688ae02085f6360a0108711ebf6e7111ba01f7fcb5b258092f19827ca5071d
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 477908a745b6629d6dde7df73752771ec02f775aa2ec5b1d57ec8fd501d2437b7f805a85e71262cd0b2c03e63686ee37cd89f98dabea776a40ace1930d005a9a
|
|
7
|
+
data.tar.gz: 0d2707b3e7a6c9be8141c5ccf8da79111a4f252ce0b757ecdad575146115c12e887dde62ba698138ea737229084d921ddbec4c35ad59c25d73682c74eb4a187b
|
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2019 modernice Ltd.
|
|
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,32 @@
|
|
|
1
|
+
# PDFire Ruby
|
|
2
|
+
|
|
3
|
+

|
|
4
|
+
|
|
5
|
+
This PHP package provides a client for the [PDFire.io](https://pdfire.io) API. Read the [Documentation](https://docs.pdfire.io) for a list of available options.
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
gem install pdfire
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Basic usage
|
|
14
|
+
|
|
15
|
+
```ruby
|
|
16
|
+
require 'pdfire'
|
|
17
|
+
|
|
18
|
+
client = PDFire::Client.new('YOUR-API-KEY')
|
|
19
|
+
|
|
20
|
+
params = PDFire::ConversionParams.new
|
|
21
|
+
params.url = 'https://google.com'
|
|
22
|
+
params.margin = 0
|
|
23
|
+
params.format = 'A4'
|
|
24
|
+
|
|
25
|
+
pdf = client.convert(params)
|
|
26
|
+
|
|
27
|
+
pdf.save_to('/path/on/disk.pdf')
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## License
|
|
31
|
+
|
|
32
|
+
[MIT](https://choosealicense.com/licenses/mit/)
|
data/lib/bytes_result.rb
ADDED
data/lib/client.rb
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
require 'json'
|
|
2
|
+
require 'date'
|
|
3
|
+
require 'net/http'
|
|
4
|
+
require_relative 'conversion_params'
|
|
5
|
+
require_relative 'merge_params'
|
|
6
|
+
require_relative 'errors'
|
|
7
|
+
require_relative 'bytes_result'
|
|
8
|
+
require_relative 'conversion'
|
|
9
|
+
|
|
10
|
+
module PDFire
|
|
11
|
+
class Client
|
|
12
|
+
attr_reader :api_key
|
|
13
|
+
attr_accessor :base_url
|
|
14
|
+
attr_accessor :http
|
|
15
|
+
|
|
16
|
+
# @param api_key [String] the PDFire API key.
|
|
17
|
+
def initialize(api_key)
|
|
18
|
+
@api_key = api_key
|
|
19
|
+
@base_url = "https://api.pdfire.io"
|
|
20
|
+
uri = URI.parse(@base_url)
|
|
21
|
+
@http = Net::HTTP.new(uri.host, uri.port)
|
|
22
|
+
@http.use_ssl = true
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# @param params [ConversionParams, MergeParams] the conversion/merge parameters.
|
|
26
|
+
# @return [Result] the conversion result.
|
|
27
|
+
def convert(params)
|
|
28
|
+
map = params.to_map
|
|
29
|
+
|
|
30
|
+
request = Net::HTTP::Post.new(@base_url + "/v1/conversions", initheader = {
|
|
31
|
+
'Authorization': "Bearer #{@api_key}",
|
|
32
|
+
'Content-Type': 'application/json',
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
request.body = map.to_json
|
|
36
|
+
res = @http.request(request)
|
|
37
|
+
status = res.code.to_i
|
|
38
|
+
|
|
39
|
+
if status == 401
|
|
40
|
+
raise AuthenticationError
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
if status != 201
|
|
44
|
+
content_type = res.header['content-type']
|
|
45
|
+
|
|
46
|
+
if not content_type.include? 'application/json'
|
|
47
|
+
raise UnknownResponseError
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
raise APIError.new JSON.parse(res.body)["errors"]
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
self.result(res)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# @param params [ConversionParams, MergeParams] the conversion/merge parameters.
|
|
57
|
+
# @return [BytesResult] the conversion result.
|
|
58
|
+
def convert_to_bytes_result(params)
|
|
59
|
+
params.cdn = false
|
|
60
|
+
self.convert params
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
# @param params [ConversionParams, MergeParams] the conversion/merge parameters.
|
|
64
|
+
# @return [Array<byte>] the PDF bytes.
|
|
65
|
+
def convert_to_bytes(params)
|
|
66
|
+
self.convert_to_bytes_result(params).bytes()
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
# @param params [ConversionParams, MergeParams] the conversion/merge parameters.
|
|
70
|
+
# @return [Conversion] the conversion.
|
|
71
|
+
def convert_using_cdn(params)
|
|
72
|
+
params.cdn = true
|
|
73
|
+
self.convert params
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def result(response)
|
|
77
|
+
content_type = response.header['content-type']
|
|
78
|
+
|
|
79
|
+
if content_type.include? 'application/json'
|
|
80
|
+
return self.cdn_result(response)
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
BytesResult.new(response.body.bytes)
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def cdn_result(response)
|
|
87
|
+
body = JSON.parse(response.body)
|
|
88
|
+
created_at = Date.iso8601 body['createdAt']
|
|
89
|
+
converted_at = nil
|
|
90
|
+
|
|
91
|
+
if body['converted_at']
|
|
92
|
+
converted_at = Date.iso8601 body['convertedAt']
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
result = nil
|
|
96
|
+
|
|
97
|
+
if body['result']
|
|
98
|
+
result = Conversion::Result.new (Date.iso8601 body['result']['expiresAt']), body['result']['size'], body['result']['url']
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
conversion = Conversion.new created_at, converted_at, body['status'], body['error'], result
|
|
102
|
+
end
|
|
103
|
+
end
|
|
104
|
+
end
|
data/lib/conversion.rb
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
require 'httparty'
|
|
2
|
+
require_relative 'result'
|
|
3
|
+
|
|
4
|
+
module PDFire
|
|
5
|
+
class Conversion < Result
|
|
6
|
+
attr_reader :created_at, :converted_at, :status, :error, :result
|
|
7
|
+
|
|
8
|
+
def initialize(created_at, converted_at, status, error, result)
|
|
9
|
+
@created_at = created_at
|
|
10
|
+
@converted_at = converted_at
|
|
11
|
+
@status = status
|
|
12
|
+
@error = error
|
|
13
|
+
@result = result
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def bytes()
|
|
17
|
+
HTTParty.get(self.result.url).body.bytes
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
class Conversion::Result
|
|
22
|
+
attr_reader :expires_at, :size, :url
|
|
23
|
+
|
|
24
|
+
def initialize(expires_at, size, url)
|
|
25
|
+
@expires_at = expires_at
|
|
26
|
+
@size = size
|
|
27
|
+
@url = url
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
module PDFire
|
|
2
|
+
class ConversionParams
|
|
3
|
+
# @return [String]
|
|
4
|
+
attr_accessor :html
|
|
5
|
+
# @return [String]
|
|
6
|
+
attr_accessor :url
|
|
7
|
+
# @return [Boolean]
|
|
8
|
+
attr_accessor :cdn
|
|
9
|
+
# @return [Boolean]
|
|
10
|
+
attr_accessor :landscape
|
|
11
|
+
# @return [Boolean]
|
|
12
|
+
attr_accessor :print_background
|
|
13
|
+
# @return [Boolean]
|
|
14
|
+
attr_accessor :scale
|
|
15
|
+
# @return [Float]
|
|
16
|
+
attr_accessor :format
|
|
17
|
+
# @return [Integer, String]
|
|
18
|
+
attr_accessor :paper_width
|
|
19
|
+
# @return [Integer, String]
|
|
20
|
+
attr_accessor :paper_height
|
|
21
|
+
# @return [Integer, String]
|
|
22
|
+
attr_accessor :margin
|
|
23
|
+
# @return [Integer, String]
|
|
24
|
+
attr_accessor :margin_top
|
|
25
|
+
# @return [Integer, String]
|
|
26
|
+
attr_accessor :margin_right
|
|
27
|
+
# @return [Integer, String]
|
|
28
|
+
attr_accessor :margin_bottom
|
|
29
|
+
# @return [Integer, String]
|
|
30
|
+
attr_accessor :margin_left
|
|
31
|
+
# @return [String]
|
|
32
|
+
attr_accessor :page_ranges
|
|
33
|
+
# @return [String]
|
|
34
|
+
attr_accessor :header_template
|
|
35
|
+
# @return [String]
|
|
36
|
+
attr_accessor :footer_template
|
|
37
|
+
# @return [Boolean]
|
|
38
|
+
attr_accessor :prefer_css_page_size
|
|
39
|
+
# @return [Integer, String]
|
|
40
|
+
attr_accessor :viewport_width
|
|
41
|
+
# @return [Integer, String]
|
|
42
|
+
attr_accessor :viewport_height
|
|
43
|
+
# @return [Boolean]
|
|
44
|
+
attr_accessor :block_ads
|
|
45
|
+
# @return [String]
|
|
46
|
+
attr_accessor :selector
|
|
47
|
+
# @return [String]
|
|
48
|
+
attr_accessor :wait_for_selector
|
|
49
|
+
# @return [Integer]
|
|
50
|
+
attr_accessor :wait_for_selector_timeout
|
|
51
|
+
# @return [String]
|
|
52
|
+
attr_accessor :wait_until
|
|
53
|
+
# @return [Integer]
|
|
54
|
+
attr_accessor :wait_until_timeout
|
|
55
|
+
# @return [Integer]
|
|
56
|
+
attr_accessor :delay
|
|
57
|
+
# @return [Integer]
|
|
58
|
+
attr_accessor :timeout
|
|
59
|
+
# @return [Hash]
|
|
60
|
+
attr_accessor :headers
|
|
61
|
+
# @return [String]
|
|
62
|
+
attr_accessor :emulate_media
|
|
63
|
+
# @return [String]
|
|
64
|
+
attr_accessor :owner_password
|
|
65
|
+
# @return [String]
|
|
66
|
+
attr_accessor :user_password
|
|
67
|
+
|
|
68
|
+
# Set the page ranges of the PDF.
|
|
69
|
+
# @param ranges [Array<Integer, String>]
|
|
70
|
+
# @return [PDFire::ConversionParams]
|
|
71
|
+
def page_ranges(*ranges)
|
|
72
|
+
@page_ranges = ranges.join(',')
|
|
73
|
+
return self
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def to_map
|
|
77
|
+
map = {}
|
|
78
|
+
|
|
79
|
+
map['html'] = @html if @html != nil
|
|
80
|
+
map['url'] = @url if @url != nil
|
|
81
|
+
map['cdn'] = @cdn if @cdn != nil
|
|
82
|
+
map['landscape'] = @landscape if @landscape != nil
|
|
83
|
+
map['printBackground'] = @print_background if @print_background != nil
|
|
84
|
+
map['scale'] = @scale if @scale != nil
|
|
85
|
+
map['format'] = @format if @format != nil
|
|
86
|
+
map['paperWidth'] = @paper_width if @paper_width != nil
|
|
87
|
+
map['paperHeight'] = @paper_height if @paper_height != nil
|
|
88
|
+
map['margin'] = @margin if @margin != nil
|
|
89
|
+
map['marginTop'] = @margin_top if @margin_top != nil
|
|
90
|
+
map['marginRight'] = @margin_right if @margin_right != nil
|
|
91
|
+
map['marginBottom'] = @margin_bottom if @margin_bottom != nil
|
|
92
|
+
map['marginLeft'] = @margin_left if @margin_left != nil
|
|
93
|
+
map['pageRanges'] = @page_ranges if @page_ranges != nil
|
|
94
|
+
map['headerTemplate'] = @header_template if @header_template != nil
|
|
95
|
+
map['footerTemplate'] = @footer_template if @footer_template != nil
|
|
96
|
+
map['preferCSSPageSize'] = @prefer_css_page_size if @prefer_css_page_size != nil
|
|
97
|
+
map['viewportWidth'] = @viewport_width if @viewport_width != nil
|
|
98
|
+
map['viewportHeight'] = @viewport_height if @viewport_height != nil
|
|
99
|
+
map['blockAds'] = @block_ads if @block_ads != nil
|
|
100
|
+
map['selector'] = @selector if @selector != nil
|
|
101
|
+
map['waitForSelector'] = @wait_for_selector if @wait_for_selector != nil
|
|
102
|
+
map['waitForSelectorTimeout'] = @wait_for_selector_timeout if @wait_for_selector_timeout != nil
|
|
103
|
+
map['waitUntil'] = @wait_until if @wait_until != nil
|
|
104
|
+
map['waitUntilTimeout'] = @wait_until_timeout if @wait_until_timeout != nil
|
|
105
|
+
map['delay'] = @delay if @delay != nil
|
|
106
|
+
map['timeout'] = @timeout if @timeout != nil
|
|
107
|
+
map['headers'] = @headers if @headers != nil
|
|
108
|
+
map['emulateMedia'] = @emulate_media if @emulate_media != nil
|
|
109
|
+
map['ownerPassword'] = @owner_password if @owner_password != nil
|
|
110
|
+
map['userPassword'] = @user_password if @user_password != nil
|
|
111
|
+
|
|
112
|
+
return map
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
end
|
data/lib/errors.rb
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
module PDFire
|
|
2
|
+
class ApiError
|
|
3
|
+
attr_reader :message
|
|
4
|
+
end
|
|
5
|
+
|
|
6
|
+
class RequestError < StandardError
|
|
7
|
+
attr_reader :errors
|
|
8
|
+
attr_reader :message
|
|
9
|
+
|
|
10
|
+
def initialize(errors, default = 'Request error.')
|
|
11
|
+
@errors = errors
|
|
12
|
+
@message = errors.length == 0 ? errors[0].message : default
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
class InvalidRequestError < RequestError
|
|
17
|
+
def initialize(errors)
|
|
18
|
+
super(errors, 'Invalid request.')
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
class AuthenticationError < RequestError
|
|
23
|
+
def initialize(errors)
|
|
24
|
+
super(errors, 'Unauthenticated.')
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
class QuotaExceededError < RequestError
|
|
29
|
+
def initialize(errors)
|
|
30
|
+
super(errors, 'Quota exceeded.')
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
class ForbiddenActionError < RequestError
|
|
35
|
+
def initialize(errors)
|
|
36
|
+
super(errors, 'Forbidden action.')
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
class ConversionError < RequestError
|
|
41
|
+
def initialize(errors)
|
|
42
|
+
super(errors, 'Conversion failed.')
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
data/lib/merge_params.rb
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
module PDFire
|
|
2
|
+
class MergeParams
|
|
3
|
+
# @return [Array<PDFire::ConversionParams>]
|
|
4
|
+
attr_accessor :documents
|
|
5
|
+
# @return [Boolean]
|
|
6
|
+
attr_accessor :cdn
|
|
7
|
+
# @return [String]
|
|
8
|
+
attr_accessor :owner_password
|
|
9
|
+
# @return [String]
|
|
10
|
+
attr_accessor :user_password
|
|
11
|
+
|
|
12
|
+
def initialize(documents = [], cdn = nil, owner_password = nil, user_password = nil)
|
|
13
|
+
@documents = documents
|
|
14
|
+
@cdn = cdn
|
|
15
|
+
@owner_password = owner_password
|
|
16
|
+
@user_password = user_password
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def to_map
|
|
20
|
+
map = {}
|
|
21
|
+
map['documents'] = @documents.map { |doc| doc.to_map } if @documents.length > 0
|
|
22
|
+
map['cdn'] = @cdn if @cdn != nil
|
|
23
|
+
map['ownerPassword'] = @owner_password if @owner_password != nil
|
|
24
|
+
map['userPassword'] = @user_password if @user_password != nil
|
|
25
|
+
|
|
26
|
+
return map
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
data/lib/result.rb
ADDED
metadata
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: pdfire
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 1.0.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- modernice Ltd.
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2019-09-22 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description: PDFire API client for ruby. Get an API key at https://pdfire.io
|
|
14
|
+
email: info@modernice.ltd
|
|
15
|
+
executables: []
|
|
16
|
+
extensions: []
|
|
17
|
+
extra_rdoc_files: []
|
|
18
|
+
files:
|
|
19
|
+
- LICENSE
|
|
20
|
+
- README.md
|
|
21
|
+
- lib/bytes_result.rb
|
|
22
|
+
- lib/client.rb
|
|
23
|
+
- lib/conversion.rb
|
|
24
|
+
- lib/conversion_params.rb
|
|
25
|
+
- lib/errors.rb
|
|
26
|
+
- lib/merge_params.rb
|
|
27
|
+
- lib/result.rb
|
|
28
|
+
homepage: https://pdfire.io
|
|
29
|
+
licenses:
|
|
30
|
+
- MIT
|
|
31
|
+
metadata:
|
|
32
|
+
source-code-uri: https://github.com/modernice/pdfire-ruby
|
|
33
|
+
post_install_message:
|
|
34
|
+
rdoc_options: []
|
|
35
|
+
require_paths:
|
|
36
|
+
- lib
|
|
37
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
38
|
+
requirements:
|
|
39
|
+
- - ">="
|
|
40
|
+
- !ruby/object:Gem::Version
|
|
41
|
+
version: '0'
|
|
42
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
43
|
+
requirements:
|
|
44
|
+
- - ">="
|
|
45
|
+
- !ruby/object:Gem::Version
|
|
46
|
+
version: '0'
|
|
47
|
+
requirements: []
|
|
48
|
+
rubygems_version: 3.0.3
|
|
49
|
+
signing_key:
|
|
50
|
+
specification_version: 4
|
|
51
|
+
summary: PDFire API client.
|
|
52
|
+
test_files: []
|