Skip to content

Commit bb13556

Browse files
committed
Fix linting issues
1 parent 4bbd710 commit bb13556

File tree

2 files changed

+50
-34
lines changed

2 files changed

+50
-34
lines changed

lib/fcm.rb

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -293,23 +293,25 @@ def jwt_token
293293
token["access_token"]
294294
end
295295

296-
def credentials_error_msg(json_key_path)
297-
param_klass = if @json_key_path.nil?
298-
'nil'
299-
else
300-
"a #{@json_key_path.class.name}"
301-
end
296+
def credentials_error_msg(param)
297+
error_msg = 'credentials must be an IO-like ' \
298+
'object or path You passed '
299+
300+
error_msg += param.class.name.to_s
301+
raise InvalidCredentialError, error_msg
302+
end
302303

303-
"credentials must be an IO-like object or a path. You passed #{param_klass}"
304+
def filename_or_io_like?(path)
305+
(path.is_a?(String) || path.respond_to?(:open)) && File.file?(path)
304306
end
305307

306308
def json_key
307309
@json_key ||= if @json_key_path.respond_to?(:read)
308-
@json_key_path
309-
elsif (@json_key_path.is_a?(String) || @json_key_path.respond_to?(:open)) && File.file?(@json_key_path)
310-
File.open(@json_key_path)
311-
else
312-
raise InvalidCredentialError, credentials_error_msg(@json_key_path)
313-
end
310+
@json_key_path
311+
elsif filename_or_io_like?(@json_key_path)
312+
File.open(@json_key_path)
313+
else
314+
credentials_error_msg(@json_key_path)
315+
end
314316
end
315317
end

spec/fcm_spec.rb

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,33 @@
1313
}
1414
end
1515

16+
let(:client_email) do
17+
'83315528762cf7e0-7bbcc3aad87e0083391bc7f234d487' \
18+
'c8@developer.gserviceaccount.com'
19+
end
20+
21+
let(:client_x509_cert_url) do
22+
'https://www.googleapis.com/robot/v1/metadata/x509/' \
23+
'fd6b61037dd2bb8585527679" + "-7bbcc3aad87e0083391b' \
24+
'c7f234d487c8%40developer.gserviceaccount.com'
25+
end
26+
27+
let(:creds_error) do
28+
FCM::InvalidCredentialError
29+
end
30+
1631
let(:json_credentials) do
1732
{
1833
"type": 'service_account',
1934
"project_id": 'example',
2035
"private_key_id": 'c09c4593eee53707ca9f4208fbd6fe72b29fc7ab',
2136
"private_key": OpenSSL::PKey::RSA.new(2048),
22-
"client_email": '83315528762cf7e0-7bbcc3aad87e0083391bc7f234d487c8@developer.gserviceaccount.com',
23-
"client_id": 'acedc3c0a63b3562376386f0-f3b94aafbecd0e7d60563bf7bb8bb47f.apps.googleusercontent.com',
37+
"client_email": client_email,
38+
"client_id": 'acedc3c0a63b3562376386f0.apps.googleusercontent.com',
2439
"auth_uri": 'https://accounts.google.com/o/oauth2/auth',
2540
"token_uri": 'https://oauth2.googleapis.com/token',
2641
"auth_provider_x509_cert_url": 'https://www.googleapis.com/oauth2/v1/certs',
27-
"client_x509_cert_url": 'https://www.googleapis.com/robot/v1/metadata/x509/fd6b61037dd2bb8585527679-7bbcc3aad87e0083391bc7f234d487c8%40developer.gserviceaccount.com',
42+
"client_x509_cert_url": client_x509_cert_url,
2843
"universe_domain": 'googleapis.com'
2944
}.to_json
3045
end
@@ -42,35 +57,34 @@
4257
end
4358

4459
describe "credentials path" do
45-
it "can be a path to a file" do
60+
it 'can be a path to a file' do
4661
fcm = FCM.new("README.md")
4762
expect(fcm.__send__(:json_key).class).to eq(File)
4863
end
4964

50-
it "can be an IO object" do
51-
fcm = FCM.new(StringIO.new("hey"))
65+
it 'can be an IO object' do
66+
fcm = FCM.new(StringIO.new('hey'))
5267
expect(fcm.__send__(:json_key).class).to eq(StringIO)
5368
end
5469

55-
it "raises an error when passed a non-existent credentials file path" do
56-
fcm = FCM.new('spec/fake_credentials.json', '', {})
57-
expect { fcm.__send__(:json_key).class }.to raise_error(FCM::InvalidCredentialError)
70+
it 'raises an error when passed a non IO-like object' do
71+
[
72+
FCM.new(nil, '', {}),
73+
FCM.new({}, '', {}),
74+
FCM.new(json_credentials, '', {})
75+
].each do |fcm|
76+
expect { fcm.__send__(:json_key) }.to raise_error(creds_error)
77+
end
5878
end
5979

60-
it "raises an error when passed a string of a file that does not exist" do
61-
fcm = FCM.new("fake_credentials.json", '', {})
62-
expect { fcm.__send__(:json_key).class }.to raise_error(FCM::InvalidCredentialError)
80+
it 'raises an error when passed a non-existent credentials file path' do
81+
fcm = FCM.new('spec/fake_credentials.json', '', {})
82+
expect { fcm.__send__(:json_key) }.to raise_error(creds_error)
6383
end
6484

65-
it 'raises an error when passed a non IO-like object' do
66-
fcm_with_non_io_objects = [
67-
fcm_with_nil_creds = FCM.new(nil, '', {}),
68-
fcm_with_hash_creds = FCM.new({}, '', {}),
69-
fcm_with_json = FCM.new(json_credentials, '', {})
70-
]
71-
fcm_with_non_io_objects.each do |fcm_with_non_io_object|
72-
expect { fcm_with_non_io_object.__send__(:json_key).class }.to raise_error(FCM::InvalidCredentialError)
73-
end
85+
it 'raises an error when passed a string of a file that does not exist' do
86+
fcm = FCM.new('fake_credentials.json', '', {})
87+
expect { fcm.__send__(:json_key) }.to raise_error(creds_error)
7488
end
7589
end
7690

0 commit comments

Comments
 (0)