Skip to content

Commit 5b988d9

Browse files
authored
Merge pull request #42 from phaxio/FAX-1989
Fax 1989
2 parents da2f567 + 43dcb9a commit 5b988d9

77 files changed

Lines changed: 2617 additions & 1138 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

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: 9 additions & 3 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 callback 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,13 @@ class << self
3232
# @see Config.api_key
3333
# @!attribute api_secret
3434
# @see Config.api_secret
35+
# @!attribute webhook_token
36+
# @see Config.webhook_token
3537
# @!attribute callback_token
36-
# @see Config.callback_token
38+
# @see Config.webhook_token
3739
# @!attribute api_endpoint
3840
# @see Config.api_endpoint
39-
%w(api_key api_secret callback_token api_endpoint).each do |config_attribute|
41+
%w(api_key api_secret webhook_token callback_token api_endpoint).each do |config_attribute|
4042
# Define getters
4143
define_method(config_attribute) do
4244
Config.public_send config_attribute
@@ -48,5 +50,9 @@ class << self
4850
Config.public_send setter, value
4951
end
5052
end
53+
54+
# for backwards compatibility
55+
alias callback_token webhook_token
56+
alias callback_token= webhook_token=
5157
end
5258
end

lib/phaxio/config.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@ 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
21+
alias callback_token webhook_token
22+
alias callback_token= webhook_token=
2123

2224
# The Phaxio API endpoint. Users generally shouldn't need to change it.
2325
# Defaults to https://api.phaxio.com/v2.1/

lib/phaxio/resource.rb

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,18 @@ class Collection
139139
#
140140
# @see Phaxio::Resource.response_collection
141141
def initialize response_data, resource
142-
if response_data.key? 'paging'
143-
self.total = response_data['paging']['total']
144-
self.per_page = response_data['paging']['per_page']
145-
self.page = response_data['paging']['page']
142+
# For some endpoints we'll get a hash with `paging` and `data` attributes.
143+
# For others, just an array.
144+
if response_data.is_a? Hash
145+
if response_data.key? 'paging'
146+
self.total = response_data['paging']['total']
147+
self.per_page = response_data['paging']['per_page']
148+
self.page = response_data['paging']['page']
149+
end
150+
self.raw_data = response_data['data']
151+
else
152+
self.raw_data = response_data
146153
end
147-
self.raw_data = response_data['data']
148154
self.collection = raw_data.map { |record_data| resource.response_record record_data }
149155
end
150156

lib/phaxio/resources/ata.rb

Lines changed: 80 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,37 @@ class Ata < Resource
2020
# @return [String] The domain for the ATA.
2121
# @!attribute domain
2222

23+
# @return [String] The user agent for the ATA.
24+
# @!attribute user_agent
25+
26+
# @return [String] The SIP URI for the ATA.
27+
# @!attribute sip_uri
28+
2329
# @return [String] The mac address for the ATA.
2430
# @!attribute mac_address
2531

32+
# @return [String] The name of the group which the ATA belongs to.
33+
# @!attribute group
34+
2635
# @return [String] The username for the ATA.
27-
# @!attribute uername
36+
# @!attribute username
2837

2938
# @return [String] The password for the ATA.
3039
# @!attribute password
3140

3241
has_normal_attributes %w[
33-
id name description user_phone_number domain username password mac_address
42+
id name description user_phone_number domain user_agent sip_uri
43+
mac_address group username password
44+
]
45+
46+
# @return [Time] The time at which the ATA was last registered.
47+
# @!attribute last_registered
48+
49+
# @return [Time] The time at which the ATA's registration expires.
50+
# @!attribute expiry_time
51+
52+
has_time_attributes %w[
53+
last_registered expiry_time
3454
]
3555

3656
# A reference to an ATA. This is returned by certain actions which don't
@@ -44,28 +64,48 @@ def to_i
4464
id
4565
end
4666

67+
# Gets the referenced ATA.
68+
# @return [Phaxio::Resources::ATA]
69+
def get
70+
Ata.get self
71+
end
72+
alias :retrieve :get
73+
alias :find :get
74+
4775
private
4876

4977
def initialize id
5078
self.id = id
5179
end
5280
end
5381

54-
# A reference to a phone number, returned by ATA phone number management
55-
# actions.
56-
class PhoneNumberReference
57-
# @return [String]
58-
# The phone number.
59-
attr_accessor :phone_number
82+
# A set of provisioning URLs.
83+
class ProvisioningURLs
84+
# @return [Hash<String: String>] The hash of provisioning URLs.
85+
# @!attribute urls
86+
attr_reader :urls
87+
88+
GRANDSTREAM = 'Grandstream'
89+
OBI = 'OBi'
90+
NETGEN = 'Netgen'
6091

