Skip to content

Commit 345a2fe

Browse files
author
Laurynas Butkus
committed
Switch to authorization header
1 parent b073aa7 commit 345a2fe

13 files changed

+31
-73
lines changed

.github/workflows/main.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,4 @@ jobs:
2424
- run: bundle install
2525
- env:
2626
CONVERT_API_SECRET: ${{ secrets.CONVERTAPI_SECRET }}
27-
CONVERT_API_TOKEN: ${{ secrets.CONVERTAPI_TOKEN }}
2827
run: bundle exec rake spec

README.md

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,11 @@ gem 'convert_api'
2323

2424
### Configuration
2525

26-
You can get your secret at https://www.convertapi.com/a/auth
26+
You can get your credentials at https://www.convertapi.com/a/auth
2727

2828
```ruby
2929
ConvertApi.configure do |config|
30-
config.api_secret = 'your-api-secret'
31-
end
32-
```
33-
34-
Or
35-
36-
You can get your token at https://www.convertapi.com/a/access-tokens
37-
```ruby
38-
ConvertApi.configure do |config|
39-
config.token = 'your-token'
30+
config.api_credentials = 'your-api-secret-or-token'
4031
end
4132
```
4233

@@ -136,10 +127,6 @@ Find more advanced examples in the [examples/](https://github.com/ConvertAPI/con
136127

137128
Run `CONVERT_API_SECRET=your_secret rake spec` to run the tests.
138129

139-
Or
140-
141-
Run `CONVERT_API_TOKEN=your_token rake spec` to run the tests.
142-
143130
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).
144131

145132
## Contributing

examples/conversions_chaining.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
require 'tmpdir'
33

44
ConvertApi.configure do |config|
5-
config.api_secret = ENV['CONVERT_API_SECRET'] # your api secret
6-
# or
7-
config.token = ENV['CONVERT_API_TOKEN'] # your token
5+
config.api_credentials = ENV['CONVERT_API_SECRET'] # your api secret or token
86
end
97

108
# Short example of conversions chaining, the PDF pages extracted and saved as separated JPGs and then ZIP'ed

examples/convert_stream.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
require 'tmpdir'
33

44
ConvertApi.configure do |config|
5-
config.api_secret = ENV['CONVERT_API_SECRET'] # your api secret
6-
# or
7-
config.token = ENV['CONVERT_API_TOKEN'] # your token
5+
config.api_credentials = ENV['CONVERT_API_SECRET'] # your api secret or token
86
end
97

108
# Example of converting text to PDF

examples/convert_url_to_pdf.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22
require 'tmpdir'
33

44
ConvertApi.configure do |config|
5-
config.api_secret = ENV['CONVERT_API_SECRET'] # your api secret
6-
# or
7-
config.token = ENV['CONVERT_API_TOKEN'] # your token
5+
config.api_credentials = ENV['CONVERT_API_SECRET'] # your api secret or token
86
end
7+
98
# Example of converting Web Page URL to PDF file
109
# https://www.convertapi.com/web-to-pdf
1110

examples/convert_word_to_pdf_and_png.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
require 'tmpdir'
33

44
ConvertApi.configure do |config|
5-
config.api_secret = ENV['CONVERT_API_SECRET'] # your api secret
6-
# or
7-
config.token = ENV['CONVERT_API_TOKEN'] # your token
5+
config.api_credentials = ENV['CONVERT_API_SECRET'] # your api secret or token
86
end
97

108
# Example of saving Word docx to PDF and to PNG

examples/create_pdf_thumbnail.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
require 'tmpdir'
33

44
ConvertApi.configure do |config|
5-
config.api_secret = ENV['CONVERT_API_SECRET'] # your api secret
6-
# or
7-
config.token = ENV['CONVERT_API_TOKEN'] # your token
5+
config.api_credentials = ENV['CONVERT_API_SECRET'] # your api secret or token
86
end
97

108
# Example of extracting first page from PDF and then chaining conversion PDF page to JPG.

examples/retrieve_user_information.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
require 'convert_api'
22

33
ConvertApi.configure do |config|
4-
config.api_secret = ENV['CONVERT_API_SECRET'] # your api secret
5-
# or
6-
config.token = ENV['CONVERT_API_TOKEN'] # your token
4+
config.api_credentials = ENV['CONVERT_API_SECRET'] # your api secret or token
75
end
86

97
# Retrieve user information

examples/split_and_merge_pdf.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
require 'tmpdir'
33

44
ConvertApi.configure do |config|
5-
config.api_secret = ENV['CONVERT_API_SECRET'] # your api secret
6-
# or
7-
config.token = ENV['CONVERT_API_TOKEN'] # your token
5+
config.api_credentials = ENV['CONVERT_API_SECRET'] # your api secret or token
86
end
97

