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 a0e854f..6069513 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