61-
def to_s
62-
phone_number
92+
def initialize data
93+
@urls = data
6394
end
6495

65-
private
96+
# @return [String] The Grandstream provisioning url.
97+
def grandstream
98+
self.urls.fetch(GRANDSTREAM)
99+
end
100+
101+
# @return [String] The OBi provisioning url.
102+
def obi
103+
self.urls.fetch(OBI)
104+
end
66105

67-
def initialize phone_number
68-
self.phone_number = phone_number
106+
# @return [String] The Netgen provisioning url.
107+
def netgen
108+
self.urls.fetch(NETGEN)
69109
end
70110
end
71111

@@ -103,7 +143,9 @@ def create params = {}
103143
# @param id [Integer]
104144
# The ID of the ATA to retrieve information about.
105145
# @param params [Hash]
106-
# Any parameters to send to Phaxio. This action takes no unique parameters.
146+
# Any parameters to send to Phaxio.
147+
# - *with_credentials* [Boolean] - If enabled, the username and
148+
# password for the ATA will be included in the response.
107149
# @return [Phaxio::Resources::Ata]
108150
# The requested ATA.
109151
# @raise [Phaxio::Error::PhaxioError]
@@ -148,7 +190,7 @@ def regenerate_credentials id, params = {}
148190

149191
# Delete an ATA
150192
# @param id [Integer]
151-
# The Id of the ATA to delete.
193+
# The ID of the ATA to delete.
152194
# @param params [Hash]
153195
# Any parameters to send to Phaxio. This action takes no unique parameters.
154196
# @return [Phaxio::Resources::Ata::Reference]
@@ -167,7 +209,7 @@ def delete id, params = {}
167209
# The phone number to add to the ATA.
168210
# @param params [Hash]
169211
# Any parameters to send to Phaxio. This action takes no unique parameters.
170-
# @return [Phaxio::Resources::Ata::PhoneNumberReference]
212+
# @return [Phaxio::Resources::PhoneNumber::Reference]
171213
# A reference to the added phone number.
172214
# @raise [Phaxio::Error::PhaxioError]
173215
# @see https://www.phaxio.com/docs/api/v2.1/atas/add_phone_number
@@ -183,7 +225,7 @@ def add_phone_number id, phone_number, params = {}
183225
# The phone number you want to remove.
184226
# @param params [Hash]
185227
# Any parameters to send to Phaxio. This action takes no unique parameters.
186-
# @return [Phaxio::Resources::Ata::PhoneNumberReference]
228+
# @return [Phaxio::Resources::PhoneNumber::Reference]
187229
# A reference to the removed phone number.
188230
# @raise [Phaxio::Error::PhaxioError]
189231
# @see https://www.phaxio.com/docs/api/v2.1/atas/remove_phone_number
@@ -192,14 +234,30 @@ def remove_phone_number id, phone_number, params = {}
192234
response_phone_number_reference response
193235
end
194236

237+
# Get ATA provisioning URLs for your Phaxio account.
238+
# @param params [Hash]
239+
# Any parameters to send to Phaxio.
240+
# - *group* [String] - If given, this action instead returns
241+
# provisioning URLs for the named group.
242+
# @return [Phaxio::Resources::Ata::ProvisioningURLs
243+
# @see https://www.phaxio.com/docs/api/v2.1/atas/provisioning_urls
244+
def provisioning_urls params = {}
245+
response = Client.request :get, provisioning_urls_endpoint, params
246+
response_provisioning_urls response
247+
end
248+
195249
private
196250

197251
def response_reference response
198252
Reference.new Integer(response['id'])
199253
end
200254

201255
def response_phone_number_reference response
202-
PhoneNumberReference.new(response['phone_number'])
256+
PhoneNumber::Reference.new(response['phone_number'])
257+
end
258+
259+
def response_provisioning_urls response
260+
ProvisioningURLs.new(response)
203261
end
204262

205263
def atas_endpoint
@@ -217,6 +275,10 @@ def regenerate_credentials_endpoint id
217275
def phone_number_endpoint id, phone_number
218276
"#{ata_endpoint(id)}/phone_numbers/#{phone_number}"
219277
end
278+
279+
def provisioning_urls_endpoint
280+
"#{atas_endpoint}/provisioning_urls"
281+
end
220282
end
221283
end
222284
end

0 commit comments

Comments
 (0)