Skip to content

Commit fc0c4a1

Browse files
committed
Porting resources and cleanup
1 parent ec25c4b commit fc0c4a1

62 files changed

Lines changed: 1411 additions & 1104 deletions

Some content is hidden

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

lib/phaxio.rb

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

2020
%w[
21-
fax_recipient fax account webhook 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__)
@@ -34,9 +34,11 @@ class << self
3434
# @see Config.api_secret
3535
# @!attribute webhook_token
3636
# @see Config.webhook_token
37+
# @!attribute callback_token
38+
# @see Config.webhook_token
3739
# @!attribute api_endpoint
3840
# @see Config.api_endpoint
39-
%w(api_key api_secret webhook_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

lib/phaxio/config.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ class << self
1818
#
1919
# To find your webhook token, visit https://console.phaxio.com/user/callbacks/edit
2020
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/callback.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module Phaxio
2+
module Resources
3+
# This class is provided for the sake of backwards compatibility; use the Webhook resource instead.
4+
# @see Phaxio::Resources::Webhook
5+
class Callback
6+
class << self
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
15+
end
16+
end
17+
end
18+
end
19+
end

lib/phaxio/resources/port_number_note.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def list port_number_id, params = {}
3939
response = Client.request :get, port_number_notes_endpoint(port_number_id.to_i), params
4040
response_collection response
4141
end
42-
42+
4343
private
4444

4545
def port_number_notes_endpoint port_number_id

lib/phaxio/resources/webhook.rb

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module Phaxio
22
module Resources
3-
# Provides utilities for working with callbacks.
3+
# Provides utilities for working with webhooks.
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
66
class Webhook
@@ -61,8 +61,5 @@ def generate_file_string(file)
6161
end
6262
end
6363
end
64-
65-
# for backwards compatibility
66-
Callback = Webhook
6764
end
6865
end

lib/phaxio/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module Phaxio
2-
VERSION = "2.1.0.pre"
2+
VERSION = "2.1.0"
33
end

spec/phaxio_spec.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,10 @@
1717
subject.webhook_token = 'test-webhook-token'
1818
expect(subject.webhook_token).to eq('test-webhook-token')
1919
end
20+
21+
it 'sets and gets the webhook token as callback_token' do
22+
subject.callback_token = 'test-callback-token'
23+
expect(subject.callback_token).to eq('test-callback-token')
24+
expect(subject.webhook_token).to eq('test-callback-token')
25+
end
2026
end

spec/resources/account_spec.rb

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
11
require 'spec_helper'
22

33
RSpec.describe Account do
4-
describe 'getting account information' do
4+
describe 'getting account information', vcr: 'account/get' do
55
let(:action) { Account.get params }
66
let(:params) { {} }
77

8-
around do |example|
9-
VCR.use_cassette('resources/account/status') do
10-
example.run
11-
end
12-
end
13-
148
it 'sends the request to Phaxio' do
159
expect_api_request :get, 'account/status', params
1610
action

spec/resources/fax_spec.rb

Lines changed: 35 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,13 @@
22

33
RSpec.describe Fax do
44
let(:test_file) { File.open test_file_path }
5-
let(:test_file_path) { File.expand_path(File.join('..', '..', 'support', 'files', 'test.pdf'), __FILE__) }
6-
let(:test_recipient_number) { ENV.fetch('TEST_RECIPIENT_NUMBER', '+15558675309') }
5+
let(:test_file_path) { File.join File.expand_path('..', __dir__), 'support', 'files', 'test.pdf' }
6+
let(:test_recipient_number) { TEST_NUMBER }
77

8-
describe 'creating a fax' do
8+
describe 'creating a fax', vcr: 'fax/create' do
99
let(:action) { Fax.create params }
1010
let(:params) { {to: test_recipient_number, file: test_file} }
1111

12-
around(:each) do |example|
13-
VCR.use_cassette('resources/fax/create') do
14-
example.run
15-
end
16-
end
17-
1812
it 'makes the request to Phaxio' do
1913
expect_api_request :post, 'faxes', to: test_recipient_number, file: test_file
2014
action
@@ -27,21 +21,11 @@
2721
end
2822
end
2923

30-
describe 'retrieving a fax' do
24+
describe 'retrieving a fax', vcr: 'fax/get' do
3125
let(:action) { Fax.get fax_id, params }
32-
let!(:fax_id) {
33-
VCR.use_cassette('resources/fax/create_for_get') do
34-
Phaxio::Fax.create(to: test_recipient_number, file: test_file).id
35-
end
36-
}
26+
let(:fax_id) { Fax.create(to: test_recipient_number, file: test_file).id }
3727
let(:params) { {} }
3828

39-
around(:each) do |example|
40-
VCR.use_cassette('resources/fax/get') do
41-
example.run
42-
end
43-
end
44-
4529
it 'makes the request to Phaxio' do
4630
expect_api_request :get, "faxes/#{fax_id}", params
4731
action
@@ -54,20 +38,10 @@
5438
end
5539
end
5640

57-
describe 'cancelling a fax' do
41+
describe 'cancelling a fax', vcr: 'fax/cancel' do
5842
let(:action) { Fax.cancel fax_id, params }
43+
let(:fax_id) { Fax.create(to: test_recipient_number, file: test_file).id }
5944
let(:params) { {} }
60-
let!(:fax_id) {
61-
VCR.use_cassette('resources/fax/create_for_cancel') do
62-
Phaxio::Fax.create(to: test_recipient_number, file: test_file).id
63-
end
64-
}
65-
66-
around(:each) do |example|
67-
VCR.use_cassette('resources/fax/cancel') do
68-
example.run
69-
end
70-
end
7145

7246
it 'makes the request to Phaxio' do
7347
expect_api_request :post, "faxes/#{fax_id}/cancel", params
@@ -81,17 +55,11 @@
8155
end
8256
end
8357

84-
describe 'listing faxes' do
58+
describe 'listing faxes', vcr: 'fax/list' do
8559
let(:action) { Fax.list params }
8660
let(:params) { {created_before: time} }
8761
let(:time) { Time.new 2017, 10, 28, 0, 17, 0, 0 }
8862

89-
around do |example|
90-
VCR.use_cassette('resources/fax/list') do
91-
example.run
92-
end
93-
end
94-
9563
it 'sends the request to phaxio' do
9664
expect_api_request :get, 'faxes', params
9765
action
@@ -103,21 +71,15 @@
10371
end
10472
end
10573

106-
describe 'resending a fax' do
74+
describe 'resending a fax', vcr: 'fax/resend' do
10775
let(:action) { Fax.resend fax_id, params }
108-
let!(:fax_id) {
109-
VCR.use_cassette('resources/fax/create_for_resend') do
110-
Phaxio::Fax.create(to: test_recipient_number, file: test_file).id
111-
end
76+
let(:fax_id) {
77+
fax_id = Fax.create(to: test_recipient_number, file: test_file).id
78+
sleep 30 if VCR.current_cassette.recording?
79+
fax_id
11280
}
11381
let(:params) { {} }
11482

115-
around do |example|
116-
VCR.use_cassette('resources/fax/resend') do
117-
example.run
118-
end
119-
end
120-
12183
it 'makes the request to Phaxio' do
12284
expect_api_request :post, "faxes/#{fax_id}/resend", params
12385
action
@@ -130,21 +92,15 @@
13092
end
13193
end
13294

133-
describe 'deleting a fax' do
95+
describe 'deleting a fax', vcr: 'fax/delete' do
13496
let(:action) { Fax.delete fax_id, params }
135-
let!(:fax_id) {
136-
VCR.use_cassette('resources/fax/create_for_delete') do
137-
Phaxio::Fax.create(to: test_recipient_number, file: test_file).id
138-
end
97+
let(:fax_id) {
98+
fax_id = Fax.create(to: test_recipient_number, file: test_file).id
99+
sleep 30 if VCR.current_cassette.recording?
100+
fax_id
139101
}
140102
let(:params) { {} }
141103

142-
around do |example|
143-
VCR.use_cassette('resources/fax/delete') do
144-
example.run
145-
end
146-
end
147-
148104
it 'makes the request to Phaxio' do
149105
expect_api_request :delete, "faxes/#{fax_id}", params
150106
action
@@ -156,21 +112,15 @@
156112
end
157113
end
158114

159-
describe 'deleting a fax file' do
115+
describe 'deleting a fax file', vcr: 'fax/delete_file' do
160116
let(:action) { Fax.delete_file fax_id, params }
161-
let!(:fax_id) {
162-
VCR.use_cassette('resources/fax/create_for_delete_file') do
163-
Phaxio::Fax.create(to: test_recipient_number, file: test_file).id
164-
end
117+
let(:fax_id) {
118+
fax_id = Fax.create(to: test_recipient_number, file: test_file).id
119+
sleep 30 if VCR.current_cassette.recording?
120+
fax_id
165121
}
166122
let(:params) { {} }
167123

168-
around do |example|
169-
VCR.use_cassette('resources/fax/delete_file') do
170-
example.run
171-
end
172-
end
173-
174124
it 'makes the request to Phaxio' do
175125
expect_api_request :delete, "faxes/#{fax_id}/file", params
176126
action
@@ -182,21 +132,15 @@
182132
end
183133
end
184134

185-
describe 'downloading a fax file' do
135+
describe 'downloading a fax file', vcr: 'fax/file' do
186136
let(:action) { Fax.file fax_id, params }
187-
let!(:fax_id) {
188-
VCR.use_cassette('resources/fax/create_for_download_file') do
189-
Phaxio::Fax.create(to: test_recipient_number, file: test_file).id
190-
end
137+
let(:fax_id) {
138+
fax_id = Fax.create(to: test_recipient_number, file: test_file).id
139+
sleep 30 if VCR.current_cassette.recording?
140+
fax_id
191141
}
192142
let(:params) { {} }
193143

194-
around do |example|
195-
VCR.use_cassette('resources/fax/file') do
196-
example.run
197-
end
198-
end
199-
200144
it 'makes the request to phaxio' do
201145
expect_api_request :get, "faxes/#{fax_id}/file", params
202146
action
@@ -208,16 +152,10 @@
208152
end
209153
end
210154

211-
describe 'receiving a test fax' do
155+
describe 'receiving a test fax', vcr: 'fax/test_receive' do
212156
let(:action) { Fax.test_receive params }
213157
let(:params) { {file: test_file} }
214158

215-
around do |example|
216-
VCR.use_cassette('resources/fax/test_receive') do
217-
example.run
218-
end
219-
end
220-
221159
it 'makes the request to Phaxio' do
222160
expect_api_request :post, 'faxes', {file: test_file, direction: 'received'}
223161
action
@@ -229,20 +167,14 @@
229167
end
230168
end
231169

232-
describe Fax::Reference do
233-
let!(:fax_id) {
234-
VCR.use_cassette('resources/fax/create_for_reference') do
235-
Phaxio::Fax.create(to: test_recipient_number, file: test_file).id
236-
end
237-
}
170+
describe Fax::Reference, vcr: 'fax/reference' do
171+
let(:fax_id) { Fax.create(to: test_recipient_number, file: test_file).id }
238172

239173
it 'gets the full fax' do
240-
VCR.use_cassette('resources/fax/reference') do
241-
reference = Fax::Reference.new fax_id
242-
result = reference.get
243-
expect(result).to be_a(Fax)
244-
expect(result.id).to eq(fax_id)
245-
end
174+
reference = Fax::Reference.new fax_id
175+
result = reference.get
176+
expect(result).to be_a(Fax)
177+
expect(result.id).to eq(fax_id)
246178
end
247179
end
248180
end

0 commit comments

Comments
 (0)