mirror of
https://github.com/seejohnrun/haste-client.git
synced 2025-12-12 14:55:56 +00:00
@@ -3,6 +3,7 @@ branches:
|
||||
only:
|
||||
- master
|
||||
rvm:
|
||||
- 1.8.7
|
||||
- 1.9.2
|
||||
- 1.9.3
|
||||
- 2.0.0
|
||||
|
||||
@@ -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'
|
||||
|
||||
@@ -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'
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user