phaxio 2.0.0 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (90) hide show
  1. checksums.yaml +5 -5
  2. data/Gemfile +2 -1
  3. data/README.md +53 -20
  4. data/lib/phaxio/client.rb +46 -16
  5. data/lib/phaxio/config.rb +7 -5
  6. data/lib/phaxio/resource.rb +11 -5
  7. data/lib/phaxio/resources/account.rb +1 -1
  8. data/lib/phaxio/resources/ata.rb +285 -0
  9. data/lib/phaxio/resources/callback.rb +10 -56
  10. data/lib/phaxio/resources/fax.rb +11 -11
  11. data/lib/phaxio/resources/phax_code.rb +2 -2
  12. data/lib/phaxio/resources/phone_number.rb +31 -5
  13. data/lib/phaxio/resources/port_number.rb +42 -0
  14. data/lib/phaxio/resources/port_number_note.rb +51 -0
  15. data/lib/phaxio/resources/port_order.rb +172 -0
  16. data/lib/phaxio/resources/public/area_code.rb +1 -1
  17. data/lib/phaxio/resources/public/country.rb +1 -1
  18. data/lib/phaxio/resources/webhook.rb +65 -0
  19. data/lib/phaxio/version.rb +1 -1
  20. data/lib/phaxio.rb +13 -3
  21. data/phaxio.gemspec +2 -1
  22. data/spec/client_spec.rb +38 -6
  23. data/spec/phaxio_spec.rb +7 -1
  24. data/spec/resources/account_spec.rb +1 -7
  25. data/spec/resources/ata_spec.rb +207 -0
  26. data/spec/resources/fax_spec.rb +42 -89
  27. data/spec/resources/phax_code_spec.rb +26 -32
  28. data/spec/resources/phone_number_spec.rb +19 -30
  29. data/spec/resources/port_number_note_spec.rb +33 -0
  30. data/spec/resources/port_number_spec.rb +32 -0
  31. data/spec/resources/port_order_spec.rb +76 -0
  32. data/spec/resources/public/area_code_spec.rb +1 -1
  33. data/spec/resources/public/country_spec.rb +1 -7
  34. data/spec/resources/{callback_spec.rb → webhook_spec.rb} +8 -8
  35. data/spec/spec_helper.rb +1 -0
  36. data/spec/support/{vcr_cassettes/resources/account/status.yml → cassettes/account/get.yml} +13 -14
  37. data/spec/support/cassettes/ata/add_phone_number.yml +89 -0
  38. data/spec/support/cassettes/ata/create.yml +46 -0
  39. data/spec/support/cassettes/ata/delete.yml +86 -0
  40. data/spec/support/cassettes/ata/get.yml +87 -0
  41. data/spec/support/cassettes/ata/list.yml +67 -0
  42. data/spec/support/cassettes/ata/provisioning_urls.yml +43 -0
  43. data/spec/support/cassettes/ata/reference.yml +87 -0
  44. data/spec/support/cassettes/ata/regenerate.yml +89 -0
  45. data/spec/support/cassettes/ata/remove_phone_number.yml +130 -0
  46. data/spec/support/cassettes/ata/update.yml +89 -0
  47. data/spec/support/cassettes/fax/cancel.yml +90 -0
  48. data/spec/support/cassettes/fax/create.yml +48 -0
  49. data/spec/support/cassettes/fax/delete.yml +88 -0
  50. data/spec/support/cassettes/fax/delete_file.yml +88 -0
  51. data/spec/support/cassettes/fax/file.yml +93 -0
  52. data/spec/support/cassettes/fax/get.yml +88 -0
  53. data/spec/support/cassettes/fax/list.yml +49 -0
  54. data/spec/support/cassettes/fax/reference.yml +88 -0
  55. data/spec/support/cassettes/fax/resend.yml +90 -0
  56. data/spec/support/cassettes/fax/test_receive.yml +49 -0
  57. data/spec/support/{vcr_cassettes/resources/fax/resend.yml → cassettes/phax_code/create.yml} +14 -15
  58. data/spec/support/cassettes/phax_code/create_png.yml +50 -0
  59. data/spec/support/cassettes/phax_code/get_by_id.yml +86 -0
  60. data/spec/support/{vcr_cassettes/resources/fax/delete.yml → cassettes/phax_code/get_default.yml} +14 -15
  61. data/spec/support/cassettes/phax_code/get_default_png.yml +48 -0
  62. data/spec/support/cassettes/phax_code/get_png_by_id.yml +90 -0
  63. data/spec/support/{vcr_cassettes/resources → cassettes}/phone_number/create.yml +15 -16
  64. data/spec/support/cassettes/phone_number/get.yml +44 -0
  65. data/spec/support/cassettes/phone_number/list.yml +72 -0
  66. data/spec/support/cassettes/phone_number/reference.yml +45 -0
  67. data/spec/support/cassettes/phone_number/release.yml +86 -0
  68. data/spec/support/cassettes/port_number/get.yml +88 -0
  69. data/spec/support/cassettes/port_number_note/list.yml +88 -0
  70. data/spec/support/cassettes/port_order/create.yml +48 -0
  71. data/spec/support/cassettes/port_order/get.yml +91 -0
  72. data/spec/support/cassettes/port_order/list.yml +52 -0
  73. data/spec/support/{vcr_cassettes/resources/public/area_codes → cassettes/public/area_code}/list.yml +18 -18
  74. data/spec/support/{vcr_cassettes/resources → cassettes}/public/country/list.yml +14 -15
  75. data/spec/support/files/test.txt +1 -0
  76. data/spec/support/vcr.rb +25 -8
  77. metadata +125 -31
  78. data/spec/support/credentials.rb +0 -7
  79. data/spec/support/vcr_cassettes/resources/fax/cancel.yml +0 -46
  80. data/spec/support/vcr_cassettes/resources/fax/create.yml +0 -230
  81. data/spec/support/vcr_cassettes/resources/fax/delete_file.yml +0 -44
  82. data/spec/support/vcr_cassettes/resources/fax/file.yml +0 -251
  83. data/spec/support/vcr_cassettes/resources/fax/get.yml +0 -44
  84. data/spec/support/vcr_cassettes/resources/fax/list.yml +0 -56
  85. data/spec/support/vcr_cassettes/resources/fax/test_receive.yml +0 -231
  86. data/spec/support/vcr_cassettes/resources/phax_code/create.yml +0 -100
  87. data/spec/support/vcr_cassettes/resources/phax_code/get.yml +0 -190
  88. data/spec/support/vcr_cassettes/resources/phone_number/get.yml +0 -45
  89. data/spec/support/vcr_cassettes/resources/phone_number/list.yml +0 -52
  90. data/spec/support/vcr_cassettes/resources/phone_number/release.yml +0 -44
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 6a10449404bdb1df029a52a878f49202589e727d
4
- data.tar.gz: f500188f94760b8300dc2f63b17eb4ad31edc5fa
2
+ SHA256:
3
+ metadata.gz: 508fcbe635de23974bfdefe06aec6811fd7f91bd03377dd4a90b971cd1b6a44f
4
+ data.tar.gz: e33347de232350332fe604ecae8c0f511a3774fb9ef7c863f6a269bb53bad787
5
5
  SHA512:
6
- metadata.gz: df06285d6e87fbc048f0ebcad0cd5ee608ccfb0b45c6813926f272c4302aa5dc60421445df11bacc6fae2e26a32951912ca201376a56c77bcd2e5fa71e0e5976
7
- data.tar.gz: 9d5bcfa978e6e92317d17d4bb0a31e4cd2f33f57d96f8241721bc581be34c768623704d951d4a177f5634dc0eb6a7f2cff4c8db7588e6e286a61ba2aa5020dd3
6
+ metadata.gz: 6a041ed3d71f144ae2c2776a69be5d947982f1eb76c2a2e748f07b245d48ac8714c9199702b71a9b8c0cd404ea6318518834cb9da9365f569cc6b35633c66e83
7
+ data.tar.gz: d50baaa1fc873a3a20268fb0c56d1b902d0b922d8fcdb731d40e23d89c74bd9c6fea3e35aa66e8c7472502b5af7ac1ad060b5cec22d3fe244e816d98d9be10d5
data/Gemfile CHANGED
@@ -4,10 +4,11 @@ gemspec
4
4
 
5
5
  group :development do
6
6
  gem 'bundler'
7
- gem 'rake', '~> 10'
7
+ gem 'rake'
8
8
  gem 'rspec', '~> 3.7'
