phaxio 2.0.1 → 2.1.0.pre

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. checksums.yaml +5 -5
  2. data/Gemfile +2 -1
  3. data/README.md +46 -13
  4. data/lib/phaxio.rb +2 -1
  5. data/lib/phaxio/client.rb +31 -10
  6. data/lib/phaxio/config.rb +1 -1
  7. data/lib/phaxio/resources/ata.rb +219 -0
  8. data/lib/phaxio/version.rb +1 -1
  9. data/phaxio.gemspec +1 -1
  10. data/spec/client_spec.rb +6 -6
  11. data/spec/resources/fax_spec.rb +40 -19
  12. data/spec/resources/phax_code_spec.rb +52 -24
  13. data/spec/support/vcr.rb +1 -2
  14. data/spec/support/vcr_cassettes/resources/account/status.yml +14 -13
  15. data/spec/support/vcr_cassettes/resources/fax/cancel.yml +16 -15
  16. data/spec/support/vcr_cassettes/resources/fax/create.yml +17 -197
  17. data/spec/support/vcr_cassettes/resources/fax/create_for_cancel.yml +50 -0
  18. data/spec/support/vcr_cassettes/resources/fax/create_for_delete.yml +50 -0
  19. data/spec/support/vcr_cassettes/resources/fax/create_for_delete_file.yml +50 -0
  20. data/spec/support/vcr_cassettes/resources/fax/create_for_download_file.yml +50 -0
  21. data/spec/support/vcr_cassettes/resources/fax/create_for_get.yml +50 -0
  22. data/spec/support/vcr_cassettes/resources/fax/create_for_reference.yml +50 -0
  23. data/spec/support/vcr_cassettes/resources/fax/create_for_resend.yml +50 -0
  24. data/spec/support/vcr_cassettes/resources/fax/delete.yml +11 -10
  25. data/spec/support/vcr_cassettes/resources/fax/delete_file.yml +11 -10
  26. data/spec/support/vcr_cassettes/resources/fax/file.yml +15 -216
  27. data/spec/support/vcr_cassettes/resources/fax/get.yml +14 -13
  28. data/spec/support/vcr_cassettes/resources/fax/list.yml +15 -20
  29. data/spec/support/vcr_cassettes/resources/fax/reference.yml +45 -0
  30. data/spec/support/vcr_cassettes/resources/fax/resend.yml +15 -14
  31. data/spec/support/vcr_cassettes/resources/fax/test_receive.yml +14 -194
  32. data/spec/support/vcr_cassettes/resources/phax_code/create.yml +12 -65
  33. data/spec/support/vcr_cassettes/resources/phax_code/create_png.yml +52 -0
  34. data/spec/support/vcr_cassettes/resources/phax_code/get.yml +13 -158
  35. data/spec/support/vcr_cassettes/resources/phax_code/get_id.yml +46 -0
  36. data/spec/support/vcr_cassettes/resources/phax_code/get_id_png.yml +50 -0
  37. data/spec/support/vcr_cassettes/resources/phax_code/get_png.yml +50 -0
  38. data/spec/support/vcr_cassettes/resources/phone_number/create.yml +16 -15
  39. data/spec/support/vcr_cassettes/resources/phone_number/get.yml +14 -13
  40. data/spec/support/vcr_cassettes/resources/phone_number/list.yml +24 -16
  41. data/spec/support/vcr_cassettes/resources/phone_number/release.yml +13 -12
  42. data/spec/support/vcr_cassettes/resources/public/area_codes/list.yml +23 -21
  43. data/spec/support/vcr_cassettes/resources/public/country/list.yml +15 -14
  44. metadata +26 -8
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 12ce42728923c83667571c73e2c0320f41a3f579
4
- data.tar.gz: f6840ec8c126853273bd76f68d3399b3f5a24989
2
+ SHA256:
3
+ metadata.gz: 7f20cd3d8695312c8b64cf40cc01b8fe0e320b921a3527abc1ca005a9d140b11
4
+ data.tar.gz: '088662055ff2bd6ebbeb5055ad7316bd6d99acf9a93249383d9ad1aa08133a4b'
5
5
  SHA512:
