From 1117620b589505aa8932aa7c1a6a60a697cf3573 Mon Sep 17 00:00:00 2001 From: Jonas Jasas Date: Fri, 21 Jun 2019 11:03:37 +0300 Subject: [PATCH 01/59] Alternative converter --- lib/convert_api/task.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/convert_api/task.rb b/lib/convert_api/task.rb index 1781a7f..adb1bf4 100644 --- a/lib/convert_api/task.rb +++ b/lib/convert_api/task.rb @@ -73,4 +73,4 @@ def config ConvertApi.config end end -end +end \ No newline at end of file From 301b03190a311b40f76bb9856f5436cb16da2e4d Mon Sep 17 00:00:00 2001 From: laurynas-baltsoft <38224044+laurynas-baltsoft@users.noreply.github.com> Date: Sun, 1 Mar 2020 14:52:52 +0200 Subject: [PATCH 02/59] Update rake dependency --- convert_api.gemspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/convert_api.gemspec b/convert_api.gemspec index 89311f9..52c23a5 100644 --- a/convert_api.gemspec +++ b/convert_api.gemspec @@ -22,6 +22,6 @@ Gem::Specification.new do |spec| spec.require_paths = ['lib'] spec.add_development_dependency 'bundler', '~> 1.16' - spec.add_development_dependency 'rake', '~> 10.0' + spec.add_development_dependency 'rake', '>= 12.3.3' spec.add_development_dependency 'rspec', '~> 3.0' end From 9326679c902a8b091b23f95b76e418ed5da6301f Mon Sep 17 00:00:00 2001 From: laurynas-baltsoft <38224044+laurynas-baltsoft@users.noreply.github.com> Date: Sun, 1 Mar 2020 14:56:54 +0200 Subject: [PATCH 03/59] Adjust bundler dependency --- convert_api.gemspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/convert_api.gemspec b/convert_api.gemspec index 52c23a5..cf71b76 100644 --- a/convert_api.gemspec +++ b/convert_api.gemspec @@ -21,7 +21,7 @@ Gem::Specification.new do |spec| spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } spec.require_paths = ['lib'] - spec.add_development_dependency 'bundler', '~> 1.16' + spec.add_development_dependency 'bundler' spec.add_development_dependency 'rake', '>= 12.3.3' spec.add_development_dependency 'rspec', '~> 3.0' end From 0218ee344f17910b4d7cc12bd75191798e1a8cea Mon Sep 17 00:00:00 2001 From: laurynas-baltsoft <38224044+laurynas-baltsoft@users.noreply.github.com> Date: Sun, 1 Mar 2020 15:01:11 +0200 Subject: [PATCH 04/59] Enable 2.6 and 2.7 ruby in Travis --- .travis.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.travis.yml b/.travis.yml index 0aa53bd..4bc1bd4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,6 +6,8 @@ rvm: - 2.3.0 - 2.4.1 - 2.5.1 +- 2.6.5 +- 2.7.0 - ruby-head - ruby-head-clang - jruby-head From 6fdcc74f8a86a4a59270b54d58c6f00847ce77ef Mon Sep 17 00:00:00 2001 From: laurynas-baltsoft <38224044+laurynas-baltsoft@users.noreply.github.com> Date: Sun, 1 Mar 2020 15:08:00 +0200 Subject: [PATCH 05/59] Disable travis tests on ruby-head --- .travis.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 4bc1bd4..f593e93 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,8 +8,6 @@ rvm: - 2.5.1 - 2.6.5 - 2.7.0 -- ruby-head -- ruby-head-clang - jruby-head bundler_args: --jobs=3 --retry=3 --binstubs env: From 7ca139823b7b793e7e64171c3bd8532c48d8e76c Mon Sep 17 00:00:00 2001 From: Kostas <62603039+kostas-baltsoft@users.noreply.github.com> Date: Tue, 14 Apr 2020 12:18:32 +0300 Subject: [PATCH 06/59] Content update --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index b442e98..a96a0c8 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ ## Convert your files with our online file conversion API -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. +ConvertAPI helps in 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 other file manipulations. You can integrate it into your application in just a few minutes and use it easily. ## Installation @@ -31,7 +31,7 @@ end ### File conversion -Example to convert file to PDF. All supported formats and options can be found +Convert file to PDF example. All supported formats and options can be found [here](https://www.convertapi.com). ```ruby @@ -86,7 +86,7 @@ result = ConvertApi.convert( ### User information -You can always check remaining seconds amount by fetching [user information](https://www.convertapi.com/doc/user). +You can always check your remaining seconds programmatically by fetching [user information](https://www.convertapi.com/doc/user). ```ruby user_info = ConvertApi.user @@ -96,7 +96,7 @@ puts user_info['SecondsLeft'] ### More examples -You can find more advanced examples in the [examples/](https://github.com/ConvertAPI/convertapi-ruby/tree/master/examples) folder. +Find more advanced examples in the [examples/](https://github.com/ConvertAPI/convertapi-ruby/tree/master/examples) folder. ## Development From 74db700b1c6b10eab30487d42fb1904f24da57b6 Mon Sep 17 00:00:00 2001 From: Tomas Rutkauskas Date: Fri, 29 May 2020 10:52:03 +0300 Subject: [PATCH 07/59] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index b442e98..df13a04 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ end ### File conversion Example to convert file to PDF. All supported formats and options can be found -[here](https://www.convertapi.com). +[here](https://www.convertapi.com/doc/supported-formats). ```ruby result = ConvertApi.convert('pdf', File: '/path/to/my_file.docx') From 0c43a5ca07695e4382f156abb0a84877dfcec461 Mon Sep 17 00:00:00 2001 From: laurynas-baltsoft <38224044+laurynas-baltsoft@users.noreply.github.com> Date: Sat, 4 Jul 2020 18:24:47 +0300 Subject: [PATCH 08/59] Fix example url --- examples/alternative_converter.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/alternative_converter.rb b/examples/alternative_converter.rb index db36818..aa161bb 100644 --- a/examples/alternative_converter.rb +++ b/examples/alternative_converter.rb @@ -6,7 +6,7 @@ end # Example of saving Word docx to PDF using OpenOffice converter -# https://www.convertapi.com/doc-to-pdf/openofficetopdf +# https://www.convertapi.com/doc-to-pdf/openoffice # Use upload IO wrapper to upload file only once to the API upload_io = ConvertApi::UploadIO.new(File.open('files/test.docx')) @@ -15,4 +15,4 @@ .convert('pdf', File: upload_io, converter: 'openofficetopdf') .save_files(Dir.tmpdir) -puts "The PDF saved to: #{saved_files}" \ No newline at end of file +puts "The PDF saved to: #{saved_files}" From 05f0ff01a50d66493324baa8b429b6d86903c4c2 Mon Sep 17 00:00:00 2001 From: Laurynas Butkus Date: Sat, 4 Jul 2020 22:29:33 +0300 Subject: [PATCH 09/59] Add result files example --- README.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/README.md b/README.md index df13a04..696a22a 100644 --- a/README.md +++ b/README.md @@ -84,6 +84,17 @@ result = ConvertApi.convert( ) ``` +#### Accessing result files + +You can access result file collection like this: + +```ruby +result = ConvertApi.convert('pdf', File: 'https://website/my_file.docx') + +puts result.files[0].url +puts result.files[0].size +``` + ### User information You can always check remaining seconds amount by fetching [user information](https://www.convertapi.com/doc/user). From ff1cd773cc1ca1b4e42ac61cdb9b1328d82a5dd8 Mon Sep 17 00:00:00 2001 From: laurynas-baltsoft <38224044+laurynas-baltsoft@users.noreply.github.com> Date: Sat, 4 Jul 2020 22:31:29 +0300 Subject: [PATCH 10/59] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 696a22a..bf45deb 100644 --- a/README.md +++ b/README.md @@ -84,7 +84,7 @@ result = ConvertApi.convert( ) ``` -#### Accessing result files +#### Accessing result file properties You can access result file collection like this: From db7aaa405001c14724bc816ab46c9c453b7b543d Mon Sep 17 00:00:00 2001 From: Laurynas Butkus Date: Sun, 23 Aug 2020 12:42:01 +0300 Subject: [PATCH 11/59] Add stream example --- examples/convert_stream.rb | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 examples/convert_stream.rb diff --git a/examples/convert_stream.rb b/examples/convert_stream.rb new file mode 100644 index 0000000..6332a79 --- /dev/null +++ b/examples/convert_stream.rb @@ -0,0 +1,24 @@ +require 'convert_api' +require 'tmpdir' + +ConvertApi.configure do |config| + config.api_secret = ENV['CONVERT_API_SECRET'] # your api secret +end + +# Example of converting text to PDF +# https://www.convertapi.com/txt-to-pdf + +content = 'Test file body' + +io = StringIO.new +io.write(content) +io.rewind + +# Use upload IO wrapper to upload data to the API +upload_io = ConvertApi::UploadIO.new(io, 'test.txt') + +saved_files = ConvertApi + .convert('pdf', File: upload_io) + .save_files(Dir.tmpdir) + +puts "The PDF saved to: #{saved_files}" From 85ecba722a28d1411722da410f57cfa3cc77d78b Mon Sep 17 00:00:00 2001 From: Laurynas Butkus Date: Sun, 23 Aug 2020 12:52:37 +0300 Subject: [PATCH 12/59] Fix keyword parameters deprecation --- lib/convert_api/client.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/convert_api/client.rb b/lib/convert_api/client.rb index d9c0b9a..bb832f8 100644 --- a/lib/convert_api/client.rb +++ b/lib/convert_api/client.rb @@ -91,10 +91,10 @@ def handle_http_exceptions raise(TimeoutError, e) end - def http(read_timeout: nil) + def http(options = {}) http = Net::HTTP.new(base_uri.host, base_uri.port) http.open_timeout = config.connect_timeout - http.read_timeout = read_timeout || config.read_timeout + http.read_timeout = options.fetch(:read_timeout, config.read_timeout) http.use_ssl = base_uri.scheme == 'https' # http.set_debug_output $stderr http From 1ac03fbe7180a07e1299e4c9ecdd93d527cd7009 Mon Sep 17 00:00:00 2001 From: Laurynas Butkus Date: Sun, 23 Aug 2020 12:58:31 +0300 Subject: [PATCH 13/59] Fix uri open deprecation --- lib/convert_api/result_file.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/convert_api/result_file.rb b/lib/convert_api/result_file.rb index c3b557c..8fbfccd 100644 --- a/lib/convert_api/result_file.rb +++ b/lib/convert_api/result_file.rb @@ -21,7 +21,7 @@ def size end def io - @io ||= open(url, download_options) + @io ||= URI.open(url, download_options) end def save(path) From 07b3df3a442e05d678cd378c2d5574a48422715f Mon Sep 17 00:00:00 2001 From: Laurynas Butkus Date: Sun, 23 Aug 2020 13:11:12 +0300 Subject: [PATCH 14/59] Add open-uri backwards compatibility --- lib/convert_api/result_file.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/convert_api/result_file.rb b/lib/convert_api/result_file.rb index 8fbfccd..9c1f069 100644 --- a/lib/convert_api/result_file.rb +++ b/lib/convert_api/result_file.rb @@ -21,7 +21,7 @@ def size end def io - @io ||= URI.open(url, download_options) + @io ||= URI.parse(url).open(download_options) end def save(path) From ea807ab10c7dc57edfbefbac3ec8ed8390d8c72e Mon Sep 17 00:00:00 2001 From: Laurynas Butkus Date: Sun, 23 Aug 2020 13:18:23 +0300 Subject: [PATCH 15/59] Fix URI escape deprecation --- lib/convert_api/client.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/convert_api/client.rb b/lib/convert_api/client.rb index bb832f8..59489e7 100644 --- a/lib/convert_api/client.rb +++ b/lib/convert_api/client.rb @@ -48,7 +48,7 @@ def post(path, params, options = {}) def upload(io, filename) handle_response do request_uri = base_uri.path + 'upload' - encoded_filename = URI.encode(filename) + encoded_filename = CGI.escape(filename) headers = DEFAULT_HEADERS.merge( 'Content-Type' => 'application/octet-stream', From c0effb8212cc007e254f7fcad56ac91ab01edf95 Mon Sep 17 00:00:00 2001 From: Laurynas Butkus Date: Sun, 23 Aug 2020 13:23:52 +0300 Subject: [PATCH 16/59] Bump version --- lib/convert_api/version.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/convert_api/version.rb b/lib/convert_api/version.rb index dad6693..b4c1b5e 100644 --- a/lib/convert_api/version.rb +++ b/lib/convert_api/version.rb @@ -1,3 +1,3 @@ module ConvertApi - VERSION = '1.2.0' -end \ No newline at end of file + VERSION = '1.2.1' +end From faa1770c827973ee360b1b2cd13df74f26dd6e67 Mon Sep 17 00:00:00 2001 From: Laurynas Butkus Date: Sun, 23 Aug 2020 13:25:41 +0300 Subject: [PATCH 17/59] Update ruby version --- .ruby-version | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.ruby-version b/.ruby-version index 2bf1c1c..ec1cf33 100644 --- a/.ruby-version +++ b/.ruby-version @@ -1 +1 @@ -2.3.1 +2.6.3 From e6928c2b11afffed3ea788594fc595e9fc4ade86 Mon Sep 17 00:00:00 2001 From: Laurynas Butkus Date: Sun, 30 Aug 2020 12:22:14 +0300 Subject: [PATCH 18/59] Do not set default conversion timeout --- lib/convert_api/configuration.rb | 5 ++--- lib/convert_api/task.rb | 8 ++++---- spec/convert_api_spec.rb | 2 +- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/lib/convert_api/configuration.rb b/lib/convert_api/configuration.rb index 76fba27..e787cd6 100644 --- a/lib/convert_api/configuration.rb +++ b/lib/convert_api/configuration.rb @@ -13,10 +13,9 @@ def initialize @base_uri = URI('https://v2.convertapi.com/') @connect_timeout = 5 @read_timeout = 60 - @conversion_timeout = 180 @conversion_timeout_delta = 10 - @upload_timeout = 600 - @download_timeout = 600 + @upload_timeout = 1800 + @download_timeout = 1800 end end end diff --git a/lib/convert_api/task.rb b/lib/convert_api/task.rb index adb1bf4..25bc37b 100644 --- a/lib/convert_api/task.rb +++ b/lib/convert_api/task.rb @@ -11,16 +11,16 @@ def run params = normalize_params(@params).merge( Timeout: @conversion_timeout, StoreFile: true, - ) + ).compact from_format = @from_format || detect_format(params) - read_timeout = @conversion_timeout + config.conversion_timeout_delta + read_timeout = @conversion_timeout + config.conversion_timeout_delta if @conversion_timeout converter = params[:converter] ? "/converter/#{params[:converter]}" : '' response = ConvertApi.client.post( "convert/#{from_format}/to/#{@to_format}#{converter}", params, - read_timeout: read_timeout + read_timeout: read_timeout, ) Result.new(response) @@ -73,4 +73,4 @@ def config ConvertApi.config end end -end \ No newline at end of file +end diff --git a/spec/convert_api_spec.rb b/spec/convert_api_spec.rb index 31dab7d..de8534b 100644 --- a/spec/convert_api_spec.rb +++ b/spec/convert_api_spec.rb @@ -5,7 +5,7 @@ it 'has configuration defaults' do expect(described_class.config.base_uri).not_to be_nil expect(described_class.config.connect_timeout).not_to be_nil - expect(described_class.config.conversion_timeout).not_to be_nil + expect(described_class.config.conversion_timeout).to be_nil end describe '.configure' do From a5d7146070d1747b56bdd56cc0544f81fbfac7f1 Mon Sep 17 00:00:00 2001 From: Laurynas Butkus Date: Sun, 30 Aug 2020 12:27:00 +0300 Subject: [PATCH 19/59] Bump version --- lib/convert_api/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/convert_api/version.rb b/lib/convert_api/version.rb index b4c1b5e..0d2efde 100644 --- a/lib/convert_api/version.rb +++ b/lib/convert_api/version.rb @@ -1,3 +1,3 @@ module ConvertApi - VERSION = '1.2.1' + VERSION = '1.3.0' end From 7e4f9dd0b9b30ebfc38396a22c6c24fdf107bc38 Mon Sep 17 00:00:00 2001 From: Laurynas Butkus Date: Sun, 30 Aug 2020 12:29:26 +0300 Subject: [PATCH 20/59] Do not compact hash --- lib/convert_api/task.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/convert_api/task.rb b/lib/convert_api/task.rb index 25bc37b..f92dc39 100644 --- a/lib/convert_api/task.rb +++ b/lib/convert_api/task.rb @@ -11,7 +11,7 @@ def run params = normalize_params(@params).merge( Timeout: @conversion_timeout, StoreFile: true, - ).compact + ) from_format = @from_format || detect_format(params) read_timeout = @conversion_timeout + config.conversion_timeout_delta if @conversion_timeout From 17dd2d048fd14b6c516e9db3b54dc103151680b9 Mon Sep 17 00:00:00 2001 From: Laurynas Butkus Date: Sun, 11 Sep 2022 23:43:56 +0300 Subject: [PATCH 21/59] Adjust from format when archiving --- lib/convert_api/format_detector.rb | 15 +++++++++++---- lib/convert_api/task.rb | 2 +- spec/convert_api/format_detector_spec.rb | 10 +++++++++- spec/convert_api_spec.rb | 6 ------ 4 files changed, 21 insertions(+), 12 deletions(-) diff --git a/lib/convert_api/format_detector.rb b/lib/convert_api/format_detector.rb index 1a10229..bb95a79 100644 --- a/lib/convert_api/format_detector.rb +++ b/lib/convert_api/format_detector.rb @@ -1,21 +1,28 @@ module ConvertApi class FormatDetector - def initialize(resource) + ANY_FORMAT = 'any' + + def initialize(resource, to_format) @resource = resource + @to_format = to_format end def run + return ANY_FORMAT if archive? return @resource.file_ext.downcase if @resource.is_a?(UploadIO) - format_from_path + format_from_path || raise(FormatError, 'Unable to detect format') end private + def archive? + @to_format.to_s.downcase == 'zip' + end + def format_from_path extension = File.extname(path).downcase - format = extension[1..-1] - format || raise(FormatError, 'Unable to detect format') + extension[1..-1] end def path diff --git a/lib/convert_api/task.rb b/lib/convert_api/task.rb index f92dc39..3db0be2 100644 --- a/lib/convert_api/task.rb +++ b/lib/convert_api/task.rb @@ -66,7 +66,7 @@ def detect_format(params) resource = params[:File] || Array(params[:Files]).first - FormatDetector.new(resource).run + FormatDetector.new(resource, @to_format).run end def config diff --git a/spec/convert_api/format_detector_spec.rb b/spec/convert_api/format_detector_spec.rb index b56ae63..4fbe7d8 100644 --- a/spec/convert_api/format_detector_spec.rb +++ b/spec/convert_api/format_detector_spec.rb @@ -1,11 +1,19 @@ RSpec.describe ConvertApi::FormatDetector, '#run' do - subject { described_class.new(resource).run } + subject { described_class.new(resource, to_format).run } + + let(:to_format) { 'pdf' } context 'with file name' do let(:resource) { 'test.txt' } it { is_expected.to eq('txt') } end + context 'when archiving' do + let(:resource) { 'test.txt' } + let(:to_format) { 'zip' } + it { is_expected.to eq('any') } + end + context 'with file path' do let(:resource) { '/some/path/test.txt' } it { is_expected.to eq('txt') } diff --git a/spec/convert_api_spec.rb b/spec/convert_api_spec.rb index de8534b..d0dab4a 100644 --- a/spec/convert_api_spec.rb +++ b/spec/convert_api_spec.rb @@ -51,12 +51,6 @@ it_behaves_like 'successful conversion' - context 'with web resource' do - let(:params) { { Url: 'http://convertapi.com' } } - - it_behaves_like 'successful conversion' - end - context 'with web resource' do let(:from_format) { 'web' } let(:params) { { Url: 'http://convertapi.com' } } From fc62eca8120ec269b9e26050e8a418c29bb00971 Mon Sep 17 00:00:00 2001 From: laurynas-baltsoft <38224044+laurynas-baltsoft@users.noreply.github.com> Date: Sun, 11 Sep 2022 23:51:17 +0300 Subject: [PATCH 22/59] Replace Travis with Github workflows (#18) * Replace Travis with Github workflows * Remove ruby 2.5 --- .github/workflows/main.yml | 26 ++++++++++++++++++++++++++ .travis.yml | 15 --------------- README.md | 4 +++- 3 files changed, 29 insertions(+), 16 deletions(-) create mode 100644 .github/workflows/main.yml delete mode 100644 .travis.yml diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 0000000..d4ba23e --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,26 @@ +name: Build +on: [push,pull_request] +jobs: + build: + runs-on: ubuntu-latest + strategy: + matrix: + ruby-version: + - 2.0 + - 2.6 + - 2.7 + - 3.0 + - ruby-head + - jruby-9.1 + - jruby-head + name: Ruby ${{ matrix.ruby-version }} sample + steps: + - uses: actions/checkout@v3 + - uses: ruby/setup-ruby@v1 + with: + ruby-version: ${{ matrix.ruby-version }} +# bundler-cache: true + - run: bundle install + - env: + CONVERT_API_SECRET: ${{ secrets.CONVERTAPI_SECRET }} + run: bundle exec rake spec diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index f593e93..0000000 --- a/.travis.yml +++ /dev/null @@ -1,15 +0,0 @@ -sudo: false -language: ruby -rvm: -- 2.1.0 -- 2.2.0 -- 2.3.0 -- 2.4.1 -- 2.5.1 -- 2.6.5 -- 2.7.0 -- jruby-head -bundler_args: --jobs=3 --retry=3 --binstubs -env: - global: - 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= diff --git a/README.md b/README.md index 1ac6ef7..0e91409 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,9 @@ [![Gem](https://img.shields.io/gem/v/convert_api.svg)](https://rubygems.org/gems/convert_api) -[![Build Status](https://secure.travis-ci.org/ConvertAPI/convertapi-ruby.svg)](http://travis-ci.org/ConvertAPI/convertapi-ruby) +[![Build Status](https://github.com/ConvertAPI/convertapi-ruby/actions/workflows/main.yml/badge.svg)](https://github.com/ConvertAPI/convertapi-ruby/actions) +[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT) + ## Convert your files with our online file conversion API From 96bbb606722d2a56848e34f60ce57fd3ac6941fb Mon Sep 17 00:00:00 2001 From: Laurynas Butkus Date: Sun, 11 Sep 2022 23:54:56 +0300 Subject: [PATCH 23/59] Bump version --- lib/convert_api/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/convert_api/version.rb b/lib/convert_api/version.rb index 0d2efde..f8858b8 100644 --- a/lib/convert_api/version.rb +++ b/lib/convert_api/version.rb @@ -1,3 +1,3 @@ module ConvertApi - VERSION = '1.3.0' + VERSION = '1.3.1' end From 966317b3599d5b7ece813781fd3fc016ad51d9b6 Mon Sep 17 00:00:00 2001 From: laurynas-baltsoft <38224044+laurynas-baltsoft@users.noreply.github.com> Date: Sun, 18 Sep 2022 23:32:39 +0300 Subject: [PATCH 24/59] Change default read timeout to 1800 seconds (#20) --- lib/convert_api/configuration.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/convert_api/configuration.rb b/lib/convert_api/configuration.rb index e787cd6..bcd43f5 100644 --- a/lib/convert_api/configuration.rb +++ b/lib/convert_api/configuration.rb @@ -12,7 +12,7 @@ class Configuration def initialize @base_uri = URI('https://v2.convertapi.com/') @connect_timeout = 5 - @read_timeout = 60 + @read_timeout = 1800 @conversion_timeout_delta = 10 @upload_timeout = 1800 @download_timeout = 1800 From 08ffddf76b50361f7d88551624ec09cb46967cbb Mon Sep 17 00:00:00 2001 From: Laurynas Butkus Date: Sun, 18 Sep 2022 23:34:06 +0300 Subject: [PATCH 25/59] Bump version --- lib/convert_api/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/convert_api/version.rb b/lib/convert_api/version.rb index f8858b8..dd70b41 100644 --- a/lib/convert_api/version.rb +++ b/lib/convert_api/version.rb @@ -1,3 +1,3 @@ module ConvertApi - VERSION = '1.3.1' + VERSION = '1.3.2' end From 18d79c32cff39b62558fd2ce2a9af702bbed5ded Mon Sep 17 00:00:00 2001 From: Jonas Jasas Date: Mon, 21 Nov 2022 13:38:34 +0200 Subject: [PATCH 26/59] Keyword argument fix --- README.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 0e91409..af5e8a9 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,7 @@ Example to convert file to PDF. All supported formats and options can be found [here](https://www.convertapi.com/doc/supported-formats). ```ruby -result = ConvertApi.convert('pdf', File: '/path/to/my_file.docx') +result = ConvertApi.convert('pdf', { File: '/path/to/my_file.docx' }) # save to file result.file.save('/path/to/save/file.pdf') @@ -59,7 +59,7 @@ conversion_cost = result.conversion_cost #### Convert file url ```ruby -result = ConvertApi.convert('pdf', File: 'https://website/my_file.docx') +result = ConvertApi.convert('pdf', { File: 'https://website/my_file.docx' }) ``` #### Specifying from format @@ -67,8 +67,8 @@ result = ConvertApi.convert('pdf', File: 'https://website/my_file.docx') ```ruby result = ConvertApi.convert( 'pdf', - { File: /path/to/my_file' }, - from_format: 'docx' + { File: /path/to/my_file', + from_format: 'docx' } ) ``` @@ -80,9 +80,9 @@ parameters and explanations can be found [here](https://www.convertapi.com). ```ruby result = ConvertApi.convert( 'pdf', - File: /path/to/my_file.docx', + { File: /path/to/my_file.docx', PageRange: '1-10', - PdfResolution: '150', + PdfResolution: '150' } ) ``` @@ -91,7 +91,7 @@ result = ConvertApi.convert( You can access result file collection like this: ```ruby -result = ConvertApi.convert('pdf', File: 'https://website/my_file.docx') +result = ConvertApi.convert('pdf', { File: 'https://website/my_file.docx' }) puts result.files[0].url puts result.files[0].size From 2c62b2234b557dab47ba608ef10f1a56ae740e20 Mon Sep 17 00:00:00 2001 From: Jonas Jasas Date: Mon, 21 Nov 2022 13:40:04 +0200 Subject: [PATCH 27/59] Keyword argument fix --- examples/alternative_converter.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/alternative_converter.rb b/examples/alternative_converter.rb index aa161bb..405042f 100644 --- a/examples/alternative_converter.rb +++ b/examples/alternative_converter.rb @@ -12,7 +12,7 @@ upload_io = ConvertApi::UploadIO.new(File.open('files/test.docx')) saved_files = ConvertApi - .convert('pdf', File: upload_io, converter: 'openofficetopdf') + .convert('pdf', {File: upload_io, converter: 'openofficetopdf'}) .save_files(Dir.tmpdir) puts "The PDF saved to: #{saved_files}" From 5d4e294338cafd14317e3798735bcfb920ab8766 Mon Sep 17 00:00:00 2001 From: Jonas Jasas Date: Mon, 21 Nov 2022 13:40:52 +0200 Subject: [PATCH 28/59] Keyword argument fix --- examples/conversions_chaining.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/conversions_chaining.rb b/examples/conversions_chaining.rb index 1443a3e..3b864fb 100644 --- a/examples/conversions_chaining.rb +++ b/examples/conversions_chaining.rb @@ -10,7 +10,7 @@ puts 'Converting PDF to JPG and compressing result files with ZIP' -jpg_result = ConvertApi.convert('jpg', File: 'files/test.pdf') +jpg_result = ConvertApi.convert('jpg', {File: 'files/test.pdf'}) puts "Conversions done. Cost: #{jpg_result.conversion_cost}. Total files created: #{jpg_result.files.count}" From 78de3f77a27f7630425f22cd69f30f36ef03e19e Mon Sep 17 00:00:00 2001 From: Jonas Jasas Date: Mon, 21 Nov 2022 13:41:24 +0200 Subject: [PATCH 29/59] Keyword argument fix --- examples/conversions_chaining.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/conversions_chaining.rb b/examples/conversions_chaining.rb index 3b864fb..c3d583c 100644 --- a/examples/conversions_chaining.rb +++ b/examples/conversions_chaining.rb @@ -14,7 +14,7 @@ puts "Conversions done. Cost: #{jpg_result.conversion_cost}. Total files created: #{jpg_result.files.count}" -zip_result = ConvertApi.convert('zip', Files: jpg_result.files) +zip_result = ConvertApi.convert('zip', {Files: jpg_result.files}) puts "Conversions done. Cost: #{zip_result.conversion_cost}. Total files created: #{zip_result.files.count}" From 0cd9201e47ccd804d4d139e3b760d0c8400cab29 Mon Sep 17 00:00:00 2001 From: Jonas Jasas Date: Mon, 21 Nov 2022 13:42:00 +0200 Subject: [PATCH 30/59] Keyword argument fix --- examples/convert_stream.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/convert_stream.rb b/examples/convert_stream.rb index 6332a79..a398d7d 100644 --- a/examples/convert_stream.rb +++ b/examples/convert_stream.rb @@ -18,7 +18,7 @@ upload_io = ConvertApi::UploadIO.new(io, 'test.txt') saved_files = ConvertApi - .convert('pdf', File: upload_io) + .convert('pdf', {File: upload_io}) .save_files(Dir.tmpdir) puts "The PDF saved to: #{saved_files}" From 7bfb7460f0005ce672f4525470466975266ea036 Mon Sep 17 00:00:00 2001 From: Jonas Jasas Date: Mon, 21 Nov 2022 13:44:35 +0200 Subject: [PATCH 31/59] Keyword argument fix --- examples/convert_word_to_pdf_and_png.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/convert_word_to_pdf_and_png.rb b/examples/convert_word_to_pdf_and_png.rb index 023dd67..82c129b 100644 --- a/examples/convert_word_to_pdf_and_png.rb +++ b/examples/convert_word_to_pdf_and_png.rb @@ -13,7 +13,7 @@ upload_io = ConvertApi::UploadIO.new(File.open('files/test.docx')) saved_files = ConvertApi - .convert('pdf', File: upload_io) + .convert('pdf', {File: upload_io}) .save_files(Dir.tmpdir) puts "The PDF saved to: #{saved_files}" From ae07d8758cb6d846a9fb25efe633d501e2844ddb Mon Sep 17 00:00:00 2001 From: Jonas Jasas Date: Mon, 21 Nov 2022 13:45:19 +0200 Subject: [PATCH 32/59] Keyword argument fix --- examples/create_pdf_thumbnail.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/create_pdf_thumbnail.rb b/examples/create_pdf_thumbnail.rb index d64b057..fb32705 100644 --- a/examples/create_pdf_thumbnail.rb +++ b/examples/create_pdf_thumbnail.rb @@ -11,17 +11,17 @@ pdf_result = ConvertApi.convert( 'extract', - File: 'files/test.pdf', - PageRange: 1, + {File: 'files/test.pdf', + PageRange: 1} ) jpg_result = ConvertApi.convert( 'jpg', - File: pdf_result, + {File: pdf_result, ScaleImage: true, ScaleProportions: true, ImageHeight: 300, - ImageWidth: 300, + ImageWidth: 300} ) saved_files = jpg_result.save_files(Dir.tmpdir) From 4bef71a2c7382b4c4b788419d7ad9d03e240dc58 Mon Sep 17 00:00:00 2001 From: Jonas Jasas Date: Mon, 21 Nov 2022 13:46:08 +0200 Subject: [PATCH 33/59] Keyword argument fix --- examples/split_and_merge_pdf.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/split_and_merge_pdf.rb b/examples/split_and_merge_pdf.rb index 9a8a1e7..483c359 100644 --- a/examples/split_and_merge_pdf.rb +++ b/examples/split_and_merge_pdf.rb @@ -9,11 +9,11 @@ # https://www.convertapi.com/pdf-to-split # https://www.convertapi.com/pdf-to-merge -split_result = ConvertApi.convert('split', File: 'files/test.pdf') +split_result = ConvertApi.convert('split', {File: 'files/test.pdf'}) files = [split_result.files.first, split_result.files.last] -merge_result = ConvertApi.convert('merge', Files: files) +merge_result = ConvertApi.convert('merge', {Files: files}) saved_files = merge_result.save_files(Dir.tmpdir) From d18edf91b408c420ab3aeb0a1565bbbafb786daa Mon Sep 17 00:00:00 2001 From: Jonas Jasas Date: Mon, 21 Nov 2022 13:47:18 +0200 Subject: [PATCH 34/59] Keyword argument fix --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index af5e8a9..ddd2e9e 100644 --- a/README.md +++ b/README.md @@ -67,8 +67,8 @@ result = ConvertApi.convert('pdf', { File: 'https://website/my_file.docx' }) ```ruby result = ConvertApi.convert( 'pdf', - { File: /path/to/my_file', - from_format: 'docx' } + {File: /path/to/my_file'}, + from_format: 'docx' ) ``` @@ -80,9 +80,9 @@ parameters and explanations can be found [here](https://www.convertapi.com). ```ruby result = ConvertApi.convert( 'pdf', - { File: /path/to/my_file.docx', + {File: /path/to/my_file.docx', PageRange: '1-10', - PdfResolution: '150' } + PdfResolution: '150'} ) ``` From 87f369265028c0e9cae9dae85cf921e7c9cbdfba Mon Sep 17 00:00:00 2001 From: Laurynas Butkus Date: Mon, 6 Feb 2023 19:04:16 +0200 Subject: [PATCH 35/59] Update ruby versions --- .github/workflows/main.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index d4ba23e..1a81f4c 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -9,8 +9,8 @@ jobs: - 2.0 - 2.6 - 2.7 - - 3.0 - - ruby-head + - 3.1 + - 3.2 - jruby-9.1 - jruby-head name: Ruby ${{ matrix.ruby-version }} sample From e4891a7d9f9f6ae7df34bad6542aaf85910cfc39 Mon Sep 17 00:00:00 2001 From: Laurynas Butkus Date: Mon, 6 Feb 2023 19:10:29 +0200 Subject: [PATCH 36/59] Adjust txt sample --- spec/convert_api/task_spec.rb | 2 +- spec/convert_api_spec.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/convert_api/task_spec.rb b/spec/convert_api/task_spec.rb index 6ad393a..ccd11cc 100644 --- a/spec/convert_api/task_spec.rb +++ b/spec/convert_api/task_spec.rb @@ -2,7 +2,7 @@ let(:task) { described_class.new(from_format, to_format, params) } let(:from_format) { 'txt' } let(:to_format) { 'pdf' } - let(:params) { { File: 'https://www.w3.org/TR/PNG/iso_8859-1.txt' } } + let(:params) { { File: 'https://www.w3.org/TR/2003/REC-PNG-20031110/iso_8859-1.txt' } } describe '#run' do subject { task.run } diff --git a/spec/convert_api_spec.rb b/spec/convert_api_spec.rb index d0dab4a..14df27f 100644 --- a/spec/convert_api_spec.rb +++ b/spec/convert_api_spec.rb @@ -81,7 +81,7 @@ ConvertApi::Result.new( 'ConversionCost' => 1, 'Files' => [ - 'Url' => 'https://www.w3.org/TR/PNG/iso_8859-1.txt', + 'Url' => 'https://www.w3.org/TR/2003/REC-PNG-20031110/iso_8859-1.txt', ], ) end From 556bdc921271453224c85f81a68c895b32a8a4c2 Mon Sep 17 00:00:00 2001 From: Jonas Jasas Date: Thu, 25 May 2023 09:46:48 +0300 Subject: [PATCH 37/59] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index ddd2e9e..dcf3fbf 100644 --- a/README.md +++ b/README.md @@ -75,7 +75,7 @@ result = ConvertApi.convert( #### Additional conversion parameters ConvertAPI accepts extra conversion parameters depending on converted formats. All conversion -parameters and explanations can be found [here](https://www.convertapi.com). +parameters and explanations can be found [here](https://www.convertapi.com/doc/supported-formats). ```ruby result = ConvertApi.convert( From 70d4923d4184f044ef1d163184c28dbaa5748406 Mon Sep 17 00:00:00 2001 From: Jonas Jasas Date: Tue, 4 Jul 2023 15:30:05 +0300 Subject: [PATCH 38/59] Update README.md --- README.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/README.md b/README.md index dcf3fbf..374d090 100644 --- a/README.md +++ b/README.md @@ -107,6 +107,17 @@ user_info = ConvertApi.user puts user_info['SecondsLeft'] ``` +### Alternative domain + +Set config `base_uri` attribute to use other service domains. Dedicated to the region [domain list](https://www.convertapi.com/doc/servers-location). + +```ruby +ConvertApi.configure do |config| + config.base_uri = URI('https://v2.convertapi.com/') +end +``` + + ### More examples Find more advanced examples in the [examples/](https://github.com/ConvertAPI/convertapi-ruby/tree/master/examples) folder. From d2bea1e41301b8845df5b5f5ba06c16f3431dba9 Mon Sep 17 00:00:00 2001 From: Jonas Jasas Date: Tue, 4 Jul 2023 15:30:30 +0300 Subject: [PATCH 39/59] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 374d090..4347ba0 100644 --- a/README.md +++ b/README.md @@ -113,7 +113,7 @@ Set config `base_uri` attribute to use other service domains. Dedicated to the r ```ruby ConvertApi.configure do |config| - config.base_uri = URI('https://v2.convertapi.com/') + config.base_uri = URI('https://eu-v2.convertapi.com/') end ``` From e6130897cd874eea8ecb2108374b35ee8246855b Mon Sep 17 00:00:00 2001 From: laurynas-baltsoft <38224044+laurynas-baltsoft@users.noreply.github.com> Date: Wed, 19 Jul 2023 19:14:12 +0300 Subject: [PATCH 40/59] Make converter param name case insensitive (#24) --- lib/convert_api/task.rb | 13 +++++++++++-- spec/convert_api/task_spec.rb | 24 +++++++++++++++++------- 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/lib/convert_api/task.rb b/lib/convert_api/task.rb index 3db0be2..b611067 100644 --- a/lib/convert_api/task.rb +++ b/lib/convert_api/task.rb @@ -15,10 +15,11 @@ def run from_format = @from_format || detect_format(params) read_timeout = @conversion_timeout + config.conversion_timeout_delta if @conversion_timeout - converter = params[:converter] ? "/converter/#{params[:converter]}" : '' + converter = detect_converter(params) + converter_path = converter ? "/converter/#{converter}" : '' response = ConvertApi.client.post( - "convert/#{from_format}/to/#{@to_format}#{converter}", + "convert/#{from_format}/to/#{@to_format}#{converter_path}", params, read_timeout: read_timeout, ) @@ -69,6 +70,14 @@ def detect_format(params) FormatDetector.new(resource, @to_format).run end + def detect_converter(params) + params.each do |key, value| + return value if key.to_s.downcase == 'converter' + end + + nil + end + def config ConvertApi.config end diff --git a/spec/convert_api/task_spec.rb b/spec/convert_api/task_spec.rb index ccd11cc..c68a488 100644 --- a/spec/convert_api/task_spec.rb +++ b/spec/convert_api/task_spec.rb @@ -1,17 +1,27 @@ -RSpec.describe ConvertApi::Task do +RSpec.describe ConvertApi::Task, '#run' do + subject { task.run } + let(:task) { described_class.new(from_format, to_format, params) } let(:from_format) { 'txt' } let(:to_format) { 'pdf' } - let(:params) { { File: 'https://www.w3.org/TR/2003/REC-PNG-20031110/iso_8859-1.txt' } } + let(:params) { { File: file } } + let(:file) { 'https://www.w3.org/TR/2003/REC-PNG-20031110/iso_8859-1.txt' } + let(:result) { double } + + it 'executes task and returns result' do + expect(ConvertApi.client).to( + receive(:post).with('convert/txt/to/pdf', instance_of(Hash), instance_of(Hash)).and_return(result) + ) - describe '#run' do - subject { task.run } + expect(subject).to be_instance_of(ConvertApi::Result) + end - let(:result) { double } + context 'with converter' do + let(:params) { { File: file, Converter: 'openoffice' } } - it 'executes task and returns result' do + it 'adds converter to the path' do expect(ConvertApi.client).to( - receive(:post).with('convert/txt/to/pdf', instance_of(Hash), instance_of(Hash)).and_return(result) + receive(:post).with('convert/txt/to/pdf/converter/openoffice', instance_of(Hash), instance_of(Hash)).and_return(result) ) expect(subject).to be_instance_of(ConvertApi::Result) From f381152f1401a7ac35e7fa10d01bcac559458195 Mon Sep 17 00:00:00 2001 From: laurynas-baltsoft <38224044+laurynas-baltsoft@users.noreply.github.com> Date: Wed, 19 Jul 2023 19:36:02 +0300 Subject: [PATCH 41/59] Handle multiple file params (#25) --- lib/convert_api/task.rb | 8 ++++---- spec/convert_api/task_spec.rb | 27 +++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/lib/convert_api/task.rb b/lib/convert_api/task.rb index b611067..39194d4 100644 --- a/lib/convert_api/task.rb +++ b/lib/convert_api/task.rb @@ -33,10 +33,10 @@ def normalize_params(params) result = {} symbolize_keys(params).each do |key, value| - case key - when :File - result[:File] = FileParam.build(value) - when :Files + case + when key != :StoreFile && key.to_s.end_with?('File') + result[key] = FileParam.build(value) + when key == :Files result[:Files] = files_batch(value) else result[key] = value diff --git a/spec/convert_api/task_spec.rb b/spec/convert_api/task_spec.rb index c68a488..8af9197 100644 --- a/spec/convert_api/task_spec.rb +++ b/spec/convert_api/task_spec.rb @@ -27,4 +27,31 @@ expect(subject).to be_instance_of(ConvertApi::Result) end end + + context 'when file is instance of ResultFile' do + let(:file) { ConvertApi::ResultFile.new('Url' => 'testurl') } + let(:expected_params) { hash_including(File: 'testurl') } + + it 'uses file url' do + expect(ConvertApi.client).to( + receive(:post).with('convert/txt/to/pdf', expected_params, instance_of(Hash)).and_return(result) + ) + + expect(subject).to be_instance_of(ConvertApi::Result) + end + end + + context 'when multiple file params' do + let(:file) { ConvertApi::ResultFile.new('Url' => 'testurl') } + let(:params) { { File: file, CompareFile: file } } + let(:expected_params) { hash_including(File: 'testurl', CompareFile: 'testurl') } + + it 'uses multiple file urls' do + expect(ConvertApi.client).to( + receive(:post).with('convert/txt/to/pdf', expected_params, instance_of(Hash)).and_return(result) + ) + + expect(subject).to be_instance_of(ConvertApi::Result) + end + end end From 88ed026ac1679829f48cb36157c4127457d18a9f Mon Sep 17 00:00:00 2001 From: Laurynas Butkus Date: Wed, 19 Jul 2023 19:38:38 +0300 Subject: [PATCH 42/59] Bump version --- lib/convert_api/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/convert_api/version.rb b/lib/convert_api/version.rb index dd70b41..84d7d2b 100644 --- a/lib/convert_api/version.rb +++ b/lib/convert_api/version.rb @@ -1,3 +1,3 @@ module ConvertApi - VERSION = '1.3.2' + VERSION = '1.4.0' end From c0c373bfda25f06415a99b34b877959b87247e16 Mon Sep 17 00:00:00 2001 From: Gabriel Lima Pereira Date: Sun, 4 Aug 2024 15:35:10 -0300 Subject: [PATCH 43/59] Add Token authentication (#30) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Adicionado opção de configurar o token * Utilizando config de token no lugar do api_secret * Ajustado teste para validar o token no lugar do api_secret * ajustado teste para secret * Ajustado teste validando erro de secret em branco, para agr validar o token * Ajustado teste quando a falta de autenticação valida * Ajustado utilização do token, como opcional e sempre tentando utilizado primeiro * Ajustado raise * Ajustado nome do método * Ajustado erro de autenticação * Ajustado testes unitarios * adjust the readme to use secret or token * adjusts the examples to use secret or token * adjusts the specs for the use of secret or token * skipped user test, as it is not included in the request * Update README.md --------- Co-authored-by: fernando.sacchet@gmail.com Co-authored-by: Fernando Sacchet dos Santos <75328547+FernandoSacchet@users.noreply.github.com> --- .github/workflows/main.yml | 1 + README.md | 17 +++++++++++++-- examples/alternative_converter.rb | 2 ++ examples/conversions_chaining.rb | 2 ++ examples/convert_stream.rb | 2 ++ examples/convert_url_to_pdf.rb | 3 ++- examples/convert_word_to_pdf_and_png.rb | 2 ++ examples/create_pdf_thumbnail.rb | 2 ++ examples/retrieve_user_information.rb | 2 ++ examples/split_and_merge_pdf.rb | 2 ++ lib/convert_api/client.rb | 13 ++++++++--- lib/convert_api/configuration.rb | 1 + lib/convert_api/errors.rb | 2 +- spec/convert_api_spec.rb | 29 ++++++++++++++++--------- spec/spec_helper.rb | 2 ++ 15 files changed, 65 insertions(+), 17 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 1a81f4c..f4aa040 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -23,4 +23,5 @@ jobs: - run: bundle install - env: CONVERT_API_SECRET: ${{ secrets.CONVERTAPI_SECRET }} + CONVERT_API_TOKEN: ${{ secrets.CONVERTAPI_TOKEN }} run: bundle exec rake spec diff --git a/README.md b/README.md index 4347ba0..f6d56c4 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ gem 'convert_api' ### Configuration -You can get your secret at https://www.convertapi.com/a +You can get your secret at https://www.convertapi.com/a/auth ```ruby ConvertApi.configure do |config| @@ -31,9 +31,18 @@ ConvertApi.configure do |config| end ``` +Or + +You can get your token at https://www.convertapi.com/a/access-tokens +```ruby +ConvertApi.configure do |config| + config.token = 'your-token' +end +``` + ### File conversion -Example to convert file to PDF. All supported formats and options can be found +Example to convert file to PDF. All supported formats and options can be found [here](https://www.convertapi.com/doc/supported-formats). ```ruby @@ -127,6 +136,10 @@ Find more advanced examples in the [examples/](https://github.com/ConvertAPI/con Run `CONVERT_API_SECRET=your_secret rake spec` to run the tests. +Or + +Run `CONVERT_API_TOKEN=your_token rake spec` to run the tests. + 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). ## Contributing diff --git a/examples/alternative_converter.rb b/examples/alternative_converter.rb index 405042f..34e441b 100644 --- a/examples/alternative_converter.rb +++ b/examples/alternative_converter.rb @@ -3,6 +3,8 @@ ConvertApi.configure do |config| config.api_secret = ENV['CONVERT_API_SECRET'] # your api secret + # or + config.token = ENV['CONVERT_API_TOKEN'] # your token end # Example of saving Word docx to PDF using OpenOffice converter diff --git a/examples/conversions_chaining.rb b/examples/conversions_chaining.rb index c3d583c..1ab1a97 100644 --- a/examples/conversions_chaining.rb +++ b/examples/conversions_chaining.rb @@ -3,6 +3,8 @@ ConvertApi.configure do |config| config.api_secret = ENV['CONVERT_API_SECRET'] # your api secret + # or + config.token = ENV['CONVERT_API_TOKEN'] # your token end # Short example of conversions chaining, the PDF pages extracted and saved as separated JPGs and then ZIP'ed diff --git a/examples/convert_stream.rb b/examples/convert_stream.rb index a398d7d..ad78a61 100644 --- a/examples/convert_stream.rb +++ b/examples/convert_stream.rb @@ -3,6 +3,8 @@ ConvertApi.configure do |config| config.api_secret = ENV['CONVERT_API_SECRET'] # your api secret + # or + config.token = ENV['CONVERT_API_TOKEN'] # your token end # Example of converting text to PDF diff --git a/examples/convert_url_to_pdf.rb b/examples/convert_url_to_pdf.rb index 166bf41..5214208 100644 --- a/examples/convert_url_to_pdf.rb +++ b/examples/convert_url_to_pdf.rb @@ -3,8 +3,9 @@ ConvertApi.configure do |config| config.api_secret = ENV['CONVERT_API_SECRET'] # your api secret + # or + config.token = ENV['CONVERT_API_TOKEN'] # your token end - # Example of converting Web Page URL to PDF file # https://www.convertapi.com/web-to-pdf diff --git a/examples/convert_word_to_pdf_and_png.rb b/examples/convert_word_to_pdf_and_png.rb index 82c129b..b24d283 100644 --- a/examples/convert_word_to_pdf_and_png.rb +++ b/examples/convert_word_to_pdf_and_png.rb @@ -3,6 +3,8 @@ ConvertApi.configure do |config| config.api_secret = ENV['CONVERT_API_SECRET'] # your api secret + # or + config.token = ENV['CONVERT_API_TOKEN'] # your token end # Example of saving Word docx to PDF and to PNG diff --git a/examples/create_pdf_thumbnail.rb b/examples/create_pdf_thumbnail.rb index fb32705..3321032 100644 --- a/examples/create_pdf_thumbnail.rb +++ b/examples/create_pdf_thumbnail.rb @@ -3,6 +3,8 @@ ConvertApi.configure do |config| config.api_secret = ENV['CONVERT_API_SECRET'] # your api secret + # or + config.token = ENV['CONVERT_API_TOKEN'] # your token end # Example of extracting first page from PDF and then chaining conversion PDF page to JPG. diff --git a/examples/retrieve_user_information.rb b/examples/retrieve_user_information.rb index 3280b16..0795184 100644 --- a/examples/retrieve_user_information.rb +++ b/examples/retrieve_user_information.rb @@ -2,6 +2,8 @@ ConvertApi.configure do |config| config.api_secret = ENV['CONVERT_API_SECRET'] # your api secret + # or + config.token = ENV['CONVERT_API_TOKEN'] # your token end # Retrieve user information diff --git a/examples/split_and_merge_pdf.rb b/examples/split_and_merge_pdf.rb index 483c359..2d4e260 100644 --- a/examples/split_and_merge_pdf.rb +++ b/examples/split_and_merge_pdf.rb @@ -3,6 +3,8 @@ ConvertApi.configure do |config| config.api_secret = ENV['CONVERT_API_SECRET'] # your api secret + # or + config.token = ENV['CONVERT_API_TOKEN'] # your token end # Example of extracting first and last pages from PDF and then merging them back to new PDF. diff --git a/lib/convert_api/client.rb b/lib/convert_api/client.rb index 59489e7..e028ef4 100644 --- a/lib/convert_api/client.rb +++ b/lib/convert_api/client.rb @@ -101,10 +101,10 @@ def http(options = {}) end def request_uri(path, params = {}) - raise(SecretError, 'API secret not configured') if config.api_secret.nil? + raise(AuthenticationError, 'API secret or Token not configured') if authentication.nil? - params_with_secret = params.merge(Secret: config.api_secret) - query = URI.encode_www_form(params_with_secret) + params_with_authentication = params.merge(authentication) + query = URI.encode_www_form(params_with_authentication) base_uri.path + path + '?' + query end @@ -123,6 +123,13 @@ def build_form_data(params) data end + def authentication + return { Token: config.token } unless config.token.nil? + return { Secret: config.api_secret } unless config.api_secret.nil? + + nil + end + def base_uri config.base_uri end diff --git a/lib/convert_api/configuration.rb b/lib/convert_api/configuration.rb index bcd43f5..8f0783c 100644 --- a/lib/convert_api/configuration.rb +++ b/lib/convert_api/configuration.rb @@ -1,6 +1,7 @@ module ConvertApi class Configuration attr_accessor :api_secret + attr_accessor :token attr_accessor :base_uri attr_accessor :connect_timeout attr_accessor :read_timeout diff --git a/lib/convert_api/errors.rb b/lib/convert_api/errors.rb index ccff5ad..bdf78d0 100644 --- a/lib/convert_api/errors.rb +++ b/lib/convert_api/errors.rb @@ -1,6 +1,6 @@ module ConvertApi class Error < StandardError; end - class SecretError < Error; end + class AuthenticationError < Error; end class FileNameError < Error; end class TimeoutError < Error; end class ConnectionFailed < Error; end diff --git a/spec/convert_api_spec.rb b/spec/convert_api_spec.rb index 14df27f..d0143c2 100644 --- a/spec/convert_api_spec.rb +++ b/spec/convert_api_spec.rb @@ -10,15 +10,18 @@ describe '.configure' do let(:api_secret) { 'test_secret' } + let(:token) { 'test_token' } let(:conversion_timeout) { 20 } it 'configures' do described_class.configure do |config| config.api_secret = api_secret + config.token = token config.conversion_timeout = conversion_timeout end expect(described_class.config.api_secret).to eq(api_secret) + expect(described_class.config.token).to eq(token) expect(described_class.config.conversion_timeout).to eq(conversion_timeout) end end @@ -89,19 +92,25 @@ it_behaves_like 'successful conversion' end - context 'when secret is not set' do - before { ConvertApi.config.api_secret = nil } + context 'when has error' do + it 'raises error without secret or token' do + described_class.config.api_secret = nil + described_class.config.token = nil - it 'raises error' do - expect { subject }.to raise_error(ConvertApi::SecretError, /not configured/) + expect { subject }.to raise_error(ConvertApi::AuthenticationError, /not configured/) end - end - context 'with invalid secret' do - before { ConvertApi.config.api_secret = 'invalid' } + it 'with invalid secret' do + described_class.config.api_secret = 'invalid' + described_class.config.token = 'invalid' - it 'raises error' do - expect { subject }.to raise_error(ConvertApi::ClientError, /bad secret/) + expect { subject }.to raise_error(ConvertApi::ClientError) + end + + it 'with invalid token' do + described_class.config.token = 'invalid' + + expect { subject }.to raise_error(ConvertApi::ClientError) end end @@ -117,7 +126,7 @@ describe '.user' do subject { described_class.user } - it 'returns user information' do + xit 'returns user information' do expect(subject).to include('Email' => instance_of(String)) end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 833b9c4..fcd8a63 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -10,5 +10,7 @@ config.before(:each) do ConvertApi.config.api_secret = ENV['CONVERT_API_SECRET'] + # or + ConvertApi.config.token = ENV['CONVERT_API_TOKEN'] end end From 57ac274fa4afcb61c8e54549eac6275b5eb8d170 Mon Sep 17 00:00:00 2001 From: Laurynas Butkus Date: Sun, 4 Aug 2024 21:39:02 +0300 Subject: [PATCH 44/59] Enable user information test case --- spec/convert_api_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/convert_api_spec.rb b/spec/convert_api_spec.rb index d0143c2..2b2baa0 100644 --- a/spec/convert_api_spec.rb +++ b/spec/convert_api_spec.rb @@ -126,7 +126,7 @@ describe '.user' do subject { described_class.user } - xit 'returns user information' do + it 'returns user information' do expect(subject).to include('Email' => instance_of(String)) end end From dca4bea7f5185a4260d36409c962a53fcd697e00 Mon Sep 17 00:00:00 2001 From: Laurynas Butkus Date: Sun, 4 Aug 2024 21:39:18 +0300 Subject: [PATCH 45/59] Switch to ruby 3.3 --- .github/workflows/main.yml | 1 + .ruby-version | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index f4aa040..643bb2c 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -11,6 +11,7 @@ jobs: - 2.7 - 3.1 - 3.2 + - 3.3 - jruby-9.1 - jruby-head name: Ruby ${{ matrix.ruby-version }} sample diff --git a/.ruby-version b/.ruby-version index ec1cf33..a0891f5 100644 --- a/.ruby-version +++ b/.ruby-version @@ -1 +1 @@ -2.6.3 +3.3.4 From 232d30dd9e8e1f9fa7aef5f9c5505504c2a5b074 Mon Sep 17 00:00:00 2001 From: Laurynas Butkus Date: Sun, 4 Aug 2024 21:45:54 +0300 Subject: [PATCH 46/59] Disable token injection in actions --- .github/workflows/main.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 643bb2c..b4c9f80 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -24,5 +24,5 @@ jobs: - run: bundle install - env: CONVERT_API_SECRET: ${{ secrets.CONVERTAPI_SECRET }} - CONVERT_API_TOKEN: ${{ secrets.CONVERTAPI_TOKEN }} + # CONVERT_API_TOKEN: ${{ secrets.CONVERTAPI_TOKEN }} run: bundle exec rake spec From 0bcb1d4c475dd5077c10d9729496e401f599d26e Mon Sep 17 00:00:00 2001 From: Laurynas Butkus Date: Sun, 4 Aug 2024 21:49:42 +0300 Subject: [PATCH 47/59] Prioritize secret over token --- .github/workflows/main.yml | 2 +- lib/convert_api/client.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index b4c9f80..643bb2c 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -24,5 +24,5 @@ jobs: - run: bundle install - env: CONVERT_API_SECRET: ${{ secrets.CONVERTAPI_SECRET }} - # CONVERT_API_TOKEN: ${{ secrets.CONVERTAPI_TOKEN }} + CONVERT_API_TOKEN: ${{ secrets.CONVERTAPI_TOKEN }} run: bundle exec rake spec diff --git a/lib/convert_api/client.rb b/lib/convert_api/client.rb index e028ef4..3027d1a 100644 --- a/lib/convert_api/client.rb +++ b/lib/convert_api/client.rb @@ -124,8 +124,8 @@ def build_form_data(params) end def authentication - return { Token: config.token } unless config.token.nil? return { Secret: config.api_secret } unless config.api_secret.nil? + return { Token: config.token } unless config.token.nil? nil end From a4665b6b53a9f2370f10a4d38c77594b96143e9c Mon Sep 17 00:00:00 2001 From: Laurynas Butkus Date: Sun, 4 Aug 2024 21:53:29 +0300 Subject: [PATCH 48/59] Fix spec --- spec/convert_api_spec.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/spec/convert_api_spec.rb b/spec/convert_api_spec.rb index 2b2baa0..9eb58d4 100644 --- a/spec/convert_api_spec.rb +++ b/spec/convert_api_spec.rb @@ -102,13 +102,14 @@ it 'with invalid secret' do described_class.config.api_secret = 'invalid' - described_class.config.token = 'invalid' + described_class.config.token = nil expect { subject }.to raise_error(ConvertApi::ClientError) end it 'with invalid token' do described_class.config.token = 'invalid' + described_class.config.api_secret = nil expect { subject }.to raise_error(ConvertApi::ClientError) end From 5790da8a794174be827f5eb1387d32753c59b64d Mon Sep 17 00:00:00 2001 From: Laurynas Butkus Date: Sun, 4 Aug 2024 22:27:27 +0300 Subject: [PATCH 49/59] Add gem publish action --- .github/workflows/publish.yml | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 .github/workflows/publish.yml diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml new file mode 100644 index 0000000..7f27d9d --- /dev/null +++ b/.github/workflows/publish.yml @@ -0,0 +1,18 @@ +name: Publish gem to rubygems +on: + release: + types: [published] +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: ruby/setup-ruby@v1 + + # setup .gem/credentials + - run: mkdir -p ~/.gem + - run: echo -e "---\n:rubygems_api_key: ${{ secrets.RUBYGEMS_API_KEY }}" > ~/.gem/credentials + - run: chmod 0600 ~/.gem/credentials + + - run: gem build convert_api.gemspec --output=release.gem + - run: gem push release.gem From 12574a317f126e6727aacd63bf36db1087df2b3b Mon Sep 17 00:00:00 2001 From: Laurynas Butkus Date: Sun, 4 Aug 2024 22:32:09 +0300 Subject: [PATCH 50/59] Update README to use ConversionsConsumed --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f6d56c4..a0e854f 100644 --- a/README.md +++ b/README.md @@ -113,7 +113,7 @@ You can always check your remaining seconds programmatically by fetching [user i ```ruby user_info = ConvertApi.user -puts user_info['SecondsLeft'] +puts user_info['ConversionsConsumed'] ``` ### Alternative domain From f9b64a9a454813929991c46dbecfc41c4aeb59b9 Mon Sep 17 00:00:00 2001 From: Laurynas Butkus Date: Sun, 4 Aug 2024 22:35:52 +0300 Subject: [PATCH 51/59] Bump version --- lib/convert_api/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/convert_api/version.rb b/lib/convert_api/version.rb index 84d7d2b..b1c1a8c 100644 --- a/lib/convert_api/version.rb +++ b/lib/convert_api/version.rb @@ -1,3 +1,3 @@ module ConvertApi - VERSION = '1.4.0' + VERSION = '2.0.0' end From da11a8f29c4088296b2cd588e4d34ca22d37607f Mon Sep 17 00:00:00 2001 From: Laurynas Butkus Date: Sun, 4 Aug 2024 22:53:18 +0300 Subject: [PATCH 52/59] Adjust publish action --- .github/workflows/publish.yml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 7f27d9d..3b92e35 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -11,7 +11,10 @@ jobs: # setup .gem/credentials - run: mkdir -p ~/.gem - - run: echo -e "---\n:rubygems_api_key: ${{ secrets.RUBYGEMS_API_KEY }}" > ~/.gem/credentials + - run: >- + echo -e "---\n:rubygems_api_key: ${RUBYGEMS_API_KEY}" > ~/.gem/credentials + env: + RUBYGEMS_API_KEY: ${{ secrets.RUBYGEMS_API_KEY }} - run: chmod 0600 ~/.gem/credentials - run: gem build convert_api.gemspec --output=release.gem From b073aa7d4458e9b155f2a4a097d1356933479037 Mon Sep 17 00:00:00 2001 From: Laurynas Butkus Date: Sun, 4 Aug 2024 22:57:30 +0300 Subject: [PATCH 53/59] Adjust publish action --- .github/workflows/publish.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 3b92e35..6e9f041 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -11,10 +11,10 @@ jobs: # setup .gem/credentials - run: mkdir -p ~/.gem - - run: >- - echo -e "---\n:rubygems_api_key: ${RUBYGEMS_API_KEY}" > ~/.gem/credentials - env: + - env: RUBYGEMS_API_KEY: ${{ secrets.RUBYGEMS_API_KEY }} + run: >- + echo -e "---\n:rubygems_api_key: ${RUBYGEMS_API_KEY}" > ~/.gem/credentials - run: chmod 0600 ~/.gem/credentials - run: gem build convert_api.gemspec --output=release.gem From 849e09e7ec801a622773be7199cbfe1e7be3648d Mon Sep 17 00:00:00 2001 From: laurynas-convertapi <38224044+laurynas-convertapi@users.noreply.github.com> Date: Sat, 14 Sep 2024 08:45:45 +0300 Subject: [PATCH 54/59] Remove alternative converter support (#32) Co-authored-by: Laurynas Butkus --- examples/alternative_converter.rb | 20 -------------------- lib/convert_api/task.rb | 12 +----------- spec/convert_api/task_spec.rb | 12 ------------ 3 files changed, 1 insertion(+), 43 deletions(-) delete mode 100644 examples/alternative_converter.rb diff --git a/examples/alternative_converter.rb b/examples/alternative_converter.rb deleted file mode 100644 index 34e441b..0000000 --- a/examples/alternative_converter.rb +++ /dev/null @@ -1,20 +0,0 @@ -require 'convert_api' -require 'tmpdir' - -ConvertApi.configure do |config| - config.api_secret = ENV['CONVERT_API_SECRET'] # your api secret - # or - config.token = ENV['CONVERT_API_TOKEN'] # your token -end - -# Example of saving Word docx to PDF using OpenOffice converter -# https://www.convertapi.com/doc-to-pdf/openoffice - -# Use upload IO wrapper to upload file only once to the API -upload_io = ConvertApi::UploadIO.new(File.open('files/test.docx')) - -saved_files = ConvertApi - .convert('pdf', {File: upload_io, converter: 'openofficetopdf'}) - .save_files(Dir.tmpdir) - -puts "The PDF saved to: #{saved_files}" diff --git a/lib/convert_api/task.rb b/lib/convert_api/task.rb index 39194d4..85fcc1f 100644 --- a/lib/convert_api/task.rb +++ b/lib/convert_api/task.rb @@ -15,11 +15,9 @@ def run from_format = @from_format || detect_format(params) read_timeout = @conversion_timeout + config.conversion_timeout_delta if @conversion_timeout - converter = detect_converter(params) - converter_path = converter ? "/converter/#{converter}" : '' response = ConvertApi.client.post( - "convert/#{from_format}/to/#{@to_format}#{converter_path}", + "convert/#{from_format}/to/#{@to_format}", params, read_timeout: read_timeout, ) @@ -70,14 +68,6 @@ def detect_format(params) FormatDetector.new(resource, @to_format).run end - def detect_converter(params) - params.each do |key, value| - return value if key.to_s.downcase == 'converter' - end - - nil - end - def config ConvertApi.config end diff --git a/spec/convert_api/task_spec.rb b/spec/convert_api/task_spec.rb index 8af9197..d8b87c8 100644 --- a/spec/convert_api/task_spec.rb +++ b/spec/convert_api/task_spec.rb @@ -16,18 +16,6 @@ expect(subject).to be_instance_of(ConvertApi::Result) end - context 'with converter' do - let(:params) { { File: file, Converter: 'openoffice' } } - - it 'adds converter to the path' do - expect(ConvertApi.client).to( - receive(:post).with('convert/txt/to/pdf/converter/openoffice', instance_of(Hash), instance_of(Hash)).and_return(result) - ) - - expect(subject).to be_instance_of(ConvertApi::Result) - end - end - context 'when file is instance of ResultFile' do let(:file) { ConvertApi::ResultFile.new('Url' => 'testurl') } let(:expected_params) { hash_including(File: 'testurl') } From 68fe7e609230d675a15904ea5040f51e3c2d2f39 Mon Sep 17 00:00:00 2001 From: Laurynas Butkus Date: Sat, 14 Sep 2024 11:01:30 +0300 Subject: [PATCH 55/59] Update usage example --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index a0e854f..e5e2136 100644 --- a/README.md +++ b/README.md @@ -108,7 +108,7 @@ puts result.files[0].size ### User information -You can always check your remaining seconds programmatically by fetching [user information](https://www.convertapi.com/doc/user). +You can always check your usage by fetching [user information](https://www.convertapi.com/doc/user). ```ruby user_info = ConvertApi.user From 30289d575f39252a02fe8548308b1d511de9380c Mon Sep 17 00:00:00 2001 From: laurynas-convertapi <38224044+laurynas-convertapi@users.noreply.github.com> Date: Sat, 14 Sep 2024 11:24:07 +0300 Subject: [PATCH 56/59] Switch to authorization header (#33) Co-authored-by: Laurynas Butkus --- .github/workflows/main.yml | 1 - README.md | 17 ++-------------- examples/conversions_chaining.rb | 4 +--- examples/convert_stream.rb | 4 +--- examples/convert_url_to_pdf.rb | 5 ++--- examples/convert_word_to_pdf_and_png.rb | 4 +--- examples/create_pdf_thumbnail.rb | 4 +--- examples/retrieve_user_information.rb | 4 +--- examples/split_and_merge_pdf.rb | 4 +--- lib/convert_api/client.rb | 24 +++++++++++------------ lib/convert_api/configuration.rb | 3 +-- spec/convert_api_spec.rb | 26 +++++++------------------ spec/spec_helper.rb | 4 +--- 13 files changed, 31 insertions(+), 73 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 643bb2c..18cb448 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -24,5 +24,4 @@ jobs: - run: bundle install - env: CONVERT_API_SECRET: ${{ secrets.CONVERTAPI_SECRET }} - CONVERT_API_TOKEN: ${{ secrets.CONVERTAPI_TOKEN }} run: bundle exec rake spec diff --git a/README.md b/README.md index e5e2136..39df8db 100644 --- a/README.md +++ b/README.md @@ -23,20 +23,11 @@ gem 'convert_api' ### Configuration -You can get your secret at https://www.convertapi.com/a/auth +You can get your credentials at https://www.convertapi.com/a/auth ```ruby ConvertApi.configure do |config| - config.api_secret = 'your-api-secret' -end -``` - -Or - -You can get your token at https://www.convertapi.com/a/access-tokens -```ruby -ConvertApi.configure do |config| - config.token = 'your-token' + config.api_credentials = 'your-api-secret-or-token' end ``` @@ -136,10 +127,6 @@ Find more advanced examples in the [examples/](https://github.com/ConvertAPI/con Run `CONVERT_API_SECRET=your_secret rake spec` to run the tests. -Or - -Run `CONVERT_API_TOKEN=your_token rake spec` to run the tests. - 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). ## Contributing diff --git a/examples/conversions_chaining.rb b/examples/conversions_chaining.rb index 1ab1a97..979c2d9 100644 --- a/examples/conversions_chaining.rb +++ b/examples/conversions_chaining.rb @@ -2,9 +2,7 @@ require 'tmpdir' ConvertApi.configure do |config| - config.api_secret = ENV['CONVERT_API_SECRET'] # your api secret - # or - config.token = ENV['CONVERT_API_TOKEN'] # your token + config.api_credentials = ENV['CONVERT_API_SECRET'] # your api secret or token end # Short example of conversions chaining, the PDF pages extracted and saved as separated JPGs and then ZIP'ed diff --git a/examples/convert_stream.rb b/examples/convert_stream.rb index ad78a61..38d7d5a 100644 --- a/examples/convert_stream.rb +++ b/examples/convert_stream.rb @@ -2,9 +2,7 @@ require 'tmpdir' ConvertApi.configure do |config| - config.api_secret = ENV['CONVERT_API_SECRET'] # your api secret - # or - config.token = ENV['CONVERT_API_TOKEN'] # your token + config.api_credentials = ENV['CONVERT_API_SECRET'] # your api secret or token end # Example of converting text to PDF diff --git a/examples/convert_url_to_pdf.rb b/examples/convert_url_to_pdf.rb index 5214208..d19f26f 100644 --- a/examples/convert_url_to_pdf.rb +++ b/examples/convert_url_to_pdf.rb @@ -2,10 +2,9 @@ require 'tmpdir' ConvertApi.configure do |config| - config.api_secret = ENV['CONVERT_API_SECRET'] # your api secret - # or - config.token = ENV['CONVERT_API_TOKEN'] # your token + config.api_credentials = ENV['CONVERT_API_SECRET'] # your api secret or token end + # Example of converting Web Page URL to PDF file # https://www.convertapi.com/web-to-pdf diff --git a/examples/convert_word_to_pdf_and_png.rb b/examples/convert_word_to_pdf_and_png.rb index b24d283..2c50bac 100644 --- a/examples/convert_word_to_pdf_and_png.rb +++ b/examples/convert_word_to_pdf_and_png.rb @@ -2,9 +2,7 @@ require 'tmpdir' ConvertApi.configure do |config| - config.api_secret = ENV['CONVERT_API_SECRET'] # your api secret - # or - config.token = ENV['CONVERT_API_TOKEN'] # your token + config.api_credentials = ENV['CONVERT_API_SECRET'] # your api secret or token end # Example of saving Word docx to PDF and to PNG diff --git a/examples/create_pdf_thumbnail.rb b/examples/create_pdf_thumbnail.rb index 3321032..69d2a9f 100644 --- a/examples/create_pdf_thumbnail.rb +++ b/examples/create_pdf_thumbnail.rb @@ -2,9 +2,7 @@ require 'tmpdir' ConvertApi.configure do |config| - config.api_secret = ENV['CONVERT_API_SECRET'] # your api secret - # or - config.token = ENV['CONVERT_API_TOKEN'] # your token + config.api_credentials = ENV['CONVERT_API_SECRET'] # your api secret or token end # Example of extracting first page from PDF and then chaining conversion PDF page to JPG. diff --git a/examples/retrieve_user_information.rb b/examples/retrieve_user_information.rb index 0795184..310283e 100644 --- a/examples/retrieve_user_information.rb +++ b/examples/retrieve_user_information.rb @@ -1,9 +1,7 @@ require 'convert_api' ConvertApi.configure do |config| - config.api_secret = ENV['CONVERT_API_SECRET'] # your api secret - # or - config.token = ENV['CONVERT_API_TOKEN'] # your token + config.api_credentials = ENV['CONVERT_API_SECRET'] # your api secret or token end # Retrieve user information diff --git a/examples/split_and_merge_pdf.rb b/examples/split_and_merge_pdf.rb index 2d4e260..80a8ea7 100644 --- a/examples/split_and_merge_pdf.rb +++ b/examples/split_and_merge_pdf.rb @@ -2,9 +2,7 @@ require 'tmpdir' ConvertApi.configure do |config| - config.api_secret = ENV['CONVERT_API_SECRET'] # your api secret - # or - config.token = ENV['CONVERT_API_TOKEN'] # your token + config.api_credentials = ENV['CONVERT_API_SECRET'] # your api secret or token end # Example of extracting first and last pages from PDF and then merging them back to new PDF. diff --git a/lib/convert_api/client.rb b/lib/convert_api/client.rb index 3027d1a..848aa05 100644 --- a/lib/convert_api/client.rb +++ b/lib/convert_api/client.rb @@ -30,7 +30,7 @@ class Client def get(path, params = {}, options = {}) handle_response do - request = Net::HTTP::Get.new(request_uri(path, params), DEFAULT_HEADERS) + request = Net::HTTP::Get.new(request_uri(path, params), headers_with_auth) http(options).request(request) end @@ -38,7 +38,7 @@ def get(path, params = {}, options = {}) def post(path, params, options = {}) handle_response do - request = Net::HTTP::Post.new(request_uri(path), DEFAULT_HEADERS) + request = Net::HTTP::Post.new(request_uri(path), headers_with_auth) request.form_data = build_form_data(params) http(options).request(request) @@ -101,12 +101,7 @@ def http(options = {}) end def request_uri(path, params = {}) - raise(AuthenticationError, 'API secret or Token not configured') if authentication.nil? - - params_with_authentication = params.merge(authentication) - query = URI.encode_www_form(params_with_authentication) - - base_uri.path + path + '?' + query + base_uri.path + path + '?' + URI.encode_www_form(params) end def build_form_data(params) @@ -123,11 +118,16 @@ def build_form_data(params) data end - def authentication - return { Secret: config.api_secret } unless config.api_secret.nil? - return { Token: config.token } unless config.token.nil? + def headers_with_auth + DEFAULT_HEADERS.merge(auth_headers) + end + + def auth_headers + { 'Authorization' => "Bearer #{api_credentials}" } + end - nil + def api_credentials + config.api_credentials || raise(AuthenticationError, 'API credentials not configured') end def base_uri diff --git a/lib/convert_api/configuration.rb b/lib/convert_api/configuration.rb index 8f0783c..c29cfbe 100644 --- a/lib/convert_api/configuration.rb +++ b/lib/convert_api/configuration.rb @@ -1,7 +1,6 @@ module ConvertApi class Configuration - attr_accessor :api_secret - attr_accessor :token + attr_accessor :api_credentials attr_accessor :base_uri attr_accessor :connect_timeout attr_accessor :read_timeout diff --git a/spec/convert_api_spec.rb b/spec/convert_api_spec.rb index 9eb58d4..85fc638 100644 --- a/spec/convert_api_spec.rb +++ b/spec/convert_api_spec.rb @@ -9,19 +9,16 @@ end describe '.configure' do - let(:api_secret) { 'test_secret' } - let(:token) { 'test_token' } + let(:api_credentials) { 'test_secret' } let(:conversion_timeout) { 20 } it 'configures' do described_class.configure do |config| - config.api_secret = api_secret - config.token = token + config.api_credentials = api_credentials config.conversion_timeout = conversion_timeout end - expect(described_class.config.api_secret).to eq(api_secret) - expect(described_class.config.token).to eq(token) + expect(described_class.config.api_credentials).to eq(api_credentials) expect(described_class.config.conversion_timeout).to eq(conversion_timeout) end end @@ -93,23 +90,14 @@ end context 'when has error' do - it 'raises error without secret or token' do - described_class.config.api_secret = nil - described_class.config.token = nil + it 'raises error without credentials' do + described_class.config.api_credentials = nil expect { subject }.to raise_error(ConvertApi::AuthenticationError, /not configured/) end - it 'with invalid secret' do - described_class.config.api_secret = 'invalid' - described_class.config.token = nil - - expect { subject }.to raise_error(ConvertApi::ClientError) - end - - it 'with invalid token' do - described_class.config.token = 'invalid' - described_class.config.api_secret = nil + it 'with invalid credentials' do + described_class.config.api_credentials = 'invalid' expect { subject }.to raise_error(ConvertApi::ClientError) end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index fcd8a63..20b8bf4 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -9,8 +9,6 @@ end config.before(:each) do - ConvertApi.config.api_secret = ENV['CONVERT_API_SECRET'] - # or - ConvertApi.config.token = ENV['CONVERT_API_TOKEN'] + ConvertApi.config.api_credentials = ENV['CONVERT_API_SECRET'] end end From 02a7c6cd6d59c5ab7c4b3825a83e6b5ad7de89c2 Mon Sep 17 00:00:00 2001 From: Laurynas Butkus Date: Sat, 14 Sep 2024 11:36:44 +0300 Subject: [PATCH 57/59] Bump version --- lib/convert_api/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/convert_api/version.rb b/lib/convert_api/version.rb index b1c1a8c..bf3847f 100644 --- a/lib/convert_api/version.rb +++ b/lib/convert_api/version.rb @@ -1,3 +1,3 @@ module ConvertApi - VERSION = '2.0.0' + VERSION = '3.0.0' end From 5050730279ee7174ec96d305c5d9473b75c22830 Mon Sep 17 00:00:00 2001 From: Jonas Jasas Date: Mon, 2 Jun 2025 17:03:48 +0300 Subject: [PATCH 58/59] Remove all references to Secret and ApiKey #34 --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 39df8db..ef0655e 100644 --- a/README.md +++ b/README.md @@ -23,11 +23,11 @@ gem 'convert_api' ### Configuration -You can get your credentials at https://www.convertapi.com/a/auth +You can get your credentials at [https://www.convertapi.com/a/authentication](https://www.convertapi.com/a/authentication) ```ruby ConvertApi.configure do |config| - config.api_credentials = 'your-api-secret-or-token' + config.api_credentials = 'api-token' end ``` @@ -125,7 +125,7 @@ Find more advanced examples in the [examples/](https://github.com/ConvertAPI/con ## Development -Run `CONVERT_API_SECRET=your_secret rake spec` to run the tests. +Run `CONVERT_API_SECRET=your_api_token rake spec` to run the tests. 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). From 551d77e883f4649da8f608513c9ac8f26af062fa Mon Sep 17 00:00:00 2001 From: Jonas Jasas Date: Mon, 2 Jun 2025 17:10:31 +0300 Subject: [PATCH 59/59] Remove all references to `Secret` and `ApiKey` (Issue #34) --- examples/conversions_chaining.rb | 4 ++-- examples/convert_stream.rb | 4 ++-- examples/convert_url_to_pdf.rb | 4 ++-- examples/convert_word_to_pdf_and_png.rb | 4 ++-- examples/create_pdf_thumbnail.rb | 4 ++-- examples/retrieve_user_information.rb | 4 ++-- examples/split_and_merge_pdf.rb | 4 ++-- 7 files changed, 14 insertions(+), 14 deletions(-) diff --git a/examples/conversions_chaining.rb b/examples/conversions_chaining.rb index 979c2d9..857de79 100644 --- a/examples/conversions_chaining.rb +++ b/examples/conversions_chaining.rb @@ -2,7 +2,7 @@ require 'tmpdir' ConvertApi.configure do |config| - config.api_credentials = ENV['CONVERT_API_SECRET'] # your api secret or token + config.api_credentials = ENV['API_TOKEN'] # your api token end # Short example of conversions chaining, the PDF pages extracted and saved as separated JPGs and then ZIP'ed @@ -20,4 +20,4 @@ saved_files = zip_result.save_files(Dir.tmpdir) -puts "File saved to #{saved_files}" +puts "File saved to #{saved_files}" \ No newline at end of file diff --git a/examples/convert_stream.rb b/examples/convert_stream.rb index 38d7d5a..61a7555 100644 --- a/examples/convert_stream.rb +++ b/examples/convert_stream.rb @@ -2,7 +2,7 @@ require 'tmpdir' ConvertApi.configure do |config| - config.api_credentials = ENV['CONVERT_API_SECRET'] # your api secret or token + config.api_credentials = ENV['API_TOKEN'] # your api token end # Example of converting text to PDF @@ -21,4 +21,4 @@ .convert('pdf', {File: upload_io}) .save_files(Dir.tmpdir) -puts "The PDF saved to: #{saved_files}" +puts "The PDF saved to: #{saved_files}" \ No newline at end of file diff --git a/examples/convert_url_to_pdf.rb b/examples/convert_url_to_pdf.rb index d19f26f..880886b 100644 --- a/examples/convert_url_to_pdf.rb +++ b/examples/convert_url_to_pdf.rb @@ -2,7 +2,7 @@ require 'tmpdir' ConvertApi.configure do |config| - config.api_credentials = ENV['CONVERT_API_SECRET'] # your api secret or token + config.api_credentials = ENV['API_TOKEN'] # your api token end # Example of converting Web Page URL to PDF file @@ -20,4 +20,4 @@ saved_files = result.save_files(Dir.tmpdir) -puts "The web page PDF saved to #{saved_files}" +puts "The web page PDF saved to #{saved_files}" \ No newline at end of file diff --git a/examples/convert_word_to_pdf_and_png.rb b/examples/convert_word_to_pdf_and_png.rb index 2c50bac..ad3b882 100644 --- a/examples/convert_word_to_pdf_and_png.rb +++ b/examples/convert_word_to_pdf_and_png.rb @@ -2,7 +2,7 @@ require 'tmpdir' ConvertApi.configure do |config| - config.api_credentials = ENV['CONVERT_API_SECRET'] # your api secret or token + config.api_credentials = ENV['API_TOKEN'] # your api token end # Example of saving Word docx to PDF and to PNG @@ -23,4 +23,4 @@ .convert('png', File: upload_io) .save_files(Dir.tmpdir) -puts "The PNG saved to: #{saved_files}" +puts "The PNG saved to: #{saved_files}" \ No newline at end of file diff --git a/examples/create_pdf_thumbnail.rb b/examples/create_pdf_thumbnail.rb index 69d2a9f..7a08683 100644 --- a/examples/create_pdf_thumbnail.rb +++ b/examples/create_pdf_thumbnail.rb @@ -2,7 +2,7 @@ require 'tmpdir' ConvertApi.configure do |config| - config.api_credentials = ENV['CONVERT_API_SECRET'] # your api secret or token + config.api_credentials = ENV['API_TOKEN'] # your api token end # Example of extracting first page from PDF and then chaining conversion PDF page to JPG. @@ -26,4 +26,4 @@ saved_files = jpg_result.save_files(Dir.tmpdir) -puts "The thumbnail saved to #{saved_files}" +puts "The thumbnail saved to #{saved_files}" \ No newline at end of file diff --git a/examples/retrieve_user_information.rb b/examples/retrieve_user_information.rb index 310283e..52573b8 100644 --- a/examples/retrieve_user_information.rb +++ b/examples/retrieve_user_information.rb @@ -1,10 +1,10 @@ require 'convert_api' ConvertApi.configure do |config| - config.api_credentials = ENV['CONVERT_API_SECRET'] # your api secret or token + config.api_credentials = ENV['API_TOKEN'] # your api token end # Retrieve user information # https://www.convertapi.com/doc/user -puts ConvertApi.user +puts ConvertApi.user \ No newline at end of file diff --git a/examples/split_and_merge_pdf.rb b/examples/split_and_merge_pdf.rb index 80a8ea7..fd0aba4 100644 --- a/examples/split_and_merge_pdf.rb +++ b/examples/split_and_merge_pdf.rb @@ -2,7 +2,7 @@ require 'tmpdir' ConvertApi.configure do |config| - config.api_credentials = ENV['CONVERT_API_SECRET'] # your api secret or token + config.api_credentials = ENV['API_TOKEN'] # your api token end # Example of extracting first and last pages from PDF and then merging them back to new PDF. @@ -17,4 +17,4 @@ saved_files = merge_result.save_files(Dir.tmpdir) -puts "The PDF saved to #{saved_files}" +puts "The PDF saved to #{saved_files}" \ No newline at end of file