convert_api187 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +12 -0
- data/.rspec +2 -0
- data/.ruby-version +1 -0
- data/.travis.yml +15 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +115 -0
- data/Rakefile +6 -0
- data/convert_api.gemspec +30 -0
- data/lib/convert_api/client.rb +142 -0
- data/lib/convert_api/configuration.rb +22 -0
- data/lib/convert_api/errors.rb +52 -0
- data/lib/convert_api/file_param.rb +20 -0
- data/lib/convert_api/format_detector.rb +32 -0
- data/lib/convert_api/result.rb +41 -0
- data/lib/convert_api/result_file.rb +71 -0
- data/lib/convert_api/task.rb +81 -0
- data/lib/convert_api/upload_io.rb +41 -0
- data/lib/convert_api/version.rb +3 -0
- data/lib/convert_api.rb +39 -0
- metadata +150 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.8.7
|
data/.travis.yml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
sudo: false
|
2
|
+
language: ruby
|
3
|
+
rvm:
|
4
|
+
- 2.1.0
|
5
|
+
- 2.2.0
|
6
|
+
- 2.3.0
|
7
|
+
- 2.4.1
|
8
|
+
- 2.5.1
|
9
|
+
- ruby-head
|
10
|
+
- ruby-head-clang
|
11
|
+
- jruby-head
|
12
|
+
bundler_args: --jobs=3 --retry=3 --binstubs
|
13
|
+
env:
|
14
|
+
global:
|
15
|
+
secure: Pri+nCT6ayGYkjGO/WfMGkqk4DBKseJjQuWHWdft9YH2G+5dc7UKypDqmlfZvqwLPw2kybepKfh/utRGYsL5nfbET+3Ml8+2Bgaf0IT4Ej/3B5KIdFbivHmRp/NP/Dgh64M+5u1wKEEnw1NIMHwcanQDzPUDtaxqsw1xwowUL5Axr+UnJtWL2bP/RCED2JEgwhBQBFSMBXXwpSK2yFT7X1pInPWX2S0hELxj1UDefLDjp6kEnkrU/u+FjC9RhmYhusx2muXC4pHiqdNL0fDZStgginAQAd92FsMHQyzwewefCGRAg0g25ZmSGx5cHL4BZORdUnxaG9+AS9Wt17tZI2WS9eUBc9yyRU+R76aSo79uJj4RKi1Ljd3+4fyI/9BDz9a2koEgeDbYUHHf0dYaFSLXcOpnLBBPtr0qD02SUP1EcYTOe1/sNMweDLfN67E70YV69+ZqQeb+xgCbAeeoTOGJ9uKR2c91wSZwDjpTXXSLrNeNw98FPRyWxpfRRFQV29ZDPx/cfHt9gAVQVnd+ZBCAFwr353nqE8Ux1wtrxkexPkT8G+TXIgTvvYDfu2WmaO7qrZR2D6427jc4sgndwWfSPgiLw4eW1u3hCyaCnPBsviugtj1sTs2NfnLPdgtSUcKBd4xNbbVQoqZM1qTL139HvpZFB+jD8uoVtgWigUA=
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Tomas Rutkauskas, Ian Heggie
|
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
|
13
|
+
all 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
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,115 @@
|
|
1
|
+
# ConvertAPI Ruby Client for ruby 1.8.7
|
2
|
+
|
3
|
+
## Convert your files with our online file conversion API
|
4
|
+
|
5
|
+
The ConvertAPI helps converting various file formats. Creating PDF and Images from various sources like Word, Excel, Powerpoint, images, web pages or raw HTML codes. Merge, Encrypt, Split, Repair and Decrypt PDF files. And many others files manipulations. In just few minutes you can integrate it into your application and use it easily.
|
6
|
+
|
7
|
+
**This is a clone that has been backported to ruby 1.8.7 by Ian Heggie**
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'convert_api187'
|
15
|
+
```
|
16
|
+
|
17
|
+
## Usage
|
18
|
+
|
19
|
+
### Configuration
|
20
|
+
|
21
|
+
You can get your secret at https://www.convertapi.com/a
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
ConvertApi.configure do |config|
|
25
|
+
config.api_secret = 'your-api-secret'
|
26
|
+
end
|
27
|
+
```
|
28
|
+
|
29
|
+
### File conversion
|
30
|
+
|
31
|
+
Example to convert file to PDF. All supported formats and options can be found
|
32
|
+
[here](https://www.convertapi.com).
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
result = ConvertApi.convert('pdf', :File => '/path/to/my_file.docx')
|
36
|
+
|
37
|
+
# save to file
|
38
|
+
result.file.save('/path/to/save/file.pdf')
|
39
|
+
```
|
40
|
+
|
41
|
+
Other result operations:
|
42
|
+
|
43
|
+
```ruby
|
44
|
+
# save all result files to folder
|
45
|
+
result.save_files('/path/to/save/files')
|
46
|
+
|
47
|
+
# get result file io
|
48
|
+
io = result.file.io
|
49
|
+
|
50
|
+
# get conversion cost
|
51
|
+
conversion_cost = result.conversion_cost
|
52
|
+
```
|
53
|
+
|
54
|
+
#### Convert file url
|
55
|
+
|
56
|
+
```ruby
|
57
|
+
result = ConvertApi.convert('pdf', :File => 'https://website/my_file.docx')
|
58
|
+
```
|
59
|
+
|
60
|
+
#### Specifying from format
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
result = ConvertApi.convert(
|
64
|
+
'pdf',
|
65
|
+
{ :File => /path/to/my_file' },
|
66
|
+
from_format: 'docx'
|
67
|
+
)
|
68
|
+
```
|
69
|
+
|
70
|
+
#### Additional conversion parameters
|
71
|
+
|
72
|
+
ConvertAPI accepts extra conversion parameters depending on converted formats. All conversion
|
73
|
+
parameters and explanations can be found [here](https://www.convertapi.com).
|
74
|
+
|
75
|
+
```ruby
|
76
|
+
result = ConvertApi.convert(
|
77
|
+
'pdf',
|
78
|
+
:File => /path/to/my_file.docx',
|
79
|
+
PageRange: '1-10',
|
80
|
+
PdfResolution: '150',
|
81
|
+
)
|
82
|
+
```
|
83
|
+
|
84
|
+
### User information
|
85
|
+
|
86
|
+
You can always check remaining seconds amount by fetching [user information](https://www.convertapi.com/doc/user).
|
87
|
+
|
88
|
+
```ruby
|
89
|
+
user_info = ConvertApi.user
|
90
|
+
|
91
|
+
puts user_info['SecondsLeft']
|
92
|
+
```
|
93
|
+
|
94
|
+
### More examples
|
95
|
+
|
96
|
+
You can find more advanced examples in the [examples/](examples) folder.
|
97
|
+
|
98
|
+
|
99
|
+
## Development
|
100
|
+
|
101
|
+
Run `CONVERT_API_SECRET=your_secret rake spec` to run the tests.
|
102
|
+
|
103
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
104
|
+
|
105
|
+
## Contributing
|
106
|
+
|
107
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/ConvertAPI/convertapi-ruby. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
108
|
+
|
109
|
+
## License
|
110
|
+
|
111
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
112
|
+
|
113
|
+
## Known Issues
|
114
|
+
|
115
|
+
* Uploading multiple files doesn't currently work -
|
data/Rakefile
ADDED
data/convert_api.gemspec
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
lib = File.expand_path('../lib', __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require 'convert_api/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'convert_api187'
|
7
|
+
spec.version = ConvertApi::VERSION
|
8
|
+
spec.authors = ['Tomas Rutkauskas', 'Ian Heggie']
|
9
|
+
spec.email = ['ian@heggie.biz']
|
10
|
+
|
11
|
+
spec.summary = %q{ConvertAPI client library backported to ruby 1.8.7}
|
12
|
+
spec.description = %q{Convert various files like MS Word, Excel, PowerPoint, Images to PDF and Images. Create PDF and Images from url and raw HTML. Extract and create PowerPoint presentation from PDF. Merge, Encrypt, Split, Repair and Decrypt PDF files. All supported files conversions and manipulations can be found at https://www.convertapi.com/doc/supported-formats [Backport to ruby 1.8.7]}
|
13
|
+
spec.homepage = 'https://github.com/ianheggie/convertapi-ruby'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.required_ruby_version = '~> 1.8.7'
|
17
|
+
|
18
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
19
|
+
f.match(%r{^(test|spec|features|examples)/})
|
20
|
+
end
|
21
|
+
|
22
|
+
spec.bindir = 'exe'
|
23
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
24
|
+
spec.require_paths = ['lib']
|
25
|
+
|
26
|
+
spec.add_development_dependency 'bundler', '~> 1.16'
|
27
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
28
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
29
|
+
spec.add_development_dependency 'json', '~> 1.7.7'
|
30
|
+
end
|
@@ -0,0 +1,142 @@
|
|
1
|
+
require 'net/https'
|
2
|
+
require 'uri'
|
3
|
+
require 'cgi'
|
4
|
+
require 'rubygems'
|
5
|
+
require 'json'
|
6
|
+
require 'zlib'
|
7
|
+
|
8
|
+
module ConvertApi
|
9
|
+
class Client
|
10
|
+
NET_HTTP_EXCEPTIONS = [
|
11
|
+
IOError,
|
12
|
+
Errno::ECONNABORTED,
|
13
|
+
Errno::ECONNREFUSED,
|
14
|
+
Errno::ECONNRESET,
|
15
|
+
Errno::EHOSTUNREACH,
|
16
|
+
Errno::EINVAL,
|
17
|
+
Errno::ENETUNREACH,
|
18
|
+
Errno::EPIPE,
|
19
|
+
Net::HTTPBadResponse,
|
20
|
+
Net::HTTPHeaderSyntaxError,
|
21
|
+
Net::ProtocolError,
|
22
|
+
SocketError,
|
23
|
+
Zlib::GzipFile::Error
|
24
|
+
]
|
25
|
+
|
26
|
+
USER_AGENT = "ConvertAPI-Ruby/#{VERSION}"
|
27
|
+
|
28
|
+
DEFAULT_HEADERS = {
|
29
|
+
'User-Agent' => USER_AGENT,
|
30
|
+
'Accept' => 'application/json'
|
31
|
+
}
|
32
|
+
|
33
|
+
def get(path, params = {}, options = {})
|
34
|
+
handle_response do
|
35
|
+
request = Net::HTTP::Get.new(request_uri(path, params), DEFAULT_HEADERS)
|
36
|
+
|
37
|
+
http(options).request(request)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def post(path, params, options = {})
|
42
|
+
handle_response do
|
43
|
+
request = Net::HTTP::Post.new(request_uri(path), DEFAULT_HEADERS)
|
44
|
+
request.form_data = build_form_data(params)
|
45
|
+
|
46
|
+
http(options).request(request)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def upload(io, filename)
|
51
|
+
handle_response do
|
52
|
+
request_uri = base_uri.path + 'upload'
|
53
|
+
encoded_filename = URI.encode(filename)
|
54
|
+
|
55
|
+
headers = DEFAULT_HEADERS.merge(
|
56
|
+
'Content-Type' => 'application/octet-stream',
|
57
|
+
'Transfer-Encoding' => 'chunked',
|
58
|
+
'Content-Disposition' => "attachment; filename*=UTF-8''#{encoded_filename}"
|
59
|
+
)
|
60
|
+
|
61
|
+
request = Net::HTTP::Post.new(request_uri, headers)
|
62
|
+
request.body_stream = io
|
63
|
+
|
64
|
+
http(:read_timeout => config.upload_timeout).request(request)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
private
|
69
|
+
|
70
|
+
def handle_response
|
71
|
+
handle_http_exceptions do
|
72
|
+
response = yield
|
73
|
+
status = response.code.to_i
|
74
|
+
|
75
|
+
if status != 200
|
76
|
+
headers = { }
|
77
|
+
response.each_header{|key,value| headers[key] = value }
|
78
|
+
raise(
|
79
|
+
ClientError,
|
80
|
+
:status => status,
|
81
|
+
:body => response.body,
|
82
|
+
:headers => headers
|
83
|
+
)
|
84
|
+
end
|
85
|
+
|
86
|
+
JSON.parse(response.body)
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
def handle_http_exceptions
|
91
|
+
yield
|
92
|
+
rescue *NET_HTTP_EXCEPTIONS => e
|
93
|
+
raise(ConnectionFailed, e)
|
94
|
+
rescue Timeout::Error, Errno::ETIMEDOUT => e
|
95
|
+
raise(TimeoutError, e)
|
96
|
+
end
|
97
|
+
|
98
|
+
def http(params = {})
|
99
|
+
http = Net::HTTP.new(base_uri.host, base_uri.port)
|
100
|
+
http.open_timeout = config.connect_timeout
|
101
|
+
http.read_timeout = params[:read_timeout] || config.read_timeout
|
102
|
+
http.use_ssl = base_uri.scheme == 'https'
|
103
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE if http.use_ssl && RUBY_VERSION == '1.8.7'
|
104
|
+
# http.set_debug_output $stderr
|
105
|
+
http
|
106
|
+
end
|
107
|
+
|
108
|
+
def request_uri(path, params = {})
|
109
|
+
raise(SecretError, 'API secret not configured') if config.api_secret.nil?
|
110
|
+
|
111
|
+
params_with_secret = params.merge(:Secret => config.api_secret)
|
112
|
+
query = params_with_secret.map do |key, value|
|
113
|
+
"#{key}=#{CGI.escape(value)}"
|
114
|
+
end.join('&')
|
115
|
+
#query = URI.encode_www_form(params_with_secret)
|
116
|
+
|
117
|
+
base_uri.path + path + '?' + query
|
118
|
+
end
|
119
|
+
|
120
|
+
def build_form_data(params)
|
121
|
+
data = {}
|
122
|
+
|
123
|
+
params.each do |key, value|
|
124
|
+
if value.is_a?(Array)
|
125
|
+
value.each_with_index { |v, i| data["#{key}[#{i}]"] = v }
|
126
|
+
else
|
127
|
+
data[key] = value
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
data
|
132
|
+
end
|
133
|
+
|
134
|
+
def base_uri
|
135
|
+
config.base_uri
|
136
|
+
end
|
137
|
+
|
138
|
+
def config
|
139
|
+
ConvertApi.config
|
140
|
+
end
|
141
|
+
end
|
142
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module ConvertApi
|
2
|
+
class Configuration
|
3
|
+
attr_accessor :api_secret
|
4
|
+
attr_accessor :base_uri
|
5
|
+
attr_accessor :connect_timeout
|
6
|
+
attr_accessor :read_timeout
|
7
|
+
attr_accessor :conversion_timeout
|
8
|
+
attr_accessor :conversion_timeout_delta
|
9
|
+
attr_accessor :upload_timeout
|
10
|
+
attr_accessor :download_timeout
|
11
|
+
|
12
|
+
def initialize
|
13
|
+
@base_uri = URI('https://v2.convertapi.com/')
|
14
|
+
@connect_timeout = 5
|
15
|
+
@read_timeout = 60
|
16
|
+
@conversion_timeout = 180
|
17
|
+
@conversion_timeout_delta = 10
|
18
|
+
@upload_timeout = 600
|
19
|
+
@download_timeout = 600
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
module ConvertApi
|
2
|
+
class Error < StandardError; end
|
3
|
+
class SecretError < Error; end
|
4
|
+
class FileNameError < Error; end
|
5
|
+
class TimeoutError < Error; end
|
6
|
+
class ConnectionFailed < Error; end
|
7
|
+
class FormatError < Error; end
|
8
|
+
|
9
|
+
class ClientError < Error
|
10
|
+
attr_reader :response
|
11
|
+
|
12
|
+
def initialize(response)
|
13
|
+
@response = response
|
14
|
+
end
|
15
|
+
|
16
|
+
def to_s
|
17
|
+
return "the server responded with status #{http_status}" unless json?
|
18
|
+
|
19
|
+
"#{error_message} Code: #{code}. #{invalid_parameters}".strip
|
20
|
+
end
|
21
|
+
|
22
|
+
def error_message
|
23
|
+
response_json['Message']
|
24
|
+
end
|
25
|
+
|
26
|
+
def code
|
27
|
+
response_json['Code']
|
28
|
+
end
|
29
|
+
|
30
|
+
def invalid_parameters
|
31
|
+
response_json['InvalidParameters']
|
32
|
+
end
|
33
|
+
|
34
|
+
def http_status
|
35
|
+
response[:status]
|
36
|
+
end
|
37
|
+
|
38
|
+
def response_json
|
39
|
+
@response_json ||= begin
|
40
|
+
JSON.parse(response[:body])
|
41
|
+
rescue JSON::ParserError
|
42
|
+
{}
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
private
|
47
|
+
|
48
|
+
def json?
|
49
|
+
response[:headers]['content-type'] =~ /json/
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module ConvertApi
|
2
|
+
module FileParam
|
3
|
+
module_function
|
4
|
+
|
5
|
+
def build(value)
|
6
|
+
case value
|
7
|
+
when UploadIO, URI_REGEXP
|
8
|
+
value
|
9
|
+
when Result
|
10
|
+
value.file.url
|
11
|
+
when ResultFile
|
12
|
+
value.url
|
13
|
+
when IO
|
14
|
+
UploadIO.new(value)
|
15
|
+
else
|
16
|
+
UploadIO.new(File.open(value))
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module ConvertApi
|
2
|
+
class FormatDetector
|
3
|
+
def initialize(resource)
|
4
|
+
@resource = resource
|
5
|
+
end
|
6
|
+
|
7
|
+
def run
|
8
|
+
return @resource.file_ext.downcase if @resource.is_a?(UploadIO)
|
9
|
+
|
10
|
+
format_from_path
|
11
|
+
end
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
def format_from_path
|
16
|
+
extension = File.extname(path).downcase
|
17
|
+
format = extension[1..-1]
|
18
|
+
format || raise(FormatError, 'Unable to detect format')
|
19
|
+
end
|
20
|
+
|
21
|
+
def path
|
22
|
+
case @resource
|
23
|
+
when String
|
24
|
+
URI(@resource).path
|
25
|
+
when File
|
26
|
+
@resource.path
|
27
|
+
else
|
28
|
+
''
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
module ConvertApi
|
2
|
+
class Result
|
3
|
+
attr_reader :response
|
4
|
+
|
5
|
+
def initialize(response)
|
6
|
+
@response = response
|
7
|
+
end
|
8
|
+
|
9
|
+
def conversion_cost
|
10
|
+
response['ConversionCost']
|
11
|
+
end
|
12
|
+
|
13
|
+
def file
|
14
|
+
files.first
|
15
|
+
end
|
16
|
+
|
17
|
+
def files
|
18
|
+
@files ||= response['Files'].map{ |file_info| ResultFile.new(file_info) }
|
19
|
+
end
|
20
|
+
|
21
|
+
def urls
|
22
|
+
files.map{|o| o.url}
|
23
|
+
end
|
24
|
+
|
25
|
+
def save_files(path)
|
26
|
+
if ConvertApi::USE_THREADS
|
27
|
+
threads = files.map do |file|
|
28
|
+
Thread.new { file.save(path) }
|
29
|
+
end
|
30
|
+
|
31
|
+
threads.map do |thread|
|
32
|
+
thread.value
|
33
|
+
end
|
34
|
+
else
|
35
|
+
files.map do |file|
|
36
|
+
file.save(path)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
require 'net/https'
|
2
|
+
require 'uri'
|
3
|
+
|
4
|
+
module ConvertApi
|
5
|
+
class ResultFile
|
6
|
+
attr_reader :info
|
7
|
+
|
8
|
+
def initialize(info)
|
9
|
+
@info = info
|
10
|
+
end
|
11
|
+
|
12
|
+
def url
|
13
|
+
info['Url']
|
14
|
+
end
|
15
|
+
|
16
|
+
def filename
|
17
|
+
info['FileName']
|
18
|
+
end
|
19
|
+
|
20
|
+
def size
|
21
|
+
info['FileSize']
|
22
|
+
end
|
23
|
+
|
24
|
+
def http(params = {})
|
25
|
+
http = Net::HTTP.new(base_uri.host, base_uri.port)
|
26
|
+
http.open_timeout = config.connect_timeout
|
27
|
+
http.read_timeout = params[:read_timeout] || config.read_timeout
|
28
|
+
http.use_ssl = base_uri.scheme == 'https'
|
29
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE if http.use_ssl && RUBY_VERSION == '1.8.7'
|
30
|
+
# http.set_debug_output $stderr
|
31
|
+
http
|
32
|
+
end
|
33
|
+
|
34
|
+
def save(path)
|
35
|
+
path = File.join(path, filename) if File.directory?(path)
|
36
|
+
|
37
|
+
request = Net::HTTP::Get.new(url, Client::DEFAULT_HEADERS)
|
38
|
+
|
39
|
+
response = http({}).request(request)
|
40
|
+
|
41
|
+
status = response.code.to_i
|
42
|
+
|
43
|
+
if status != 200
|
44
|
+
headers = { }
|
45
|
+
response.each_header{|key,value| headers[key] = value }
|
46
|
+
raise(
|
47
|
+
ClientError,
|
48
|
+
:status => status,
|
49
|
+
:body => response.body,
|
50
|
+
:headers => headers
|
51
|
+
)
|
52
|
+
end
|
53
|
+
File.open(path, 'w') do |f|
|
54
|
+
f.write response.body
|
55
|
+
end
|
56
|
+
|
57
|
+
path
|
58
|
+
end
|
59
|
+
|
60
|
+
private
|
61
|
+
|
62
|
+
def config
|
63
|
+
ConvertApi.config
|
64
|
+
end
|
65
|
+
|
66
|
+
def base_uri
|
67
|
+
config.base_uri
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
71
|
+
end
|
@@ -0,0 +1,81 @@
|
|
1
|
+
module ConvertApi
|
2
|
+
class Task
|
3
|
+
def initialize(from_format, to_format, params, options = { })
|
4
|
+
@from_format = from_format
|
5
|
+
@to_format = to_format
|
6
|
+
@params = params
|
7
|
+
@conversion_timeout = options[:conversion_timeout] || config.conversion_timeout
|
8
|
+
end
|
9
|
+
|
10
|
+
def run
|
11
|
+
params = normalize_params(@params).merge(
|
12
|
+
:Timeout => @conversion_timeout,
|
13
|
+
:StoreFile => true
|
14
|
+
)
|
15
|
+
|
16
|
+
from_format = @from_format || detect_format(params)
|
17
|
+
read_timeout = @conversion_timeout + config.conversion_timeout_delta
|
18
|
+
|
19
|
+
response = ConvertApi.client.post(
|
20
|
+
"convert/#{from_format}/to/#{@to_format}",
|
21
|
+
params,
|
22
|
+
:read_timeout => read_timeout
|
23
|
+
)
|
24
|
+
|
25
|
+
Result.new(response)
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def normalize_params(params)
|
31
|
+
result = {}
|
32
|
+
|
33
|
+
symbolize_keys(params).each do |key, value|
|
34
|
+
case key
|
35
|
+
when :File
|
36
|
+
result[:File] = FileParam.build(value)
|
37
|
+
when :Files
|
38
|
+
result[:Files] = files_batch(value)
|
39
|
+
else
|
40
|
+
result[key] = value
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
result
|
45
|
+
end
|
46
|
+
|
47
|
+
def symbolize_keys(hash)
|
48
|
+
hash.map { |k, v| [k.to_sym, v] }.to_h
|
49
|
+
end
|
50
|
+
|
51
|
+
def files_batch(values)
|
52
|
+
files = Array(values).map { |file| FileParam.build(file) }
|
53
|
+
|
54
|
+
if ConvertApi::USE_THREADS
|
55
|
+
# upload files in parallel
|
56
|
+
files.
|
57
|
+
select { |file| file.is_a?(UploadIO) }.
|
58
|
+
map { |upload_io| Thread.new { upload_io.file_id } }.
|
59
|
+
map{|thread| thread.join}
|
60
|
+
else
|
61
|
+
files.select { |file| file.is_a?(UploadIO) }.each do |upload_io|
|
62
|
+
upload_io.file_id
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
files
|
67
|
+
end
|
68
|
+
|
69
|
+
def detect_format(params)
|
70
|
+
return DEFAULT_URL_FORMAT if params[:Url]
|
71
|
+
|
72
|
+
resource = params[:File] || Array(params[:Files]).first
|
73
|
+
|
74
|
+
FormatDetector.new(resource).run
|
75
|
+
end
|
76
|
+
|
77
|
+
def config
|
78
|
+
ConvertApi.config
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'uri'
|
2
|
+
|
3
|
+
module ConvertApi
|
4
|
+
class UploadIO
|
5
|
+
def initialize(io, filename = nil)
|
6
|
+
@io = io
|
7
|
+
@filename = filename || io_filename || raise(FileNameError, 'IO filename must be provided')
|
8
|
+
end
|
9
|
+
|
10
|
+
def to_s
|
11
|
+
file_id
|
12
|
+
end
|
13
|
+
|
14
|
+
def file_id
|
15
|
+
result['FileId']
|
16
|
+
end
|
17
|
+
|
18
|
+
def file_name
|
19
|
+
result['FileName']
|
20
|
+
end
|
21
|
+
|
22
|
+
def file_ext
|
23
|
+
result['FileExt']
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def result
|
29
|
+
@result ||= upload_file
|
30
|
+
end
|
31
|
+
|
32
|
+
def upload_file
|
33
|
+
ConvertApi.client.upload(@io, @filename)
|
34
|
+
end
|
35
|
+
|
36
|
+
def io_filename
|
37
|
+
return unless @io.respond_to?(:path)
|
38
|
+
File.basename(@io.path)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
data/lib/convert_api.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'convert_api/version'
|
2
|
+
require 'convert_api/array_to_h'
|
3
|
+
require 'convert_api/configuration'
|
4
|
+
require 'convert_api/task'
|
5
|
+
require 'convert_api/client'
|
6
|
+
require 'convert_api/errors'
|
7
|
+
require 'convert_api/result'
|
8
|
+
require 'convert_api/result_file'
|
9
|
+
require 'convert_api/upload_io'
|
10
|
+
require 'convert_api/file_param'
|
11
|
+
require 'convert_api/format_detector'
|
12
|
+
|
13
|
+
module ConvertApi
|
14
|
+
URI_REGEXP = URI::regexp(%w(http https))
|
15
|
+
DEFAULT_URL_FORMAT = 'url'
|
16
|
+
USE_THREADS = true
|
17
|
+
|
18
|
+
module_function
|
19
|
+
|
20
|
+
def configure
|
21
|
+
yield(config)
|
22
|
+
end
|
23
|
+
|
24
|
+
def config
|
25
|
+
@config ||= Configuration.new
|
26
|
+
end
|
27
|
+
|
28
|
+
def convert(to_format, params, options = { })
|
29
|
+
Task.new(options[:from_format], to_format, params, :conversion_timeout => options[:conversion_timeout]).run
|
30
|
+
end
|
31
|
+
|
32
|
+
def user
|
33
|
+
client.get('user')
|
34
|
+
end
|
35
|
+
|
36
|
+
def client
|
37
|
+
Thread.current[:convert_api_client] ||= Client.new
|
38
|
+
end
|
39
|
+
end
|
metadata
ADDED
@@ -0,0 +1,150 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: convert_api187
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 19
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 1
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 1.1.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Tomas Rutkauskas
|
14
|
+
- Ian Heggie
|
15
|
+
autorequire:
|
16
|
+
bindir: exe
|
17
|
+
cert_chain: []
|
18
|
+
|
19
|
+
date: 2018-10-11 00:00:00 +11:00
|
20
|
+
default_executable:
|
21
|
+
dependencies:
|
22
|
+
- !ruby/object:Gem::Dependency
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ~>
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
hash: 47
|
29
|
+
segments:
|
30
|
+
- 1
|
31
|
+
- 16
|
32
|
+
version: "1.16"
|
33
|
+
version_requirements: *id001
|
34
|
+
prerelease: false
|
35
|
+
type: :development
|
36
|
+
name: bundler
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ~>
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
hash: 35
|
44
|
+
segments:
|
45
|
+
- 10
|
46
|
+
- 0
|
47
|
+
version: "10.0"
|
48
|
+
version_requirements: *id002
|
49
|
+
prerelease: false
|
50
|
+
type: :development
|
51
|
+
name: rake
|
52
|
+
- !ruby/object:Gem::Dependency
|
53
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
55
|
+
requirements:
|
56
|
+
- - ~>
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
hash: 7
|
59
|
+
segments:
|
60
|
+
- 3
|
61
|
+
- 0
|
62
|
+
version: "3.0"
|
63
|
+
version_requirements: *id003
|
64
|
+
prerelease: false
|
65
|
+
type: :development
|
66
|
+
name: rspec
|
67
|
+
- !ruby/object:Gem::Dependency
|
68
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
69
|
+
none: false
|
70
|
+
requirements:
|
71
|
+
- - ~>
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
hash: 5
|
74
|
+
segments:
|
75
|
+
- 1
|
76
|
+
- 7
|
77
|
+
- 7
|
78
|
+
version: 1.7.7
|
79
|
+
version_requirements: *id004
|
80
|
+
prerelease: false
|
81
|
+
type: :development
|
82
|
+
name: json
|
83
|
+
description: Convert various files like MS Word, Excel, PowerPoint, Images to PDF and Images. Create PDF and Images from url and raw HTML. Extract and create PowerPoint presentation from PDF. Merge, Encrypt, Split, Repair and Decrypt PDF files. All supported files conversions and manipulations can be found at https://www.convertapi.com/doc/supported-formats [Backport to ruby 1.8.7]
|
84
|
+
email:
|
85
|
+
- ian@heggie.biz
|
86
|
+
executables: []
|
87
|
+
|
88
|
+
extensions: []
|
89
|
+
|
90
|
+
extra_rdoc_files: []
|
91
|
+
|
92
|
+
files:
|
93
|
+
- .gitignore
|
94
|
+
- .rspec
|
95
|
+
- .ruby-version
|
96
|
+
- .travis.yml
|
97
|
+
- Gemfile
|
98
|
+
- LICENSE.txt
|
99
|
+
- README.md
|
100
|
+
- Rakefile
|
101
|
+
- convert_api.gemspec
|
102
|
+
- lib/convert_api.rb
|
103
|
+
- lib/convert_api/client.rb
|
104
|
+
- lib/convert_api/configuration.rb
|
105
|
+
- lib/convert_api/errors.rb
|
106
|
+
- lib/convert_api/file_param.rb
|
107
|
+
- lib/convert_api/format_detector.rb
|
108
|
+
- lib/convert_api/result.rb
|
109
|
+
- lib/convert_api/result_file.rb
|
110
|
+
- lib/convert_api/task.rb
|
111
|
+
- lib/convert_api/upload_io.rb
|
112
|
+
- lib/convert_api/version.rb
|
113
|
+
has_rdoc: true
|
114
|
+
homepage: https://github.com/ianheggie/convertapi-ruby
|
115
|
+
licenses:
|
116
|
+
- MIT
|
117
|
+
post_install_message:
|
118
|
+
rdoc_options: []
|
119
|
+
|
120
|
+
require_paths:
|
121
|
+
- lib
|
122
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
123
|
+
none: false
|
124
|
+
requirements:
|
125
|
+
- - ~>
|
126
|
+
- !ruby/object:Gem::Version
|
127
|
+
hash: 57
|
128
|
+
segments:
|
129
|
+
- 1
|
130
|
+
- 8
|
131
|
+
- 7
|
132
|
+
version: 1.8.7
|
133
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
134
|
+
none: false
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
hash: 3
|
139
|
+
segments:
|
140
|
+
- 0
|
141
|
+
version: "0"
|
142
|
+
requirements: []
|
143
|
+
|
144
|
+
rubyforge_project:
|
145
|
+
rubygems_version: 1.6.2
|
146
|
+
signing_key:
|
147
|
+
specification_version: 3
|
148
|
+
summary: ConvertAPI client library backported to ruby 1.8.7
|
149
|
+
test_files: []
|
150
|
+
|