6
- metadata.gz: 3f805c26607a691b2af4e692cd5c8c6cf4b24a624b711527f72e85ee49287071369087e261d41eb292a7a9747cf32e26cd7ac67f3b15018d906cf1c9a8a4ceeb
7
- data.tar.gz: a1bc1b11cfa3244f1361a526c0439bfc9dab5c82efe45e80ee888c5d01920af8e190dc5336b232980d0c57ee3162f3ffa6c5220ecba65b5fc98c577a92275526
6
+ metadata.gz: 99cfc91fefd07016507eb20049f5c7afeca903d2c227941bcd55a1cd14913faa1b03e69a2ceea1798da457bf338a48e5ad7c212f107aba598378965210a0d24a
7
+ data.tar.gz: cf3b858ff97a2b005f50a21709ee524fd20b6fc12080a59a48d57842b9e295f18de91a20f48ba5f9f1b64d632e5a38d7d375b0af8e323cea2232df49421deb93
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
@@ -261,27 +261,60 @@ Callback.valid_signature? received_signature, callback_url, received_params, rec
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
270
-
271
- post '/phaxio_callback' do
264
+ class PhaxioWebhookExample < Sinatra::Base
265
+ Phaxio.callback_token = 'YOUR WEBHOOK TOKEN HERE'
266
+
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::Callback.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.callback_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::Callback.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 callback signature"
307
+ render plain: 'Invalid callback 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.rb CHANGED
@@ -1,3 +1,4 @@
1
+ require 'base64'
1
2
  require 'json'
2
3
  require 'tempfile'
3
4
  require 'openssl'
@@ -16,7 +17,7 @@ Dir[File.expand_path(File.join('..', 'phaxio', 'helpers', '*.rb'), __FILE__)].ea
16
17
  require file
17
18
  end
18
19
 
19
- %w[fax_recipient fax account callback phax_code phone_number public].each do |filename|
20
+ %w[fax_recipient fax account callback phax_code phone_number public ata].each do |filename|
20
21
  require File.expand_path(File.join('..', 'phaxio', 'resources', filename), __FILE__)
21
22
  end
22
23
 
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."
@@ -43,6 +44,10 @@ module Phaxio
43
44
  conn.request :multipart
44
45
  conn.request :url_encoded
45
46
  conn.adapter :net_http
47
+
48
+ if Phaxio.api_key && Phaxio.api_secret
49
+ conn.basic_auth Phaxio.api_key, Phaxio.api_secret
50
+ end
46
51
  end
47
52
  end
48
53
 
@@ -109,20 +114,35 @@ module Phaxio
109
114
  params[k] = file_param
110
115
  end
111
116
 
112
- conn.post endpoint, params
117
+ conn.post endpoint, params, api_headers(params)
118
+ end
119
+
120
+ def patch endpoint, params = {}
121
+ # Handle file params
122
+ params.each do |k, v|
123
+ next unless k.to_s == 'file'
124
+
125
+ if v.is_a? Array
126
+ file_param = v.map { |file| file_to_param file }
127
+ else
128
+ file_param = file_to_param v
129
+ end
130
+
131
+ params[k] = file_param
132
+ end
133
+
134
+ conn.patch endpoint, params, api_headers(params)
113
135
  end
114
136
 
115
137
  def get endpoint, params = {}
116
- conn.get endpoint, params
138
+ conn.get endpoint, params, api_headers(params)
117
139
  end
118
140
 
119
141
  def delete endpoint, params = {}
120
- conn.delete endpoint, params
142
+ conn.delete endpoint, params, api_headers(params)
121
143
  end
122
144
 
123
145
  def api_params params
124
- params = default_params.merge params
125
-
126
146
  # Convert times to ISO 8601
127
147
  params.each do |k, v|
128
148
  next unless v.kind_of?(Time) || v.kind_of?(Date)
@@ -132,11 +152,12 @@ module Phaxio
132
152
  params
133
153
  end
134
154
 
135
- def default_params
136
- {
137
- api_key: Phaxio.api_key,
138
- api_secret: Phaxio.api_secret
139
- }
155
+ def api_headers params
156
+ api_key = params[:api_key] || Phaxio.api_key
157
+ api_secret = params[:api_secret] || Phaxio.api_secret
158
+ return unless api_key && api_secret
159
+ auth = Base64.strict_encode64("#{api_key}:#{api_secret}")
160
+ {'Authorization' => "Basic #{auth}"}
140
161
  end
141
162
 