108
# Example of extracting first and last pages from PDF and then merging them back to new PDF.

lib/convert_api/client.rb

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@ class Client
3030

3131
def get(path, params = {}, options = {})
3232
handle_response do
33-
request = Net::HTTP::Get.new(request_uri(path, params), DEFAULT_HEADERS)
33+
request = Net::HTTP::Get.new(request_uri(path, params), headers_with_auth)
3434

3535
http(options).request(request)
3636
end
3737
end
3838

3939
def post(path, params, options = {})
4040
handle_response do
41-
request = Net::HTTP::Post.new(request_uri(path), DEFAULT_HEADERS)
41+
request = Net::HTTP::Post.new(request_uri(path), headers_with_auth)
4242
request.form_data = build_form_data(params)
4343

4444
http(options).request(request)
@@ -101,12 +101,7 @@ def http(options = {})
101101
end
102102

103103
def request_uri(path, params = {})
104-
raise(AuthenticationError, 'API secret or Token not configured') if authentication.nil?
105-
106-
params_with_authentication = params.merge(authentication)
107-
query = URI.encode_www_form(params_with_authentication)
108-
109-
base_uri.path + path + '?' + query
104+
base_uri.path + path + '?' + URI.encode_www_form(params)
110105
end
111106

112107
def build_form_data(params)
@@ -123,11 +118,16 @@ def build_form_data(params)
123118
data
124119
end
125120

126-
def authentication
127-
return { Secret: config.api_secret } unless config.api_secret.nil?
128-
return { Token: config.token } unless config.token.nil?
121+
def headers_with_auth
122+
DEFAULT_HEADERS.merge(auth_headers)
123+
end
124+
125+
def auth_headers
126+
{ 'Authorization' => "Bearer #{api_credentials}" }
127+
end
129128

130-
nil
129+
def api_credentials
130+
config.api_credentials || raise(AuthenticationError, 'API credentials not configured')
131131
end
132132

133133
def base_uri

lib/convert_api/configuration.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
module ConvertApi
22
class Configuration
3-
attr_accessor :api_secret
4-
attr_accessor :token
3+
attr_accessor :api_credentials
54
attr_accessor :base_uri
65
attr_accessor :connect_timeout
76
attr_accessor :read_timeout

spec/convert_api_spec.rb

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,16 @@
99
end
1010

1111
describe '.configure' do
12-
let(:api_secret) { 'test_secret' }
13-
let(:token) { 'test_token' }
12+
let(:api_credentials) { 'test_secret' }
1413
let(:conversion_timeout) { 20 }
1514

1615
it 'configures' do
1716
described_class.configure do |config|
18-
config.api_secret = api_secret
19-
config.token = token
17+
config.api_credentials = api_credentials
2018
config.conversion_timeout = conversion_timeout
2119
end
2220

23-
expect(described_class.config.api_secret).to eq(api_secret)
24-
expect(described_class.config.token).to eq(token)
21+
expect(described_class.config.api_credentials).to eq(api_credentials)
2522
expect(described_class.config.conversion_timeout).to eq(conversion_timeout)
2623
end
2724
end
@@ -93,23 +90,14 @@
9390
end
9491

9592
context 'when has error' do
96-
it 'raises error without secret or token' do
97-
described_class.config.api_secret = nil
98-
described_class.config.token = nil
93+
it 'raises error without credentials' do
94+
described_class.config.api_credentials = nil
9995

10096
expect { subject }.to raise_error(ConvertApi::AuthenticationError, /not configured/)
10197
end
10298

103-
it 'with invalid secret' do
104-
described_class.config.api_secret = 'invalid'
105-
described_class.config.token = nil
106-
107-
expect { subject }.to raise_error(ConvertApi::ClientError)
108-
end
109-
110-
it 'with invalid token' do
111-
described_class.config.token = 'invalid'
112-
described_class.config.api_secret = nil
99+
it 'with invalid credentials' do
100+
described_class.config.api_credentials = 'invalid'
113101

114102
expect { subject }.to raise_error(ConvertApi::ClientError)
115103
end

spec/spec_helper.rb

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
end
1010

1111
config.before(:each) do
12-
ConvertApi.config.api_secret = ENV['CONVERT_API_SECRET']
13-
# or
14-
ConvertApi.config.token = ENV['CONVERT_API_TOKEN']
12+
ConvertApi.config.api_credentials = ENV['CONVERT_API_SECRET']
1513
end
1614
end

0 commit comments

Comments
 (0)