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/.github/workflows/publish.yml b/.github/workflows/publish.yml index 7f27d9d..6e9f041 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 + - 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 diff --git a/README.md b/README.md index a0e854f..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 ``` @@ -108,7 +99,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 @@ -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/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/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/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/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 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') } 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