142
163
  def file_to_param file
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.
@@ -0,0 +1,219 @@
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 username for the ATA.
24
+ # @!attribute uername
25
+
26
+ # @return [String] The password for the ATA.
27
+ # @!attribute password
28
+
29
+ has_normal_attributes %w[
30
+ id name description user_phone_number domain username password
31
+ ]
32
+
33
+ # A reference to an ATA. This is returned by certain actions which don't
34
+ # return the full ATA.
35
+ class Reference
36
+ # @return [Integer]
37
+ # The ID of the referenced ATA.
38
+ attr_accessor :id
39
+
40
+ def to_i
41
+ id
42
+ end
43
+
44
+ private
45
+
46
+ def initialize id
47
+ self.id = id
48
+ end
49
+ end
50
+
51
+ # A reference to a phone number, returned by ATA phone number management
52
+ # actions.
53
+ class PhoneNumberReference
54
+ # @return [String]
55
+ # The phone number.
56
+ attr_accessor :phone_number
57
+
58
+ def to_s
59
+ phone_number
60
+ end
61
+
62
+ private
63
+
64
+ def initialize phone_number
65
+ self.phone_number = phone_number
66
+ end
67
+ end
68
+
69
+ class << self
70
+ # @macro paging
71
+ # List ATAs
72
+ # @param params[Hash]
73
+ # Any parameters to send to Phaxio.
74
+ # @return [Phaxio::Resource::Collection<Phaxio::Resources::Ata>]
75
+ # The collection of ATAs matching your request.
76
+ # @raise [Phaxio::Error::PhaxioError]
77
+ # @see https://www.phaxio.com/docs/api/v2.1/atas/list
78
+ def list params = {}
79
+ response = Client.request :get, atas_endpoint, params
80
+ response_collection response
81
+ end
82
+
83
+ # Create an ATA
84
+ # @param params [Hash]
85
+ # Any parameters to send to Phaxio.
86
+ # - *name* [String] - A name used to identify the ATA.
87
+ # - *description* [String] - A longer description of the ATA.
88
+ # - *domain* [String] - A domain for the ATA.
89
+ # @return [Phaxio::Resources::Ata]
90
+ # The created ATA, including the generated username and password.
91
+ # @raise [Phaxio::Error::PhaxioError]
92
+ # @see https://www.phaxio.com/docs/api/v2.1/atas/create
93
+ def create params = {}
94
+ response = Client.request :post, atas_endpoint, params
95
+ response_record response
96
+ end
97
+
98
+ # Get an ATA
99
+ # @param id [Integer]
100
+ # The ID of the ATA to retrieve information about.
101
+ # @param params [Hash]
102
+ # Any parameters to send to Phaxio. This action takes no unique parameters.
103
+ # @return [Phaxio::Resources::Ata]
104
+ # The requested ATA.
105
+ # @raise [Phaxio::Error::PhaxioError]
106
+ # @see https://www.phaxio.com/docs/api/v2.1/atas/get
107
+ def get id, params = {}
108
+ response = Client.request :get, ata_endpoint(id.to_i), params
109
+ response_record response
110
+ end
111
+ alias :retrieve :get
112
+ alias :find :get
113
+
114
+ # Update an ATA
115
+ # @param id [Integer]
116
+ # The ID of the ATA to update.
117
+ # @param params [Hash]
118
+ # Any parameters to send to Phaxio.
119
+ # - *name* [String] - A name used to identify the ATA.
120
+ # - *description* [String] - A longer description of the ATA.
121
+ # - *domain* [String] - A domain for the ATA.
122
+ # @return [Phaxio::Resources::Ata]
123
+ # The updated ATA.
124
+ # @raise [Phaxio::Error::PhaxioError]
125
+ # @see https://www.phaxio.com/docs/api/v2.1/atas/update
126
+ def update id, params = {}
127
+ response = Client.request :patch, ata_endpoint(id.to_i), params
128
+ response_record response
129
+ end
130
+
131
+ # Regenerate credentials for an ATA
132
+ # @param id [Integer]
133
+ # The ID of the ATA for which credentials should be regenerated.
134
+ # @param params [Hash]
135
+ # Any parameters to send to Phaxio. This action takes no unique parameters.
136
+ # @return [Phaxio::Resources::Ata]
137
+ # The ATA, including the new username and password.
138
+ # @raise Phaxio::Error::PhaxioError
139
+ # @see https://www.phaxio.com/docs/api/v2.1/atas/regenerate_credentials
140
+ def regenerate_credentials id, params = {}
141
+ response = Client.request :patch, regenerate_credentials_endpoint(id.to_i), params
142
+ response_record response
143
+ end
144
+
145
+ # Delete an ATA
146
+ # @param id [Integer]
147
+ # The Id of the ATA to delete.
148
+ # @param params [Hash]
149
+ # Any parameters to send to Phaxio. This action takes no unique parameters.
150
+ # @return [Phaxio::Resources::Ata::Reference]
151
+ # A reference to the deleted ATA.
152
+ # @raise [Phaxio::Error::PhaxioError]
153
+ # @see https://www.phaxio.com/docs/api/v2.1/atas/delete
154
+ def delete id, params = {}
155
+ response = Client.request :delete, ata_endpoint(id.to_i), params
156
+ response_reference response
157
+ end
158
+
159
+ # Add a phone number
160
+ # @param id [Integer]
161
+ # The ID of the ATA to which you want to add a number.
162
+ # @param phone_number [String]
163
+ # The phone number to add to the ATA.
164
+ # @param params [Hash]
165
+ # Any parameters to send to Phaxio. This action takes no unique parameters.
166
+ # @return [Phaxio::Resources::Ata::PhoneNumberReference]
167
+ # A reference to the added phone number.
168
+ # @raise [Phaxio::Error::PhaxioError]
169
+ # @see https://www.phaxio.com/docs/api/v2.1/atas/add_phone_number
170
+ def add_phone_number id, phone_number, params = {}
171
+ response = Client.request :post, phone_number_endpoint(id, phone_number), params
172
+ response_phone_number_reference response
173
+ end
174
+
175
+ # Remove a phone number
176
+ # @param id [Integer]
177
+ # The ID of the ATA from which you want to remove the phone number.
178
+ # @param phone_number [String]
179
+ # The phone number you want to remove.
180
+ # @param params [Hash]
181
+ # Any parameters to send to Phaxio. This action takes no unique parameters.
182
+ # @return [Phaxio::Resources::Ata::PhoneNumberReference]
183
+ # A reference to the removed phone number.
184
+ # @raise [Phaxio::Error::PhaxioError]
185
+ # @see https://www.phaxio.com/docs/api/v2.1/atas/remove_phone_number
186
+ def remove_phone_number id, phone_number, params = {}
187
+ response = Client.request :delete, phone_number_endpoint(id, phone_number), params
188
+ response_phone_number_reference response
189
+ end
190
+
191
+ private
192
+
193
+ def response_reference response
194
+ Reference.new Integer(response['id'])
195
+ end
196
+
197
+ def response_phone_number_reference response
198
+ PhoneNumberReference.new(response['phone_number'])
199
+ end
200
+
201
+ def atas_endpoint
202
+ ATAS_PATH
203
+ end
204
+
205
+ def ata_endpoint id
206
+ "#{atas_endpoint}/#{id}"
207
+ end
208
+
209
+ def regenerate_credentials_endpoint id
210
+ "#{ata_endpoint(id)}/regenerate_credentials"
211
+ end
212
+
213
+ def phone_number_endpoint id, phone_number
214
+ "#{ata_endpoint(id)}/phone_numbers/#{phone_number}"
215
+ end
216
+ end
217
+ end
218
+ end
219
+ end
@@ -1,3 +1,3 @@
1
1
  module Phaxio
