From 29ad84dea3b2e2d17ba07764bc2059f2e411560a Mon Sep 17 00:00:00 2001 From: Chris Hunt Date: Tue, 4 Mar 2014 07:49:22 -0800 Subject: [PATCH 1/3] Swap httparty for faraday in gemspec --- github-auth.gemspec | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/github-auth.gemspec b/github-auth.gemspec index 7183b38..af89ea8 100644 --- a/github-auth.gemspec +++ b/github-auth.gemspec @@ -29,6 +29,6 @@ Gem::Specification.new do |spec| spec.add_development_dependency 'sinatra', '~> 1.4.4' spec.add_development_dependency 'thin', '~> 1.6.1' - spec.add_runtime_dependency 'httparty', '~> 0.12.0' - spec.add_runtime_dependency 'thor', '~> 0.18.1' + spec.add_runtime_dependency 'faraday', '~> 0.9.0' + spec.add_runtime_dependency 'thor', '~> 0.18.1' end From 91d67340756355e385cf13027b4a35950ccbe48f Mon Sep 17 00:00:00 2001 From: Chris Hunt Date: Tue, 4 Mar 2014 07:50:56 -0800 Subject: [PATCH 2/3] Use faraday for mock_github_server --- spec/support/mock_github_server.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/spec/support/mock_github_server.rb b/spec/support/mock_github_server.rb index 6cc5e06..957e202 100644 --- a/spec/support/mock_github_server.rb +++ b/spec/support/mock_github_server.rb @@ -1,4 +1,4 @@ -require 'httparty' +require 'faraday' require 'sinatra/base' require 'json' @@ -34,9 +34,9 @@ def with_mock_github_server while true begin - HTTParty.get(hostname) + Faraday.get hostname break - rescue Errno::ECONNREFUSED + rescue Faraday::ConnectionFailed # Do nothing, try again end end From f27ea608566ea7726bb0ef8e219af960ec6f3ef3 Mon Sep 17 00:00:00 2001 From: Chris Hunt Date: Tue, 4 Mar 2014 07:53:01 -0800 Subject: [PATCH 3/3] Use faraday in keys_client --- lib/github/auth/keys_client.rb | 16 ++++++------ spec/unit/github/auth/keys_client_spec.rb | 30 ++++++++++++----------- 2 files changed, 25 insertions(+), 21 deletions(-) diff --git a/lib/github/auth/keys_client.rb b/lib/github/auth/keys_client.rb index 1fd2172..4a9aca3 100644 --- a/lib/github/auth/keys_client.rb +++ b/lib/github/auth/keys_client.rb @@ -1,5 +1,6 @@ require 'cgi' -require 'httparty' +require 'json' +require 'faraday' module Github::Auth # Client for fetching public SSH keys using the Github API @@ -35,17 +36,18 @@ def keys private def github_response - response = http_client.get( + response = http_client.get \ "#{hostname}/users/#{username}/keys", headers: headers - ) - raise GithubUserDoesNotExistError if response.code == 404 - response.parsed_response - rescue SocketError, Errno::ECONNREFUSED => e + + raise GithubUserDoesNotExistError if response.status == 404 + + JSON.parse response.body + rescue Faraday::Error => e raise GithubUnavailableError, e end def http_client - HTTParty + Faraday end def headers diff --git a/spec/unit/github/auth/keys_client_spec.rb b/spec/unit/github/auth/keys_client_spec.rb index 309b52e..d7b7962 100644 --- a/spec/unit/github/auth/keys_client_spec.rb +++ b/spec/unit/github/auth/keys_client_spec.rb @@ -8,11 +8,11 @@ let(:username) { 'chrishunt' } let(:http_client) { double('HttpClient', get: response) } let(:response_code) { 200 } - let(:parsed_response) { nil } + let(:body) { [] } let(:response) { - double('HTTParty::Response', { - code: response_code, - parsed_response: parsed_response + double('Faraday::Response', { + status: response_code, + body: JSON.generate(body) }) } @@ -55,13 +55,13 @@ end context 'when the github user has keys' do - let(:parsed_response) {[ + let(:body) {[ { 'id' => 123, 'key' => 'abc123' }, { 'id' => 456, 'key' => 'def456' } ]} it 'returns the keys' do - expected_keys = parsed_response.map do |entry| + expected_keys = body.map do |entry| Github::Auth::Key.new username, entry.fetch('key') end @@ -70,7 +70,7 @@ end context 'when the github user does not have keys' do - let(:parsed_response) { [] } + let(:body) { [] } it 'returns an empty array' do expect(subject.keys).to eq [] @@ -88,14 +88,16 @@ end context 'when there is an issue connecting to Github' do - [SocketError, Errno::ECONNREFUSED].each do |exception| - before { http_client.stub(:get).and_raise exception } + before do + http_client + .stub(:get) + .and_raise Faraday::Error::ConnectionFailed.new('Oops!') + end - it 'raises a GithubUnavailableError' do - expect { - subject.keys - }.to raise_error Github::Auth::KeysClient::GithubUnavailableError - end + it 'raises a GithubUnavailableError' do + expect { + subject.keys + }.to raise_error Github::Auth::KeysClient::GithubUnavailableError end end end