Skip to content

Commit 483d9c3

Browse files
committed
Fix for v2.1
1 parent c10fa46 commit 483d9c3

37 files changed

Lines changed: 978 additions & 1071 deletions

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ group :development do
1010
gem 'webmock'
1111
gem 'pry'
1212
gem 'yard'
13+
gem 'dotenv', require: false
1314
end

lib/phaxio.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
require 'base64'
12
require 'json'
23
require 'tempfile'
34
require 'openssl'

lib/phaxio/client.rb

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ def post endpoint, params = {}
114114
params[k] = file_param
115115
end
116116

117-
conn.post endpoint, params
117+
conn.post endpoint, params, api_headers(params)
118118
end
119119

120120
def patch endpoint, params = {}
@@ -131,15 +131,15 @@ def patch endpoint, params = {}
131131
params[k] = file_param
132132
end
133133

134-
conn.patch endpoint, params
134+
conn.patch endpoint, params, api_headers(params)
135135
end
136136

137137
def get endpoint, params = {}
138-
conn.get endpoint, params
138+
conn.get endpoint, params, api_headers(params)
139139
end
140140

141141
def delete endpoint, params = {}
142-
conn.delete endpoint, params
142+
conn.delete endpoint, params, api_headers(params)
143143
end
144144

145145
def api_params params
@@ -152,6 +152,14 @@ def api_params params
152152
params
153153
end
154154

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}"}
161+
end
162+
155163
def file_to_param file
156164
mime_type = MimeTypeHelper.mimetype_for_file file.path
157165
Faraday::UploadIO.new file, mime_type

spec/client_spec.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ def test_response status_code, message: 'This is a test.'
6565
end
6666

6767
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)
7171
test_response 200
7272
end
7373
client.request :get, 'test'
@@ -76,9 +76,9 @@ def test_response status_code, message: 'This is a test.'
7676
it 'uses the api key specified in the params hash' do
7777
custom_api_key = 'custom-api-key'
7878
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)
8282
test_response 200
8383
end
8484
client.request :get, 'test', api_key: custom_api_key, api_secret: custom_api_secret

spec/resources/fax_spec.rb

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
RSpec.describe Fax do
44
let(:test_file) { File.open test_file_path }
55
let(:test_file_path) { File.expand_path(File.join('..', '..', 'support', 'files', 'test.pdf'), __FILE__) }
6-
let(:test_recipient_number) { ENV.fetch 'TEST_RECIPIENT_NUMBER' }
6+
let(:test_recipient_number) { ENV.fetch('TEST_RECIPIENT_NUMBER', '+15558675309') }
77

88
describe 'creating a fax' do
99
let(:action) { Fax.create params }
@@ -29,7 +29,11 @@
2929

3030
describe 'retrieving a fax' do
3131
let(:action) { Fax.get fax_id, params }
32-
let(:fax_id) { 1234 }
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+
}
3337
let(:params) { {} }
3438

3539
around(:each) do |example|
@@ -50,17 +54,14 @@
5054
end
5155
end
5256

53-
# TODO: Refactor this
54-
# This one's a little tricky, since it relies on having a fax to cancel.
5557
describe 'cancelling a fax' do
56-
let(:action) { Fax.cancel @fax_id, params }
58+
let(:action) { Fax.cancel fax_id, params }
5759
let(:params) { {} }
58-
59-
before do
60-
VCR.use_cassette('resources/fax/create') do
61-
@fax_id = Fax.create(to: test_recipient_number, file: test_file).id
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
6263
end
63-
end
64+
}
6465

6566
around(:each) do |example|
6667
VCR.use_cassette('resources/fax/cancel') do
@@ -69,14 +70,14 @@
6970
end
7071

7172
it 'makes the request to Phaxio' do
72-
expect_api_request :post, "faxes/#{@fax_id}/cancel", params
73+
expect_api_request :post, "faxes/#{fax_id}/cancel", params
7374
action
7475
end
7576

7677
it 'returns a reference to the fax' do
7778
result = action
7879
expect(result).to be_a(Fax::Reference)
79-
expect(result.id).to eq(@fax_id)
80+
expect(result.id).to eq(fax_id)
8081
end
8182
end
8283

@@ -104,7 +105,11 @@
104105

105106
describe 'resending a fax' do
106107
let(:action) { Fax.resend fax_id, params }
107-
let(:fax_id) { 1234 }
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
112+
}
108113
let(:params) { {} }
109114

110115
around do |example|
@@ -127,7 +132,11 @@
127132

128133
describe 'deleting a fax' do
129134
let(:action) { Fax.delete fax_id, params }
130-
let(:fax_id) { 1234 }
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
139+
}
131140
let(:params) { {} }
132141

133142
around do |example|
@@ -149,7 +158,11 @@
149158

150159
describe 'deleting a fax file' do
151160
let(:action) { Fax.delete_file fax_id, params }
152-
let(:fax_id) { 1234 }
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
165+
}
153166
let(:params) { {} }
154167

155168
around do |example|
@@ -171,7 +184,11 @@
171184