2
- VERSION = "2.0.1"
2
+ VERSION = "2.1.0.pre"
3
3
  end
data/phaxio.gemspec CHANGED
@@ -17,7 +17,7 @@ Gem::Specification.new do |gem|
17
17
  gem.licenses = ['MIT']
18
18
 
19
19
  gem.required_ruby_version = '>= 2.0'
20
- gem.add_dependency 'faraday', '~> 0.10'
20
+ gem.add_dependency 'faraday', '>= 0.10', '< 2.0'
21
21
  gem.add_dependency 'mime-types', '~> 3.0'
22
22
  gem.add_dependency 'activesupport'
23
23
  end
data/spec/client_spec.rb CHANGED
@@ -65,9 +65,9 @@ RSpec.describe Phaxio::Client do
65
65
  end
66
66
 
67
67
  it 'uses the configured API key and secret by default' do
68
- expect(test_connection).to receive(:get) do |_endpoint, request_params|
69
- expect(request_params[:api_key]).to eq(Phaxio.api_key)
70
- expect(request_params[:api_secret]).to eq(Phaxio.api_secret)
68
+ expect(test_connection).to receive(:get) do |_endpoint, _request_params, request_headers|
69
+ auth_header = "Basic #{Base64.strict_encode64("#{Phaxio.api_key}:#{Phaxio.api_secret}")}"
70
+ expect(request_headers['Authorization']).to eq(auth_header)
71
71
  test_response 200
72
72
  end
73
73
  client.request :get, 'test'
@@ -76,9 +76,9 @@ RSpec.describe Phaxio::Client do
76
76
  it 'uses the api key specified in the params hash' do
77
77
  custom_api_key = 'custom-api-key'
78
78
  custom_api_secret = 'custom-api-secret'
79
- expect(test_connection).to receive(:get) do |_endpoint, request_params|
80
- expect(request_params[:api_key]).to eq(custom_api_key)
81
- expect(request_params[:api_secret]).to eq(custom_api_secret)
79
+ expect(test_connection).to receive(:get) do |_endpoint, _request_params, request_headers|
80
+ auth_header = "Basic #{Base64.strict_encode64('custom-api-key:custom-api-secret')}"
81
+ expect(request_headers['Authorization']).to eq(auth_header)
82
82
  test_response 200
83
83
  end
84
84
  client.request :get, 'test', api_key: custom_api_key, api_secret: custom_api_secret