Skip to content

Commit 566d89a

Browse files
authored
Merge pull request #15 from emailable/add-access-token-auth
Add support for access token authentication
2 parents d37de93 + 5015b6a commit 566d89a

13 files changed

Lines changed: 71 additions & 41 deletions

.github/workflows/ci.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,16 @@ name: CI
22

33
on:
44
push:
5-
branches: [ master ]
5+
branches: [ 'master' ]
66
pull_request:
7-
branches: [ master ]
87

98
jobs:
109
tests:
1110
name: Tests
1211
runs-on: ubuntu-latest
1312
strategy:
1413
matrix:
15-
ruby-version: ['2.7', '3.0', '3.1', '3.2', '3.3']
14+
ruby-version: ['2.7', '3.0', '3.1', '3.2', '3.3', '3.4']
1615

1716
steps:
1817
- uses: actions/checkout@v4

.rubocop.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
1-
inherit_from: .rubocop_todo.yml
2-
31
inherit_gem:
42
rubocop-cache-ventures: rubocop.yml

.rubocop_todo.yml

Lines changed: 0 additions & 7 deletions
This file was deleted.

.ruby-version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3.3.0
1+
3.4.4

Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
source "https://rubygems.org"
1+
source 'https://rubygems.org'
22

33
# Specify your gem's dependencies in emailable-ruby.gemspec
44
gemspec

README.md

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,26 @@ Or install it yourself as:
3030

3131
## Usage
3232

33-
The library needs to be configured with your account's API key which is
34-
available in your [Emailable Dashboard](https://app.emailable.com/api). Set
35-
`Emailable.api_key` to its value:
33+
### Authentication
3634

37-
### Setup
35+
The Emailable API requires either an API key or an access token for
36+
authentication. API keys can be created and managed in the
37+
[Emailable Dashboard](https://app.emailable.com/api).
38+
39+
An API key can be set globally for the Emailable client:
40+
41+
```ruby
42+
Emailable.api_key = 'your_api_key'
43+
```
44+
45+
Or, you can specify an `api_key` or an `access_token` with each request:
3846

3947
```ruby
40-
require 'emailable'
48+
# set api_key at request time
49+
Emailable.verify(api_key: 'your_api_key')
4150

42-
# set api key
43-
Emailable.api_key = 'live_...'
51+
# set access_token at request time
52+
Emailable.verify(access_token: 'your_access_token')
4453
```
4554

4655
### Verification

emailable.gemspec

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ Gem::Specification.new do |s|
1515
s.email = 'support@emailable.com'
1616
s.license = 'MIT'
1717
s.metadata = {
18-
"bug_tracker_uri" => "https://github.com/emailable/emailable-ruby/issues",
19-
"documentation_uri" => "https://docs.emailable.com/?ruby",
20-
"source_code_uri" => "https://github.com/emailable/emailable-ruby"
18+
'bug_tracker_uri' => 'https://github.com/emailable/emailable-ruby/issues',
19+
'documentation_uri' => 'https://emailable.com/docs/api/?ruby',
20+
'source_code_uri' => 'https://github.com/emailable/emailable-ruby'
2121
}
2222

2323
s.files = `git ls-files`.split("\n")
@@ -29,8 +29,7 @@ Gem::Specification.new do |s|
2929

3030
s.add_development_dependency 'bundler'
3131
s.add_development_dependency 'rake', '~> 13.0'
32-
s.add_development_dependency 'pry'
33-
s.add_development_dependency 'awesome_print'
32+
s.add_development_dependency 'amazing_print'
3433
s.add_development_dependency 'minitest', '~> 5.0'
3534
s.add_development_dependency 'minitest-reporters'
3635
s.add_development_dependency 'activemodel'

lib/emailable.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def verify(email, parameters = {})
3232
parameters[:email] = email
3333

3434
client = Emailable::Client.new
35-
response = client.request(:get, 'verify', parameters)
35+
response = client.request(:post, 'verify', parameters)
3636

3737
if response.status == 249
3838
raise Emailable::TimeoutError.new(response.body)
@@ -41,9 +41,9 @@ def verify(email, parameters = {})
4141
end
4242
end
4343

44-
def account
44+
def account(parameters = {})
4545
client = Emailable::Client.new
46-
response = client.request(:get, 'account')
46+
response = client.request(:get, 'account', parameters)
4747
Account.new(response.body)
4848
end
4949

lib/emailable/client.rb

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,24 @@ def initialize
1717
end
1818

1919
def request(method, endpoint, params = {})
20-
begin
21-
tries ||= 3
20+
api_key = params.delete(:api_key)
21+
access_token = params.delete(:access_token)
2222

23-
uri = URI("#{@base_url}/#{endpoint}")
24-
params[:api_key] = Emailable.api_key
23+
uri = URI("#{@base_url}/#{endpoint}")
24+
headers = {
25+
'Authorization': "Bearer #{Emailable.api_key || api_key || access_token}",
26+
'Content-Type': 'application/json'
27+
}
2528

29+
begin
30+
tries ||= 3
2631
http_response =
2732
if method == :get
28-
uri.query = URI.encode_www_form(params)
29-
@connection.get(uri)
33+
uri.query = URI.encode_www_form(params) unless params.empty?
34+
request = Net::HTTP::Get.new(uri, headers)
35+
@connection.request(request)
3036
elsif method == :post
31-
request = Net::HTTP::Post.new(uri, 'Content-Type': 'application/json')
37+
request = Net::HTTP::Post.new(uri, headers)
3238
request.body = params.to_json
3339
@connection.request(request)
3440
end
@@ -65,7 +71,7 @@ def create_connection(uri)
6571
if connection.respond_to?(:write_timeout=)
6672
connection.write_timeout = Emailable.write_timeout
6773
end
68-
connection.use_ssl = uri.scheme == "https"
74+
connection.use_ssl = uri.scheme == 'https'
6975

7076
connection
7177
end

lib/emailable/email_validator.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def validate_each(record, attribute, value)
1818
states = options.fetch(:states, %i(deliverable risky unknown))
1919
allowed_states = %i[deliverable undeliverable risky unknown]
2020
unless (states - allowed_states).empty?
21-
raise ArgumentError, ":states must be an array of symbols containing "\
21+
raise ArgumentError, ':states must be an array of symbols containing '\
2222
"any or all of :#{allowed_states.join(', :')}"
2323
end
2424

@@ -29,7 +29,7 @@ def validate_each(record, attribute, value)
2929

3030
timeout = options.fetch(:timeout, 3)
3131
unless timeout.is_a?(Integer) && timeout > 1
32-
raise ArgumentError, ":timeout must be an Integer greater than 1"
32+
raise ArgumentError, ':timeout must be an Integer greater than 1'
3333
end
3434

3535
return if record.errors[attribute].present?

0 commit comments

Comments
 (0)