9
9
  gem 'vcr'
10
10
  gem 'webmock'
11
11
  gem 'pry'
12
12
  gem 'yard'
13
+ gem 'dotenv', require: false
13
14
  end
data/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  [![Build Status](https://travis-ci.org/phaxio/phaxio-ruby.svg?branch=master)](https://travis-ci.org/phaxio/phaxio-ruby)
4
4
 
5
- A Ruby gem for interacting with the [Phaxio API](https://www.phaxio.com/docs/api/v2).
5
+ A Ruby gem for interacting with the [Phaxio API](https://www.phaxio.com/docs/api/v2.1).
6
6
 
7
7
  ## Installation
8
8
 
@@ -26,14 +26,14 @@ $ gem install phaxio
26
26
 
27
27
  ## Usage
28
28
 
29
- Set up your API Key, API Secret, and, optionally, Callback Token.
29
+ Set up your API Key, API Secret, and, optionally, Webhook Token.
30
30
 
31
31
  ``` ruby
32
32
  require 'phaxio'
33
33
 
34
34
  Phaxio.api_key = '11111'
35
35
  Phaxio.api_secret = '22222'
36
- Phaxio.callback_token = '33333'
36
+ Phaxio.webhook_token = '33333'
37
37
  ```
38
38
 
39
39
  Try sending a fax:
@@ -243,45 +243,78 @@ Account.get
243
243
  # => Account(balance: 1000, faxes_today: 0, faxes_this_month: 100)
244
244
  ```
245
245
 
246
- #### Callback
246
+ #### Webhook
247
247
 
248
- ##### `Callback.valid_signature?`
248
+ ##### `Webhook.valid_signature?`
249
249
 
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
251
251
  set.
252
252
 
253
253
  ``` 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
255
255
  # => true
256
256
  ```
257
257
 
258
- ## Callback Validation Example with Sinatra
258
+ ## Webhook Validation Example with Sinatra
259
259
 
260
260
  ``` ruby
261
261
  require 'sinatra/base'
262
262
  require 'phaxio'
263
263
 
264
- class PhaxioCallbackExample < Sinatra::Base
265
- Phaxio.config do |config|
266
- config.api_key = '0123456789'
267
- config.api_secret = '0123456789'
268
- config.callback_token = '0123456789'
269
- end
264
+ class PhaxioWebhookExample < Sinatra::Base
265
+ Phaxio.webhook_token = 'YOUR WEBHOOK TOKEN HERE'
270
266
 
271
- post '/phaxio_callback' do
267
+ post '/webhook' do
272
268
  signature = request.env['HTTP_X_PHAXIO_SIGNATURE']
273
269
  url = request.url
274
- file_params = params[:filename]
275
- if Phaxio::Callback.valid_signature? signature, url, callback_params, file_params
270
+ file_params = params[:file]
271
+ if Phaxio::Webhook.valid_signature? signature, url, webhook_params, file_params
276
272
  'Success'
277
273
  else
278
- 'Invalid callback signature'
274
+ 'Invalid webhook signature'
279
275
  end
280
276
  end
281
277
 
282
- def callback_params
278
+ def webhook_params
283
279
  params.select do |key, _value|
284
- %w(success is_test direction fax metadata message).include?(key)
280
+ %w(success is_test direction fax metadata message event_type).include?(key)
281
+ end
282
+ end
283
+ end
284
+ ```
285
+
286
+ ## Webhook Validation Example with Rails Controller
287
+
288
+ ``` ruby
289
+ class WebhookController < ApplicationController
290
+ skip_before_action :verify_authenticity_token
291
+
292
+ def index
293
+ signature = request.headers['X-Phaxio-Signature']
294
+ Phaxio.webhook_token = 'YOUR WEBHOOK TOKEN HERE'
295
+ url = request.original_url
296
+
297
+ Rails.logger.debug "URL: " + url
298
+ Rails.logger.debug "Signature: " + signature
299
+ Rails.logger.debug "params: " + params.inspect
300
+ Rails.logger.debug "webhook_params: " + webhook_params.to_h.inspect
301
+
302
+ if Phaxio::Webhook.valid_signature? signature, url, webhook_params.to_h, file_params
303
+ Rails.logger.debug "Success"
304
+ render plain: 'Success'
305
+ else
306
+ Rails.logger.debug "Invalid webhook signature"
307
+ render plain: 'Invalid webhook signature'
308
+ end
309
+ end
310
+
311
+ def webhook_params
312
+ params.permit(:success, :is_test, :direction, :fax, :metadata, :event_type, :message)
313
+ end
314
+
315
+ def file_params
316
+ if params[:file]
317
+ [{ :name => 'file', :tempfile => params[:file].tempfile }]
285
318
  end
286
319
  end
287
320
  end
data/lib/phaxio/client.rb CHANGED
@@ -27,6 +27,7 @@ module Phaxio
27
27
  begin
28
28
  response = case method.to_s
29
29
  when 'post' then post(endpoint, params)
30
+ when 'patch' then patch(endpoint, params)
30
31
  when 'get' then get(endpoint, params)
31
32
  when 'delete' then delete(endpoint, params)
32
33
  else raise ArgumentError, "HTTP method `#{method}` is not supported."
@@ -55,10 +56,7 @@ module Phaxio
55
56
  body = JSON.parse(response.body).with_indifferent_access
56
57
  else
57
58
  extension = MimeTypeHelper.extension_for_mimetype content_type
58
- filename = File.join(
59
- Dir.tmpdir,
60
- Dir::Tmpname.make_tmpname('phaxio-', "download.#{extension}")
61
- )
59
+ filename = File.join Dir.tmpdir, tmpname(extension)
62
60
  File.open(filename, 'wb') { |file| file.write response.body }
63
61
  body = {'success' => response.success?, 'data' => File.open(filename, 'rb')}
64
62
  end
@@ -93,28 +91,54 @@ module Phaxio
93
91
  end
94
92
  end
95
93
 
94
+ def tmpname(extension)
95
+ t = Time.now.strftime("%Y%m%d")
96
+ "phaxio-#{t}-#{$$}-#{rand(0x100000000).to_s(36)}-download.#{extension}"
97
+ end
98
+
96
99
  def post endpoint, params = {}
97
100
  # Handle file params
98
101
  params.each do |k, v|
99
102
  next unless k.to_s == 'file'
100
- mime_type = MimeTypeHelper.mimetype_for_file v.path
101
- params[k] = Faraday::UploadIO.new v, mime_type
103
+
104
+ if v.is_a? Array
105
+ file_param = v.map { |file| file_to_param file }
106
+ else
107
+ file_param = file_to_param v
108
+ end
109
+
110
+ params[k] = file_param
111
+ end
112
+
113
+ conn.post endpoint, params, api_headers(params)
114
+ end
115
+
116
+ def patch endpoint, params = {}
117
+ # Handle file params
118
+ params.each do |k, v|
119
+ next unless k.to_s == 'file'
120
+
121
+ if v.is_a? Array
122
+ file_param = v.map { |file| file_to_param file }
123
+ else
124
+ file_param = file_to_param v
125
+ end
126
+
127
+ params[k] = file_param
102
128
  end
103
129
 
104
- conn.post endpoint, params
130
+ conn.patch endpoint, params, api_headers(params)
105
131
  end
106
132
 
107
133
  def get endpoint, params = {}
108
- conn.get endpoint, params
134
+ conn.get endpoint, params, api_headers(params)
109
135
  end
110
136
 
111
137
  def delete endpoint, params = {}
112
- conn.delete endpoint, params
138
+ conn.delete endpoint, params, api_headers(params)
113
139
  end
114
140
 
115
141
  def api_params params
116
- params = default_params.merge params
117
-
118
142
  # Convert times to ISO 8601
119
143
  params.each do |k, v|
120
144
  next unless v.kind_of?(Time) || v.kind_of?(Date)
@@ -124,11 +148,17 @@ module Phaxio
124
148
  params
125
149
  end
126
150
 
127
- def default_params
128
- {
129
- api_key: Phaxio.api_key,
130
- api_secret: Phaxio.api_secret
131
- }
151
+ def api_headers params
152
+ api_key = params.delete(:api_key) || Phaxio.api_key
153
+ api_secret = params.delete(:api_secret) || Phaxio.api_secret
154
+ return unless api_key && api_secret
155
+ auth = Base64.strict_encode64("#{api_key}:#{api_secret}")
156
+ {'Authorization' => "Basic #{auth}"}
157
+ end
158
+
159
+ def file_to_param file
160
+ mime_type = MimeTypeHelper.mimetype_for_file file.path
161
+ Faraday::UploadIO.new file, mime_type
132
162
  end
133
163
  end
134
164
  end
data/lib/phaxio/config.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  module Phaxio
2
2
  class Config
3
- DEFAULT_API_ENDPOINT = 'https://api.phaxio.com/v2/'.freeze
3
+ DEFAULT_API_ENDPOINT = 'https://api.phaxio.com/v2.1/'.freeze
4
4
 
5
5
  class << self
6
6
  # Your Phaxio API key. This will be used for all interactions with the Phaxio API.
@@ -13,14 +13,16 @@ module Phaxio
13
13
  # To find your API secret, visit https://console.phaxio.com/api_credentials
14
14
  attr_accessor :api_secret
15
15
 
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
17
17
  # from Phaxio.
18
18
  #
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=
21
23
 
22
24
  # The Phaxio API endpoint. Users generally shouldn't need to change it.
23
- # Defaults to https://api.phaxio.com/v2/
25
+ # Defaults to https://api.phaxio.com/v2.1/
24
26
  attr_writer :api_endpoint
25
27
 
26
28
  def api_endpoint
@@ -139,12 +139,18 @@ module Phaxio
139
139
  #
140
140
  # @see Phaxio::Resource.response_collection
141
141
  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
146
153
  end
147
- self.raw_data = response_data['data']
148
154
  self.collection = raw_data.map { |record_data| resource.response_record record_data }
149
155
  end
150
156
 
@@ -22,7 +22,7 @@ module Phaxio
22
22
  # Any parameters to send to Phaxio. This action does not have any unique parameters.
23
23
  # @return [Phaxio::Resources::Acount] Your account information.
24
24
  # @raise [Phaxio::Error::PhaxioError]
25
- # @see https://www.phaxio.com/docs/api/v2/account/status
25
+ # @see https://www.phaxio.com/docs/api/v2.1/account/status
26
26
  def get params = {}
27
27
  response = Client.request :get, account_status_endpoint, params
28
28
  response_record response
@@ -0,0 +1,285 @@
1
+ module Phaxio
2
+ module Resources
3
+ # Provides functionality for managing ATAs.
4
+ class Ata < Resource
5
+ ATAS_PATH = 'atas'.freeze
6
+ private_constant :ATAS_PATH
7
+
8
+ # @return [Integer] the ID of the ATA.
9
+ # @!attribute id
10
+
11
+ # @return [String] the name of the ATA.
12
+ # @!attribute name
13
+
14
+ # @return [String] the description of the ATA.
15
+ # @!attribute description
16
+
17
+ # @return [String] The user phone number associated with the ATA.
18
+ # @!attribute user_phone_number
19
+
20
+ # @return [String] The domain for the ATA.
21
+ # @!attribute domain
22
+
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
+
29
+ # @return [String] The mac address for the ATA.
30
+ # @!attribute mac_address
31
+
32
+ # @return [String] The name of the group which the ATA belongs to.
33
+ # @!attribute group
34
+
35
+ # @return [String] The username for the ATA.
36
+ # @!attribute username
37
+
38
+ # @return [String] The password for the ATA.
39
+ # @!attribute password
40
+
41
+ has_normal_attributes %w[
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
54
+ ]
55
+
56
+ # A reference to an ATA. This is returned by certain actions which don't
57
+ # return the full ATA.
58
+ class Reference
59
+ # @return [Integer]
60
+ # The ID of the referenced ATA.
61
+ attr_accessor :id
62
+
63
+ def to_i
64
+ id
65
+ end
66
+
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
+
75
+ private
76
+
77
+ def initialize id
78
+ self.id = id
79
+ end
80
+ end
81
+
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'
91
+
92
+ def initialize data
93
+ @urls = data
94
+ end
95
+
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
105
+
106
+ # @return [String] The Netgen provisioning url.
107
+ def netgen
108
+ self.urls.fetch(NETGEN)
109
+ end
110
+ end
111
+
112
+ class << self
113
+ # @macro paging
114
+ # List ATAs
115
+ # @param params[Hash]
116
+ # Any parameters to send to Phaxio.
117
+ # @return [Phaxio::Resource::Collection<Phaxio::Resources::Ata>]
118
+ # The collection of ATAs matching your request.
119
+ # @raise [Phaxio::Error::PhaxioError]
120
+ # @see https://www.phaxio.com/docs/api/v2.1/atas/list
121
+ def list params = {}
122
+ response = Client.request :get, atas_endpoint, params
123
+ response_collection response
124
+ end
125
+
126
+ # Create an ATA
127
+ # @param params [Hash]
128
+ # Any parameters to send to Phaxio.
129
+ # - *name* [String] - A name used to identify the ATA.
130
+ # - *description* [String] - A longer description of the ATA.
131
+ # - *domain* [String] - A domain for the ATA.
132
+ # - *mac_address* [String] - A mac address for the ATA.
133
+ # @return [Phaxio::Resources::Ata]
134
+ # The created ATA, including the generated username and password.
135
+ # @raise [Phaxio::Error::PhaxioError]
136
+ # @see https://www.phaxio.com/docs/api/v2.1/atas/create
137
+ def create params = {}
138
+ response = Client.request :post, atas_endpoint, params
139
+ response_record response
140
+ end
141
+
142
+ # Get an ATA
143
+ # @param id [Integer]
144
+ # The ID of the ATA to retrieve information about.
145
+ # @param params [Hash]
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.
149
+ # @return [Phaxio::Resources::Ata]
150
+ # The requested ATA.
151
+ # @raise [Phaxio::Error::PhaxioError]
152
+ # @see https://www.phaxio.com/docs/api/v2.1/atas/get
153
+ def get id, params = {}
154
+ response = Client.request :get, ata_endpoint(id.to_i), params
155
+ response_record response
156
+ end
157
+ alias :retrieve :get
158
+ alias :find :get
159
+
160
+ # Update an ATA
161
+ # @param id [Integer]
162
+ # The ID of the ATA to update.
163
+ # @param params [Hash]
164
+ # Any parameters to send to Phaxio.
165
+ # - *name* [String] - A name used to identify the ATA.
166
+ # - *description* [String] - A longer description of the ATA.
167
+ # - *mac_address* [String] - A mac address for the ATA.
168
+ # @return [Phaxio::Resources::Ata]
169
+ # The updated ATA.
170
+ # @raise [Phaxio::Error::PhaxioError]
171
+ # @see https://www.phaxio.com/docs/api/v2.1/atas/update
172
+ def update id, params = {}
173
+ response = Client.request :patch, ata_endpoint(id.to_i), params
174
+ response_record response
175
+ end
176
+
177
+ # Regenerate credentials for an ATA
178
+ # @param id [Integer]
179
+ # The ID of the ATA for which credentials should be regenerated.
180
+ # @param params [Hash]
181
+ # Any parameters to send to Phaxio. This action takes no unique parameters.
182
+ # @return [Phaxio::Resources::Ata]
183
+ # The ATA, including the new username and password.
184
+ # @raise Phaxio::Error::PhaxioError
185
+ # @see https://www.phaxio.com/docs/api/v2.1/atas/regenerate_credentials
186
+ def regenerate_credentials id, params = {}
187
+ response = Client.request :patch, regenerate_credentials_endpoint(id.to_i), params
188
+ response_record response
189
+ end
190
+
191
+ # Delete an ATA
192
+ # @param id [Integer]
193
+ # The ID of the ATA to delete.
194
+ # @param params [Hash]
195
+ # Any parameters to send to Phaxio. This action takes no unique parameters.
196
+ # @return [Phaxio::Resources::Ata::Reference]
197
+ # A reference to the deleted ATA.
198
+ # @raise [Phaxio::Error::PhaxioError]
199
+ # @see https://www.phaxio.com/docs/api/v2.1/atas/delete
200
+ def delete id, params = {}
201
+ response = Client.request :delete, ata_endpoint(id.to_i), params
202
+ response_reference response
203
+ end
204
+
205
+ # Add a phone number
206
+ # @param id [Integer]
207
+ # The ID of the ATA to which you want to add a number.
208
+ # @param phone_number [String]
209
+ # The phone number to add to the ATA.
210
+ # @param params [Hash]
211
+ # Any parameters to send to Phaxio. This action takes no unique parameters.
212
+ # @return [Phaxio::Resources::PhoneNumber::Reference]
213
+ # A reference to the added phone number.
214
+ # @raise [Phaxio::Error::PhaxioError]
215
+ # @see https://www.phaxio.com/docs/api/v2.1/atas/add_phone_number
216
+ def add_phone_number id, phone_number, params = {}
217
+ response = Client.request :post, phone_number_endpoint(id, phone_number), params
218
+ response_phone_number_reference response
219
+ end
220
+
221
+ # Remove a phone number
222
+ # @param id [Integer]
223
+ # The ID of the ATA from which you want to remove the phone number.
224
+ # @param phone_number [String]
225
+ # The phone number you want to remove.
226
+ # @param params [Hash]
227
+ # Any parameters to send to Phaxio. This action takes no unique parameters.
228
+ # @return [Phaxio::Resources::PhoneNumber::Reference]
229
+ # A reference to the removed phone number.
230
+ # @raise [Phaxio::Error::PhaxioError]
231
+ # @see https://www.phaxio.com/docs/api/v2.1/atas/remove_phone_number
232
+ def remove_phone_number id, phone_number, params = {}
233
+ response = Client.request :delete, phone_number_endpoint(id, phone_number), params
234
+ response_phone_number_reference response
235
+ end
236
+
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
+
249
+ private
250
+
251
+ def response_reference response
252
+ Reference.new Integer(response['id'])
253
+ end
254
+
255
+ def response_phone_number_reference response
256
+ PhoneNumber::Reference.new(response['phone_number'])
257
+ end
258
+
259
+ def response_provisioning_urls response
260
+ ProvisioningURLs.new(response)
261
+ end
262
+
263
+ def atas_endpoint
264
+ ATAS_PATH
265
+ end
266
+
267
+ def ata_endpoint id
268
+ "#{atas_endpoint}/#{id}"
269
+ end
270
+
271
+ def regenerate_credentials_endpoint id
272
+ "#{ata_endpoint(id)}/regenerate_credentials"
273
+ end
274
+
275
+ def phone_number_endpoint id, phone_number
276
+ "#{ata_endpoint(id)}/phone_numbers/#{phone_number}"
277
+ end
278
+
279
+ def provisioning_urls_endpoint
280
+ "#{atas_endpoint}/provisioning_urls"
281
+ end
282
+ end
283
+ end
284
+ end
285
+ end
@@ -1,63 +1,17 @@
1
1
  module Phaxio
2
2
  module Resources
3
- # Provides utilities for working with callbacks.
4
- # @see https://www.phaxio.com/docs/api/v2/faxes/send_callback
5
- # @see https://www.phaxio.com/docs/api/v2/faxes/receive_callback
3
+ # This class is provided for the sake of backwards compatibility; use the Webhook resource instead.
4
+ # @see Phaxio::Resources::Webhook
6
5
  class Callback
7
- DIGEST = OpenSSL::Digest.new('sha1')
8
- private_constant :DIGEST
9
-
10
6
  class << self
11
- # Determines whether or not the passed signature is valid for the given
12
- # url, params, and files.
13
- # @param signature [String]
14
- # The signature received from Phaxio.
15
- # @param url [String]
16
- # The callback URL used in this request.
17
- # @param params [Hash]
18
- # The parameters received with the callback, excluding files.
19
- # @param files [Array<File>]
20
- # The files received with the callback, if any.
21
- # @return [true, false]
22
- # @raise [Phaxio::Error::PhaxioError]
23
- # @see https://www.phaxio.com/docs/security/callbacks
24
- def valid_signature? signature, url, params, files = []
25
- check_signature = generate_check_signature url, params, files
26
- check_signature == signature
27
- end
28
-
29
- private
30
-
31
- def generate_check_signature url, params, files = []
32
- params_string = generate_params_string(params)
33
- files_string = generate_files_string(files)
34
- callback_data = "#{url}#{params_string}#{files_string}"
35
- OpenSSL::HMAC.hexdigest(DIGEST, callback_token, callback_data)
36
- end
37
-
38
- def callback_token
39
- Phaxio.callback_token or raise(Error::PhaxioError, 'No callback token has been set')
40
- end
41
-
42
- def generate_params_string(params)
43
- sorted_params = params.sort_by { |key, _value| key }
44
- params_strings = sorted_params.map { |key, value| "#{key}#{value}" }
45
- params_strings.join
46
- end
47
-
48
- def generate_files_string(files)
49
- files_array = files_to_array(files).reject(&:nil?)
50
- sorted_files = files_array.sort_by { |file| file[:name] }
51
- files_strings = sorted_files.map { |file| generate_file_string(file) }
52
- files_strings.join
53
- end
54
-
55
- def files_to_array(files)
56
- files.is_a?(Array) ? files : [files]
57
- end
58
-
59
- def generate_file_string(file)
60
- file[:name] + DIGEST.hexdigest(file[:tempfile].read)
7
+ def valid_signature? *args
8
+ Phaxio::Resources::Webhook.valid_signature? *args
9
+ rescue Error::PhaxioError => error
10
+ if error.message == 'No webhook token has been set'
11
+ raise Error::PhaxioError, 'No callback token has been set'
12
+ else
13
+ raise error
14
+ end
61
15
  end
62
16
  end
63
17
  end