172185
describe 'downloading a fax file' do
173186
let(:action) { Fax.file fax_id, params }
174-
let(:fax_id) { 1234 }
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
191+
}
175192
let(:params) { {} }
176193

177194
around do |example|
@@ -213,14 +230,18 @@
213230
end
214231

215232
describe Fax::Reference do
216-
let(:fax_id) { 1234 }
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+
}
217238

218239
it 'gets the full fax' do
219-
VCR.use_cassette('resources/fax/get') do
240+
VCR.use_cassette('resources/fax/reference') do
220241
reference = Fax::Reference.new fax_id
221242
result = reference.get
222243
expect(result).to be_a(Fax)
223-
expect(result.id).to eq(1234)
244+
expect(result.id).to eq(fax_id)
224245
end
225246
end
226247
end

spec/resources/phax_code_spec.rb

Lines changed: 52 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,33 @@
55
let(:action) { PhaxCode.create params }
66
let(:params) { {metadata: 'This is a test PhaxCode'} }
77

8-
around do |example|
9-
VCR.use_cassette('resources/phax_code/create') do
10-
example.run
8+
context 'default type' do
9+
around do |example|
10+
VCR.use_cassette('resources/phax_code/create') do
11+
example.run
12+
end
1113
end
12-
end
1314

14-
it 'makes the request to phaxio' do
15-
expect_api_request :post, 'phax_codes', params
16-
action
17-
end
15+
it 'makes the request to phaxio' do
16+
expect_api_request :post, 'phax_codes', params
17+
action
18+
end
1819

19-
it 'returns a PhaxCode instance by default' do
20-
result = action
21-
expect(result).to be_a(PhaxCode)
20+
it 'returns a PhaxCode instance by default' do
21+
result = action
22+
expect(result).to be_a(PhaxCode)
23+
end
2224
end
2325

2426
context 'type is specified to be png' do
2527
let(:params) { {metadata: 'This is a test PhaxCode', type: 'png'} }
2628

29+
around do |example|
30+
VCR.use_cassette('resources/phax_code/create_png') do
31+
example.run
32+
end
33+
end
34+
2735
it 'returns a png if type is specified to be png' do
2836
result = action
2937
expect(result).to be_a(File)
@@ -35,27 +43,35 @@
3543
let(:action) { PhaxCode.get params }
3644
let(:params) { {} }
3745

38-
around do |example|
39-
VCR.use_cassette('resources/phax_code/get') do
40-
example.run
46+
context 'default' do
47+
around do |example|
48+
VCR.use_cassette('resources/phax_code/get') do
49+
example.run
50+
end
4151
end
42-
end
4352

44-
it 'makes the request to Phaxio' do
45-
expect_api_request :get, 'phax_code', params
46-
action
47-
end
53+
it 'makes the request to Phaxio' do
54+
expect_api_request :get, 'phax_code', params
55+
action
56+
end
4857

49-
context 'getting the default phax code with no type specified' do
50-
it 'returns a PhaxCode instance' do
51-
result = action
52-
expect(result).to be_a(PhaxCode)
58+
context 'getting the default phax code with no type specified' do
59+
it 'returns a PhaxCode instance' do
60+
result = action
61+
expect(result).to be_a(PhaxCode)
62+
end
5363
end
5464
end
5565

5666
context 'getting the default phax code with png type specified' do
5767
let(:params) { {type: 'png'} }
5868

69+
around do |example|
70+
VCR.use_cassette('resources/phax_code/get_png') do
71+
example.run
72+
end
73+
end
74+
5975
it 'returns a file' do
6076
result = action
6177
expect(result).to be_a(File)
@@ -65,6 +81,12 @@
6581
context 'getting a particular phax code with no type specified' do
6682
let(:params) { {identifier: '-Y3jxX'} }
6783

84+
around do |example|
85+
VCR.use_cassette('resources/phax_code/get_id') do
86+
example.run
87+
end
88+
end
89+
6890
it 'returns a PhaxCode instance' do
6991
result = action
7092
expect(result).to be_a(PhaxCode)
@@ -74,10 +96,16 @@
7496
context 'getting a particular phax code with png type specified' do
7597
let(:params) { {identifier: '-Y3jxX', type: 'png'} }
7698

99+
around do |example|
100+
VCR.use_cassette('resources/phax_code/get_id_png') do
101+
example.run
102+
end
103+
end
104+
77105
it 'returns a file' do
78106
result = action
79107
expect(result).to be_a(File)
80108
end
81109
end
82110
end
83-
end
111+
end

spec/support/vcr.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
ENV['TEST_RECIPIENT_NUMBER'] ||= '+15558675309'
2-
31
VCR.configure do |config|
42
config.cassette_library_dir = File.expand_path File.join('..', 'vcr_cassettes'), __FILE__
53
config.hook_into :faraday
64
config.filter_sensitive_data('<API_KEY>') { Phaxio.api_key }
75
config.filter_sensitive_data('<API_SECRET>') { Phaxio.api_secret }
86
config.filter_sensitive_data('+15558675309') { ENV['TEST_RECIPIENT_NUMBER'] }
97
end
8+

0 commit comments

Comments
 (0)