Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Token authentication #30

Merged
merged 17 commits into from
Aug 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,26 @@ 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|
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'
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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions examples/alternative_converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions examples/conversions_chaining.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions examples/convert_stream.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion examples/convert_url_to_pdf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions examples/convert_word_to_pdf_and_png.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions examples/create_pdf_thumbnail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions examples/retrieve_user_information.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions examples/split_and_merge_pdf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
13 changes: 10 additions & 3 deletions lib/convert_api/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/convert_api/configuration.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/convert_api/errors.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
29 changes: 19 additions & 10 deletions spec/convert_api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading