Skip to content

Commit ec25c4b

Browse files
committed
Rename callback to webhook
1 parent da2f567 commit ec25c4b

7 files changed

Lines changed: 50 additions & 43 deletions

File tree

README.md

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ $ gem install phaxio
2626

2727
## Usage
2828

29-
Set up your API Key, API Secret, and, optionally, Callback Token.
29+
Set up your API Key, API Secret, and, optionally, Webhook Token.
3030

3131
``` ruby
3232
require 'phaxio'
3333

3434
Phaxio.api_key = '11111'
3535
Phaxio.api_secret = '22222'
36-
Phaxio.callback_token = '33333'
36+
Phaxio.webhook_token = '33333'
3737
```
3838

3939
Try sending a fax:
@@ -243,32 +243,32 @@ Account.get
243243
# => Account(balance: 1000, faxes_today: 0, faxes_this_month: 100)
244244
```
245245

246-
#### Callback
246+
#### Webhook
247247

248-
##### `Callback.valid_signature?`
248+
##### `Webhook.valid_signature?`
249249

250-
Validate the callback signature sent with a Phaxio callback. Requires that Phaxio.callback_token be
250+
Validate the webhook signature sent with a Phaxio webhook. Requires that Phaxio.webhook_token be
251251
set.
252252

253253
``` ruby
254-
Callback.valid_signature? received_signature, callback_url, received_params, received_files
254+
Webhook.valid_signature? received_signature, webhook_url, received_params, received_files
255255
# => true
256256
```
257257

258-
## Callback Validation Example with Sinatra
258+
## Webhook Validation Example with Sinatra
259259

260260
``` ruby
261261
require 'sinatra/base'
262262
require 'phaxio'
263263

264264
class PhaxioWebhookExample < Sinatra::Base
265-
Phaxio.callback_token = 'YOUR WEBHOOK TOKEN HERE'
266-
265+
Phaxio.webhook_token = 'YOUR WEBHOOK TOKEN HERE'
266+
267267
post '/webhook' do
268268
signature = request.env['HTTP_X_PHAXIO_SIGNATURE']
269269
url = request.url
270270
file_params = params[:file]
271-
if Phaxio::Callback.valid_signature? signature, url, webhook_params, file_params
271+
if Phaxio::Webhook.valid_signature? signature, url, webhook_params, file_params
272272
'Success'
273273
else
274274
'Invalid webhook signature'
@@ -289,22 +289,22 @@ end
289289
class WebhookController < ApplicationController
290290
skip_before_action :verify_authenticity_token
291291

292-
def index
292+
def index
293293
signature = request.headers['X-Phaxio-Signature']
294-
Phaxio.callback_token = 'YOUR WEBHOOK TOKEN HERE'
294+
Phaxio.webhook_token = 'YOUR WEBHOOK TOKEN HERE'
295295
url = request.original_url
296296

297297
Rails.logger.debug "URL: " + url
298298
Rails.logger.debug "Signature: " + signature
299299
Rails.logger.debug "params: " + params.inspect
300300
Rails.logger.debug "webhook_params: " + webhook_params.to_h.inspect
301-
302-
if Phaxio::Callback.valid_signature? signature, url, webhook_params.to_h, file_params
301+
302+
if Phaxio::Webhook.valid_signature? signature, url, webhook_params.to_h, file_params
303303
Rails.logger.debug "Success"
304304
render plain: 'Success'
305305
else
306-
Rails.logger.debug "Invalid callback signature"
307-
render plain: 'Invalid callback signature'
306+
Rails.logger.debug "Invalid webhook signature"
307+
render plain: 'Invalid webhook signature'
308308
end
309309
end
310310

lib/phaxio.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
end
1919

2020
%w[
21-
fax_recipient fax account callback phax_code phone_number public ata
21+
fax_recipient fax account webhook phax_code phone_number public ata
2222
port_number_note port_number port_order
2323
].each do |filename|
2424
require File.expand_path(File.join('..', 'phaxio', 'resources', filename), __FILE__)
@@ -32,11 +32,11 @@ class << self
3232
# @see Config.api_key
3333
# @!attribute api_secret
3434
# @see Config.api_secret
35-
# @!attribute callback_token
36-
# @see Config.callback_token
35+
# @!attribute webhook_token
36+
# @see Config.webhook_token
3737
# @!attribute api_endpoint
3838
# @see Config.api_endpoint
39-
%w(api_key api_secret callback_token api_endpoint).each do |config_attribute|
39+
%w(api_key api_secret webhook_token api_endpoint).each do |config_attribute|
4040
# Define getters
4141
define_method(config_attribute) do
4242
Config.public_send config_attribute
@@ -48,5 +48,9 @@ class << self
4848
Config.public_send setter, value
4949
end
5050
end
51+
52+
# for backwards compatibility
53+
alias callback_token webhook_token
54+
alias callback_token= webhook_token=
5155
end
5256
end

lib/phaxio/config.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ class << self
1313
# To find your API secret, visit https://console.phaxio.com/api_credentials
1414
attr_accessor :api_secret
1515

16-
# Your Phaxio callback token. This will be used to verify that callback requests are coming
16+
# Your Phaxio webhook token. This will be used to verify that webhook requests are coming
1717
# from Phaxio.
1818
#
19-
# To find your callback token, visit https://console.phaxio.com/user/callbacks/edit
20-
attr_accessor :callback_token
19+
# To find your webhook token, visit https://console.phaxio.com/user/callbacks/edit
20+
attr_accessor :webhook_token
2121

2222
# The Phaxio API endpoint. Users generally shouldn't need to change it.
2323
# Defaults to https://api.phaxio.com/v2.1/
Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module Resources
33
# Provides utilities for working with callbacks.
44
# @see https://www.phaxio.com/docs/api/v2.1/faxes/send_webhook
55
# @see https://www.phaxio.com/docs/api/v2.1/faxes/receive_webhooks
6-
class Callback
6+
class Webhook
77
DIGEST = OpenSSL::Digest.new('sha1')
88
private_constant :DIGEST
99

@@ -13,11 +13,11 @@ class << self
1313
# @param signature [String]
1414
# The signature received from Phaxio.
1515
# @param url [String]
16-
# The callback URL used in this request.
16+
# The webhook URL used in this request.
1717
# @param params [Hash]
18-
# The parameters received with the callback, excluding files.
18+
# The parameters received with the webhook, excluding files.
1919
# @param files [Array<File>]
20-
# The files received with the callback, if any.
20+
# The files received with the webhook, if any.
2121
# @return [true, false]
2222
# @raise [Phaxio::Error::PhaxioError]
2323
# @see https://www.phaxio.com/docs/security/callbacks
@@ -31,12 +31,12 @@ def valid_signature? signature, url, params, files = []
3131
def generate_check_signature url, params, files = []
3232
params_string = generate_params_string(params)
3333
files_string = generate_files_string(files)
34-
callback_data = "#{url}#{params_string}#{files_string}"
35-
OpenSSL::HMAC.hexdigest(DIGEST, callback_token, callback_data)
34+
webhook_data = "#{url}#{params_string}#{files_string}"
35+
OpenSSL::HMAC.hexdigest(DIGEST, webhook_token, webhook_data)
3636
end
3737

38-
def callback_token
39-
Phaxio.callback_token or raise(Error::PhaxioError, 'No callback token has been set')
38+
def webhook_token
39+
Phaxio.webhook_token or raise(Error::PhaxioError, 'No webhook token has been set')
4040
end
4141

4242
def generate_params_string(params)
@@ -61,5 +61,8 @@ def generate_file_string(file)
6161
end
6262
end
6363
end
64+
65+
# for backwards compatibility
66+
Callback = Webhook
6467
end
6568
end

spec/phaxio_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
expect(subject.api_secret).to eq('test-api-secret')
1414
end
1515

16-
it 'sets the callback token' do
17-
subject.callback_token = 'test-callback-token'
18-
expect(subject.callback_token).to eq('test-callback-token')
16+
it 'sets the webhook token' do
17+
subject.webhook_token = 'test-webhook-token'
18+
expect(subject.webhook_token).to eq('test-webhook-token')
1919
end
2020
end
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
require 'spec_helper'
22

3-
RSpec.describe Callback do
4-
describe 'validating a callback signature' do
5-
let(:action) { Callback.valid_signature? signature, url, params, files }
6-
let(:signature) { '663099785e5eff09f0cd6f2bd5d78c852f3b670d' }
3+
RSpec.describe Webhook do
4+
describe 'validating a webhook signature' do
5+
let(:action) { Webhook.valid_signature? signature, url, params, files }
6+
let(:signature) { '64a735ef0c47a0ae671e381c046648f0966deb29' }
77
let(:url) { 'example.com' }
88
let(:params) { {test: true} }
99
let(:files) { [] }
1010

11-
it 'raises an error if Phaxio::Config.callback_token is unset' do
12-
Phaxio.callback_token = nil
11+
it 'raises an error if Phaxio::Config.webhook_token is unset' do
12+
Phaxio.webhook_token = nil
1313
expect {
1414
action
15-
}.to raise_error(Phaxio::Error::PhaxioError, 'No callback token has been set')
15+
}.to raise_error(Phaxio::Error::PhaxioError, 'No webhook token has been set')
1616
end
1717

1818
context 'signature matches' do
@@ -31,4 +31,4 @@
3131
end
3232
end
3333
end
34-
end
34+
end

spec/support/credentials.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
config.before(:each) do
33
Phaxio.api_key = ENV['PHAXIO_API_KEY'] || 'test-api-key'
44
Phaxio.api_secret = ENV['PHAXIO_API_SECRET'] || 'test-api-secret'
5-
Phaxio.callback_token = ENV['PHAXIO_CALLBACK_TOKEN'] || 'test-callback-token'
5+
Phaxio.webhook_token = ENV['PHAXIO_WEBHOOK_TOKEN'] || 'test-webhook-token'
66
end
77
end

0 commit comments

Comments
 (0)