Skip to content

Commit 30289d5

Browse files
laurynas-convertapiLaurynas Butkus
andauthored
Switch to authorization header (#33)
Co-authored-by: Laurynas Butkus <[email protected]>
1 parent 68fe7e6 commit 30289d5

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

0 commit comments

Comments
 (0)