Skip to content

Commit 7567671

Browse files
committed
Merge pull request #31 from chrishunt/use-faraday
Use Faraday instead of HTTParty
2 parents 65b2e52 + f27ea60 commit 7567671

4 files changed

Lines changed: 30 additions & 26 deletions

File tree

github-auth.gemspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,6 @@ Gem::Specification.new do |spec|
2929
spec.add_development_dependency 'sinatra', '~> 1.4.4'
3030
spec.add_development_dependency 'thin', '~> 1.6.1'
3131

32-
spec.add_runtime_dependency 'httparty', '~> 0.12.0'
33-
spec.add_runtime_dependency 'thor', '~> 0.18.1'
32+
spec.add_runtime_dependency 'faraday', '~> 0.9.0'
33+
spec.add_runtime_dependency 'thor', '~> 0.18.1'
3434
end

lib/github/auth/keys_client.rb

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
require 'cgi'
2-
require 'httparty'
2+
require 'json'
3+
require 'faraday'
34

45
module Github::Auth
56
# Client for fetching public SSH keys using the Github API
@@ -35,17 +36,18 @@ def keys
3536
private
3637

3738
def github_response
38-
response = http_client.get(
39+
response = http_client.get \
3940
"#{hostname}/users/#{username}/keys", headers: headers
40-
)
41-
raise GithubUserDoesNotExistError if response.code == 404
42-
response.parsed_response
43-
rescue SocketError, Errno::ECONNREFUSED => e
41+
42+
raise GithubUserDoesNotExistError if response.status == 404
43+
44+
JSON.parse response.body
45+
rescue Faraday::Error => e
4446
raise GithubUnavailableError, e
4547
end
4648

4749
def http_client
48-
HTTParty
50+
Faraday
4951
end
5052

5153
def headers

spec/support/mock_github_server.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
require 'httparty'
1+
require 'faraday'
22
require 'sinatra/base'
33
require 'json'
44

@@ -34,9 +34,9 @@ def with_mock_github_server
3434

3535
while true
3636
begin
37-
HTTParty.get(hostname)
37+
Faraday.get hostname
3838
break
39-
rescue Errno::ECONNREFUSED
39+
rescue Faraday::ConnectionFailed
4040
# Do nothing, try again
4141
end
4242
end

spec/unit/github/auth/keys_client_spec.rb

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@
88
let(:username) { 'chrishunt' }
99
let(:http_client) { double('HttpClient', get: response) }
1010
let(:response_code) { 200 }
11-
let(:parsed_response) { nil }
11+
let(:body) { [] }
1212
let(:response) {
13-
double('HTTParty::Response', {
14-
code: response_code,
15-
parsed_response: parsed_response
13+
double('Faraday::Response', {
14+
status: response_code,
15+
body: JSON.generate(body)
1616
})
1717
}
1818

@@ -55,13 +55,13 @@
5555
end
5656

5757
context 'when the github user has keys' do
58-
let(:parsed_response) {[
58+
let(:body) {[
5959
{ 'id' => 123, 'key' => 'abc123' },
6060
{ 'id' => 456, 'key' => 'def456' }
6161
]}
6262

6363
it 'returns the keys' do
64-
expected_keys = parsed_response.map do |entry|
64+
expected_keys = body.map do |entry|
6565
Github::Auth::Key.new username, entry.fetch('key')
6666
end
6767

@@ -70,7 +70,7 @@
7070
end
7171

7272
context 'when the github user does not have keys' do
73-
let(:parsed_response) { [] }
73+
let(:body) { [] }
7474

7575
it 'returns an empty array' do
7676
expect(subject.keys).to eq []
@@ -88,14 +88,16 @@
8888
end
8989

9090
context 'when there is an issue connecting to Github' do
91-
[SocketError, Errno::ECONNREFUSED].each do |exception|
92-
before { http_client.stub(:get).and_raise exception }
91+
before do
92+
http_client
93+
.stub(:get)
94+
.and_raise Faraday::Error::ConnectionFailed.new('Oops!')
95+
end
9396

94-
it 'raises a GithubUnavailableError' do
95-
expect {
96-
subject.keys
97-
}.to raise_error Github::Auth::KeysClient::GithubUnavailableError
98-
end
97+
it 'raises a GithubUnavailableError' do
98+
expect {
99+
subject.keys
100+
}.to raise_error Github::Auth::KeysClient::GithubUnavailableError
99101
end
100102
end
101103
end

0 commit comments

Comments
 (0)