1
0
mirror of https://github.com/seejohnrun/haste-client.git synced 2025-12-13 07:15:55 +00:00

1.8.7 support

Closes #19
This commit is contained in:
John Crepezzi
2013-11-16 11:06:21 -05:00
parent b153c49aa1
commit 326ac91e2a
5 changed files with 31 additions and 17 deletions

View File

@@ -3,6 +3,7 @@ branches:
only: only:
- master - master
rvm: rvm:
- 1.8.7
- 1.9.2 - 1.9.2
- 1.9.3 - 1.9.3
- 2.0.0 - 2.0.0

View File

@@ -6,7 +6,7 @@ spec = Gem::Specification.new do |s|
s.author = 'John Crepezzi' s.author = 'John Crepezzi'
s.add_development_dependency('rspec') s.add_development_dependency('rspec')
s.add_dependency('json') s.add_dependency('json')
s.add_dependency('rest-client') s.add_dependency('faraday')
s.description = 'CLI Haste Client' s.description = 'CLI Haste Client'
s.email = 'john.crepezzi@gmail.com' s.email = 'john.crepezzi@gmail.com'
s.executables = 'haste' s.executables = 'haste'

View File

@@ -1,7 +1,4 @@
require 'bundler/setup' require 'bundler/setup'
require 'json'
require 'uri'
require 'rest-client'
require File.dirname(__FILE__) + '/haste/uploader' require File.dirname(__FILE__) + '/haste/uploader'
require File.dirname(__FILE__) + '/haste/exception' require File.dirname(__FILE__) + '/haste/exception'
require File.dirname(__FILE__) + '/haste/cli' require File.dirname(__FILE__) + '/haste/cli'

View File

@@ -1,3 +1,6 @@
require 'json'
require 'faraday'
module Haste module Haste
DEFAULT_URL = 'http://hastebin.com' DEFAULT_URL = 'http://hastebin.com'
@@ -22,19 +25,31 @@ module Haste
# Take in data and return a key # Take in data and return a key
def upload_raw(data) def upload_raw(data)
data.rstrip! data.rstrip!
raw_data = RestClient.post "#{self.server_url}/documents", data response = do_post data
data = JSON.parse(raw_data) if response.status == 200
data['key'] data = JSON.parse(response.body)
data['key']
else
fail_with "failure uploading: #{response.body}"
end
rescue JSON::ParserError => e rescue JSON::ParserError => e
fail_with "failure parsing response: #{e.message}" fail_with "failure parsing response: #{e.message}"
rescue RestClient::Exception => e
fail_with "failure uploading: #{e.message}"
rescue Errno::ECONNREFUSED => e rescue Errno::ECONNREFUSED => e
fail_with "failure connecting: #{e.message}" fail_with "failure connecting: #{e.message}"
end end
private 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) def fail_with(msg)
raise Exception.new(msg) raise Exception.new(msg)
end end

View File

@@ -2,12 +2,11 @@ require 'spec_helper'
describe Haste::Uploader do 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 describe :upload_raw do
let(:data) { 'hello world' } let(:data) { 'hello world' }
let(:url) { "#{uploader.server_url}/documents" }
let(:base) { nil } let(:base) { nil }
let(:error_message) do let(:error_message) do
begin begin
@@ -23,7 +22,8 @@ describe Haste::Uploader do
let(:json) { '{"key":"hello"}' } let(:json) { '{"key":"hello"}' }
before do 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 end
it 'should get the key' do it 'should get the key' do
@@ -38,7 +38,8 @@ describe Haste::Uploader do
let(:json) { '{that:not_even_json}' } let(:json) { '{that:not_even_json}' }
before do 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 end
it 'should get an error' do it 'should get an error' do
@@ -50,12 +51,12 @@ describe Haste::Uploader do
context 'with a 404 response' do context 'with a 404 response' do
before do before do
error = RestClient::ResourceNotFound.new ostruct = OpenStruct.new(:status => 404, :body => 'ohno')
RestClient.should_receive(:post).with(url, data).and_raise(error) uploader.send(:connection).should_receive(:post).with('/documents', data).and_return(ostruct)
end end
it 'should get an error' do it 'should get an error' do
error_message.should == 'failure uploading: Resource Not Found' error_message.should == 'failure uploading: ohno'
end end
end end
@@ -64,7 +65,7 @@ describe Haste::Uploader do
before do before do
error = Errno::ECONNREFUSED 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 end
it 'should get the key' do it 'should get the key' do