From 326ac91e2afbdc98db6e41380ee247951f7228db Mon Sep 17 00:00:00 2001 From: John Crepezzi Date: Sat, 16 Nov 2013 11:06:21 -0500 Subject: [PATCH] 1.8.7 support Closes #19 --- .travis.yml | 1 + haste.gemspec | 2 +- lib/haste.rb | 3 --- lib/haste/uploader.rb | 25 ++++++++++++++++++++----- spec/examples/uploader_spec.rb | 17 +++++++++-------- 5 files changed, 31 insertions(+), 17 deletions(-) diff --git a/.travis.yml b/.travis.yml index 20d1d90..7a7d049 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,6 +3,7 @@ branches: only: - master rvm: + - 1.8.7 - 1.9.2 - 1.9.3 - 2.0.0 diff --git a/haste.gemspec b/haste.gemspec index 79c5bca..e19c736 100644 --- a/haste.gemspec +++ b/haste.gemspec @@ -6,7 +6,7 @@ spec = Gem::Specification.new do |s| s.author = 'John Crepezzi' s.add_development_dependency('rspec') s.add_dependency('json') - s.add_dependency('rest-client') + s.add_dependency('faraday') s.description = 'CLI Haste Client' s.email = 'john.crepezzi@gmail.com' s.executables = 'haste' diff --git a/lib/haste.rb b/lib/haste.rb index 1da7add..592ac0b 100644 --- a/lib/haste.rb +++ b/lib/haste.rb @@ -1,7 +1,4 @@ require 'bundler/setup' -require 'json' -require 'uri' -require 'rest-client' require File.dirname(__FILE__) + '/haste/uploader' require File.dirname(__FILE__) + '/haste/exception' require File.dirname(__FILE__) + '/haste/cli' diff --git a/lib/haste/uploader.rb b/lib/haste/uploader.rb index 5787213..0c857c7 100644 --- a/lib/haste/uploader.rb +++ b/lib/haste/uploader.rb @@ -1,3 +1,6 @@ +require 'json' +require 'faraday' + module Haste DEFAULT_URL = 'http://hastebin.com' @@ -22,19 +25,31 @@ module Haste # Take in data and return a key def upload_raw(data) data.rstrip! - raw_data = RestClient.post "#{self.server_url}/documents", data - data = JSON.parse(raw_data) - data['key'] + response = do_post data + if response.status == 200 + data = JSON.parse(response.body) + data['key'] + else + fail_with "failure uploading: #{response.body}" + end rescue JSON::ParserError => e fail_with "failure parsing response: #{e.message}" - rescue RestClient::Exception => e - fail_with "failure uploading: #{e.message}" rescue Errno::ECONNREFUSED => e fail_with "failure connecting: #{e.message}" end private + def do_post(data) + connection.post('/documents', data) + end + + def connection + @connection ||= Faraday.new(:url => server_url) do |c| + c.adapter Faraday.default_adapter + end + end + def fail_with(msg) raise Exception.new(msg) end diff --git a/spec/examples/uploader_spec.rb b/spec/examples/uploader_spec.rb index 09b963c..7cfc138 100644 --- a/spec/examples/uploader_spec.rb +++ b/spec/examples/uploader_spec.rb @@ -2,12 +2,11 @@ require 'spec_helper' describe Haste::Uploader do - let(:uploader) { base.nil? ? Haste::Uploader.new : Haste::Uploader.new(base) } + let(:uploader) { Haste::Uploader.new(base) } describe :upload_raw do let(:data) { 'hello world' } - let(:url) { "#{uploader.server_url}/documents" } let(:base) { nil } let(:error_message) do begin @@ -23,7 +22,8 @@ describe Haste::Uploader do let(:json) { '{"key":"hello"}' } before do - RestClient.should_receive(:post).with(url, data).and_return(json) + ostruct = OpenStruct.new(:status => 200, :body => json) + uploader.send(:connection).should_receive(:post).with('/documents', data).and_return(ostruct) end it 'should get the key' do @@ -38,7 +38,8 @@ describe Haste::Uploader do let(:json) { '{that:not_even_json}' } before do - RestClient.should_receive(:post).with(url, data).and_return(json) + ostruct = OpenStruct.new(:status => 200, :body => json) + uploader.send(:connection).should_receive(:post).with('/documents', data).and_return(ostruct) end it 'should get an error' do @@ -50,12 +51,12 @@ describe Haste::Uploader do context 'with a 404 response' do before do - error = RestClient::ResourceNotFound.new - RestClient.should_receive(:post).with(url, data).and_raise(error) + ostruct = OpenStruct.new(:status => 404, :body => 'ohno') + uploader.send(:connection).should_receive(:post).with('/documents', data).and_return(ostruct) end it 'should get an error' do - error_message.should == 'failure uploading: Resource Not Found' + error_message.should == 'failure uploading: ohno' end end @@ -64,7 +65,7 @@ describe Haste::Uploader do before do error = Errno::ECONNREFUSED - RestClient.should_receive(:post).with(url, data).and_raise(error) + uploader.send(:connection).should_receive(:post).with('/documents', data).and_raise(error) end it 'should get the key' do