1
0
mirror of https://github.com/seejohnrun/haste-client.git synced 2025-12-16 00:35:56 +00:00

26 Commits

Author SHA1 Message Date
John Crepezzi
94e2d1db1c Bump version 2014-08-20 10:49:40 -04:00
John Crepezzi
689387da3f Merge pull request #22 from raincoats/patch-1
fixed typo
2014-08-02 14:00:49 -07:00
raincoats
8ca0daab3b fixed typo 2014-07-30 16:56:13 +10:00
John Crepezzi
bfa7edda96 Update README.md
Construct Github pull request (using in-web editor) for awesome committer.

Closes #20
2014-02-05 08:17:23 -05:00
John Crepezzi
a83c921aa5 Remove bundler/setup from lib 2013-11-16 13:36:52 -05:00
John Crepezzi
326ac91e2a 1.8.7 support
Closes #19
2013-11-16 11:06:21 -05:00
John Crepezzi
b153c49aa1 Remove support 2013-11-16 10:21:40 -05:00
John Crepezzi
3fca7c72c8 Tests working in 1.9.2 2013-11-16 10:18:12 -05:00
John Crepezzi
e5a6d39422 travis config 2013-11-16 10:13:24 -05:00
John Crepezzi
f299892b37 Add tests back to Rakefile 2013-11-16 10:12:19 -05:00
John Crepezzi
425c80e318 rake dep 2013-11-16 10:11:29 -05:00
John Crepezzi
65d221d85c Add --raw option
Closes #17
2013-11-16 10:08:16 -05:00
John Crepezzi
b32cff6932 Update README 2013-11-16 10:01:33 -05:00
John Crepezzi
dd1c0178fc README 2013-11-16 10:00:24 -05:00
John Crepezzi
b7528b603b Merge pull request #18 from seejohnrun/rest-client
Rest client
2013-11-16 06:59:16 -08:00
John Crepezzi
a3fbc9e681 Revert "Adds Basic HTTP Auth support"
This reverts commit 1cdb3b7746.
2013-11-16 09:58:26 -05:00
John Crepezzi
d6b5954aac Merge pull request #16 from brunobuccolo/basic_auth
Adds Basic HTTP Auth support
2013-11-16 06:56:48 -08:00
John Crepezzi
c995ad95ef Tests and major refactor
[#16]
2013-11-16 09:51:13 -05:00
John Crepezzi
03d4f900de RestClient-based 2013-11-16 08:54:48 -05:00
Bruno Buccolo
1cdb3b7746 Adds Basic HTTP Auth support 2013-11-11 17:18:50 -02:00
John Crepezzi
ea7750c3bb Remove unused pieces of Rakefile 2013-11-08 14:04:24 -05:00
John Crepezzi
d380711d26 Bump version to 0.1.7 2013-11-08 14:03:46 -05:00
John Crepezzi
a195f017e2 Merge pull request #15 from TsCl/master
Fixed the JSON::ParserError rescue message
2013-11-08 11:03:04 -08:00
John Crepezzi
4e3e90f7cc Merge pull request #14 from tommcdo/master
Do not trim leading whitespace
2013-11-08 11:01:18 -08:00
Pierre MONTAGNIER
a7df62a0a5 Fixed the JSON::ParserError rescue message
I guess this message may be more appropriate for this error.
2013-09-22 00:26:29 +02:00
Tom McDonald
ce764ca7c4 Do not trim leading whitespace
For pastes of code snippets, removing leading whitespace usually
results in mis-aligned code.
2013-07-12 12:05:49 -04:00
14 changed files with 323 additions and 77 deletions

2
.gitignore vendored
View File

@@ -1,3 +1,5 @@
*.swp
*.swo
*.gem
Gemfile.lock
.rspec

9
.travis.yml Normal file
View File

@@ -0,0 +1,9 @@
script: "rspec spec"
branches:
only:
- master
rvm:
- 1.8.7
- 1.9.2
- 1.9.3
- 2.0.0

2
Gemfile Normal file
View File

@@ -0,0 +1,2 @@
source 'https://rubygems.org'
gemspec

View File

@@ -1,5 +1,7 @@
# Haste Client
[![Build Status](https://secure.travis-ci.org/seejohnrun/haste-client.png)](http://travis-ci.org/seejohnrun/haste-client)
haste-client is a simple client for uploading data to haste-server. All you do it pipe data in STDIN:
`cat file | haste`
@@ -19,24 +21,25 @@ after which the contents of `file` will be accessible at a URL which has been co
gem install haste
```
## Support
Please consider paying what you think haste-client is worth:
<a href="https://www.stripeme.com/pay/1r2f">
<img alt="Pay" src="https://www.stripeme.com/pay.jpg" />
</a>
## Making uploading file contents easier
If you supply a valid file path as argument #1 to the client, it will be uploaded:
``` bash
# equivelant
# equivalent
cat file | haste
haste file
```
## You can get the raw version
Want the raw URL returned instead? (a plain-text version)?
``` bash
cat file | haste --raw # or
haste file --raw
```
## Changing the location of your haste server
By default, haste will point at `http://hastebin.com`. You can change this by setting the value of `ENV['HASTE_SERVER']` to the URL of your haste server. You can also use `alias` to make easy shortcuts if you commonly use a few hastes intermingled with each other. To do that, you'd put something like this into ~.bash_profile:
@@ -47,22 +50,33 @@ alias work_haste="HASTE_SERVER=http://something.com haste"
After which you can use `work_haste` to send hastes to that server instead.
## Use as a library
You can also use `Haste` as a library to upload hastes:
``` ruby
require 'haste'
uploader = Haste::Uploader.new
uploader.upload_raw 'this is my data' # key
uploader.upload_path '/tmp/whaaaa' # key
```
## Windows Support
If you'd like an alternative on Windows that supports functionality similar to `pbcopy`, check out Aidan Ryan's [WinHaste](https://github.com/ajryan/WinHaste) project.
## Lightweight Alternative
Han Boetes has contributed a simple shell-script alternative for those not interested in installing a RubyGem:
Han Boetes and @nickthename have contributed a simple shell-script alternative for those not interested in installing a RubyGem:
``` bash
haste(){ ( echo "% $@"; eval "$@" ) | curl -F "$@=<-" http://hastebin.com/documents|awk -F '"' '{print "http://hastebin.com/"$4}'}
haste() { a=$(cat); curl -X POST -s -d "$a" http://hastebin.com/documents | awk -F '"' '{print "http://hastebin.com/"$4}'; }
```
Usage:
``` bash
haste `cat index.html
cat file.txt | haste
```
## Author

View File

@@ -1,6 +1,6 @@
require 'rspec/core/rake_task'
require File.dirname(__FILE__) + '/lib/haste/version'
task :build => :test do
system "gem build haste.gemspec"
end
@@ -12,14 +12,8 @@ task :release => :build do
# push the gem
system "gem push haste-#{Haste::VERSION}.gem"
end
RSpec::Core::RakeTask.new(:test) do |t|
t.pattern = 'spec/**/*_spec.rb'
fail_on_error = true # be explicit
end
RSpec::Core::RakeTask.new(:rcov) do |t|
t.pattern = 'spec/**/*_spec.rb'
t.rcov = true
fail_on_error = true # be explicit
end

View File

@@ -1,5 +1,7 @@
#!/usr/bin/env ruby
require 'bundler/setup'
require 'pathname'
path = Pathname.new(__FILE__)
require File.expand_path(File.dirname(path.realpath) + '/../lib/haste')

View File

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

View File

@@ -1,59 +1,6 @@
require 'json'
require 'net/http'
require 'net/https'
require 'uri'
require File.dirname(__FILE__) + '/haste/uploader'
require File.dirname(__FILE__) + '/haste/exception'
require File.dirname(__FILE__) + '/haste/cli'
module Haste
DEFAULT_URL = 'http://hastebin.com'
class CLI
# Pull all of the data from STDIN
def initialize
if STDIN.tty?
file = ARGV.first
abort 'No input file given' unless file
abort "#{file}: No such path" unless File.exists?(file)
@input = open(file).read
else
@input = STDIN.readlines.join
end
# clean up
@input.strip!
end
# Upload the and output the URL we get back
def start
uri = URI.parse server
http = Net::HTTP.new uri.host, uri.port
if uri.scheme =~ /^https/
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
end
response = http.post '/documents', @input
if response.is_a?(Net::HTTPOK)
data = JSON.parse(response.body)
method = STDOUT.tty? ? :puts : :print
STDOUT.send method, "#{server}/#{data['key']}"
else
abort "failure uploading: #{response.code}"
end
rescue JSON::ParserError => e
abort "failure uploading: #{response.code}"
rescue Errno::ECONNREFUSED => e
abort "failure connecting: #{e.message}"
end
private
def server
return @server if @server
@server = (ENV['HASTE_SERVER'] || Haste::DEFAULT_URL).dup
@server.chop! if server.end_with?('/')
@server
end
end
end

36
lib/haste/cli.rb Normal file
View File

@@ -0,0 +1,36 @@
module Haste
class CLI
# Create a new uploader
def initialize
@uploader = Uploader.new ENV['HASTE_SERVER']
end
# And then handle the basic usage
def start
# Take data in
if STDIN.tty?
key = @uploader.upload_path ARGV.first
else
key = @uploader.upload_raw STDIN.readlines.join
end
# Put together a URL
if ARGV.include?('--raw')
url = "#{@uploader.server_url}/raw/#{key}"
else
url = "#{@uploader.server_url}/#{key}"
end
# And write data out
if STDOUT.tty?
STDOUT.puts url
else
STDOUT.print url
end
rescue Exception => e
abort e.message
end
end
end

4
lib/haste/exception.rb Normal file
View File

@@ -0,0 +1,4 @@
module Haste
class Exception < StandardError
end
end

59
lib/haste/uploader.rb Normal file
View File

@@ -0,0 +1,59 @@
require 'json'
require 'faraday'
module Haste
DEFAULT_URL = 'http://hastebin.com'
class Uploader
attr_reader :server_url
def initialize(server_url = nil)
@server_url = server_url || Haste::DEFAULT_URL
@server_url = @server_url.dup
@server_url = @server_url.chop if @server_url.end_with?('/')
end
# Take in a path and return a key
def upload_path(path)
fail_with 'No input file given' unless path
fail_with "#{path}: No such path" unless File.exists?(path)
upload_raw open(path).read
end
# Take in data and return a key
def upload_raw(data)
data.rstrip!
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 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
end
end

View File

@@ -1,5 +1,5 @@
module Haste
VERSION = '0.1.6'
VERSION = '0.2.0'
end

View File

@@ -0,0 +1,174 @@
require 'spec_helper'
describe Haste::Uploader do
let(:uploader) { Haste::Uploader.new(base) }
describe :upload_raw do
let(:data) { 'hello world' }
let(:base) { nil }
let(:error_message) do
begin
@key = uploader.upload_raw data
nil # nil otherwise
rescue Haste::Exception => e
e.message
end
end
context 'with a good response' do
let(:json) { '{"key":"hello"}' }
before do
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
error_message.should be_nil # no error
@key.should == 'hello'
end
end
context 'with a bad json response' do
let(:json) { '{that:not_even_json}' }
before do
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
error_message.should start_with 'failure parsing response: '
end
end
context 'with a 404 response' do
before do
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: ohno'
end
end
context 'with a non-existent server' do
before do
error = Errno::ECONNREFUSED
uploader.send(:connection).should_receive(:post).with('/documents', data).and_raise(error)
end
it 'should get the key' do
error_message.should == 'failure connecting: Connection refused'
end
end
end
describe :upload_path do
let(:base) { nil }
let(:error_message) do
begin
uploader.upload_path path
nil # nil otherwise
rescue Haste::Exception => e
e.message
end
end
context 'with no path given' do
let(:path) { nil }
it 'should have an error' do
error_message.should == 'No input file given'
end
end
context 'with an invalid path given' do
let(:path) { '/tmp/why-do-you-have-a-file-called-john' }
it 'should have an error' do
error_message.should == "#{path}: No such path"
end
end
context 'with a valid path' do
let(:data) { 'hello world' }
let(:path) { '/tmp/real' }
before { File.open(path, 'w') { |f| f.write(data) } }
before do
uploader.should_receive(:upload_raw).with(data) # check
end
it 'should not receive an error' do
error_message.should be_nil
end
end
end
describe :server_url do
let(:server_url) { uploader.server_url }
context 'with default constructor' do
let(:base) { nil }
it 'should use the default url' do
server_url.should == Haste::DEFAULT_URL
end
end
context 'with server url passed in constructor' do
context 'with a trailing slash' do
before { @string = 'hello/' }
let(:base) { @string }
it 'should remove the slash' do
server_url.should == @string.chop
end
it 'should not modify the original' do
@string.should == 'hello/'
end
end
context 'with no trailing slash' do
let(:base) { 'hello' }
it 'should not chop the url' do
server_url.should == base
end
end
end
end
end

2
spec/spec_helper.rb Normal file
View File

@@ -0,0 +1,2 @@
require 'bundler/setup'
require File.dirname(__FILE__) + '/../lib/haste'