mirror of
https://github.com/seejohnrun/haste-client.git
synced 2025-12-16 16:51:29 +00:00
Compare commits
60 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b5aa8fbacc | ||
|
|
1f3318adeb | ||
|
|
4c0e5f311d | ||
|
|
dab2d08f42 | ||
|
|
dfb69c5e0d | ||
|
|
f79b70da61 | ||
|
|
898884f07c | ||
|
|
3ba2c53587 | ||
|
|
0ea5e5c61c | ||
|
|
94b2a4fbec | ||
|
|
3a1d4045b3 | ||
|
|
82ae343455 | ||
|
|
22ea2e9e3c | ||
|
|
b6113d9fd3 | ||
|
|
a98f4d765e | ||
|
|
94e2d1db1c | ||
|
|
689387da3f | ||
|
|
8ca0daab3b | ||
|
|
bfa7edda96 | ||
|
|
a83c921aa5 | ||
|
|
326ac91e2a | ||
|
|
b153c49aa1 | ||
|
|
3fca7c72c8 | ||
|
|
e5a6d39422 | ||
|
|
f299892b37 | ||
|
|
425c80e318 | ||
|
|
65d221d85c | ||
|
|
b32cff6932 | ||
|
|
dd1c0178fc | ||
|
|
b7528b603b | ||
|
|
a3fbc9e681 | ||
|
|
d6b5954aac | ||
|
|
c995ad95ef | ||
|
|
03d4f900de | ||
|
|
1cdb3b7746 | ||
|
|
ea7750c3bb | ||
|
|
d380711d26 | ||
|
|
a195f017e2 | ||
|
|
4e3e90f7cc | ||
|
|
a7df62a0a5 | ||
|
|
ce764ca7c4 | ||
|
|
e6532aa100 | ||
|
|
82d191fc5e | ||
|
|
2ef8f926af | ||
|
|
6430635bfa | ||
|
|
028e7c4424 | ||
|
|
b10fa4782a | ||
|
|
d8c4be9744 | ||
|
|
7913dd521d | ||
|
|
25140d99f5 | ||
|
|
6760a64957 | ||
|
|
98e72bce43 | ||
|
|
b53350c723 | ||
|
|
be8d4bc5de | ||
|
|
42bad59634 | ||
|
|
e156c031a8 | ||
|
|
ca0c9b239f | ||
|
|
5d88f11605 | ||
|
|
859de525b1 | ||
|
|
75ddd39187 |
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1,3 +1,5 @@
|
||||
*.swp
|
||||
*.swo
|
||||
*.gem
|
||||
Gemfile.lock
|
||||
.rspec
|
||||
|
||||
9
.travis.yml
Normal file
9
.travis.yml
Normal file
@@ -0,0 +1,9 @@
|
||||
script: "rspec spec"
|
||||
branches:
|
||||
only:
|
||||
- master
|
||||
rvm:
|
||||
- 1.8.7
|
||||
- 1.9.2
|
||||
- 1.9.3
|
||||
- 2.0.0
|
||||
3
Gemfile
Normal file
3
Gemfile
Normal file
@@ -0,0 +1,3 @@
|
||||
source 'https://rubygems.org'
|
||||
gemspec
|
||||
gem 'transpec'
|
||||
55
README.md
55
README.md
@@ -1,5 +1,7 @@
|
||||
# Haste Client
|
||||
|
||||
[](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`
|
||||
@@ -8,35 +10,78 @@ And once the output makes it to the server, it will print the URL to STDOUT.
|
||||
|
||||
This can be really really cool in combination with `pbcopy`, like:
|
||||
|
||||
* mac osx: `cat file | haste | pbcopy`
|
||||
* mac osx: `cat file | haste | pbcopy`
|
||||
* linux: `cat file | haste | xsel`
|
||||
|
||||
after which the contents of `file` will be accessible at a URL which has been copied to your pasteboard.
|
||||
|
||||
## Installation
|
||||
|
||||
``` bash
|
||||
gem install haste
|
||||
```
|
||||
|
||||
## 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
|
||||
```
|
||||
|
||||
## Changing the location of your haste server
|
||||
## You can get the raw version
|
||||
|
||||
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:
|
||||
Want the raw URL returned instead? (a plain-text version)?
|
||||
|
||||
``` bash
|
||||
alias work_haste="HASTE_SERVER=http://something.com haste"
|
||||
cat file | haste --raw # or
|
||||
haste file --raw
|
||||
```
|
||||
|
||||
## Changing the location of your haste server
|
||||
|
||||
By default, haste will point at `https://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:
|
||||
|
||||
``` bash
|
||||
alias work_haste="HASTE_SERVER=https://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 and @nickthename have contributed a simple shell-script alternative for those not interested in installing a RubyGem:
|
||||
|
||||
``` bash
|
||||
haste() { a=$(cat); curl -X POST -s -d "$a" https://hastebin.com/documents | awk -F '"' '{print "https://hastebin.com/"$4}'; }
|
||||
```
|
||||
|
||||
Usage:
|
||||
|
||||
``` bash
|
||||
cat file.txt | haste
|
||||
```
|
||||
|
||||
And a more expansive BASH option by @diethnis can be found at:
|
||||
https://github.com/diethnis/standalones/blob/master/hastebin.sh
|
||||
|
||||
## Author
|
||||
|
||||
John Crepezzi <john.crepezzi@gmail.com>
|
||||
|
||||
17
Rakefile
17
Rakefile
@@ -1,7 +1,9 @@
|
||||
require 'rspec/core/rake_task'
|
||||
require File.dirname(__FILE__) + '/lib/haste/version'
|
||||
|
||||
task :build => :test do
|
||||
|
||||
RSpec::Core::RakeTask.new(:spec)
|
||||
|
||||
task :build => :spec do
|
||||
system "gem build haste.gemspec"
|
||||
end
|
||||
|
||||
@@ -12,14 +14,3 @@ 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
|
||||
|
||||
@@ -1,12 +1,15 @@
|
||||
require 'rubygems'
|
||||
require File.dirname(__FILE__) + '/lib/haste/version'
|
||||
|
||||
spec = Gem::Specification.new do |s|
|
||||
Gem::Specification.new do |s|
|
||||
s.name = 'haste'
|
||||
s.author = 'John Crepezzi'
|
||||
s.add_development_dependency('rspec')
|
||||
s.add_dependency('json')
|
||||
s.add_dependency('faraday', '~> 0.9')
|
||||
s.description = 'CLI Haste Client'
|
||||
s.license = 'MIT License'
|
||||
s.homepage = 'https://github.com/seejohnrun/haste-client'
|
||||
s.email = 'john.crepezzi@gmail.com'
|
||||
s.executables = 'haste'
|
||||
s.files = Dir['lib/**/*.rb', 'haste']
|
||||
|
||||
53
lib/haste.rb
53
lib/haste.rb
@@ -1,53 +1,6 @@
|
||||
require 'json'
|
||||
require 'net/http'
|
||||
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?
|
||||
abort 'No input file given' unless ARGV.length == 1
|
||||
abort "#{file}: No such path" unless File.exists?(file = ARGV[0])
|
||||
@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
|
||||
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 RuntimeError, 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
|
||||
@server.chop! if server.end_with?('/')
|
||||
@server
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
36
lib/haste/cli.rb
Normal file
36
lib/haste/cli.rb
Normal 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
4
lib/haste/exception.rb
Normal file
@@ -0,0 +1,4 @@
|
||||
module Haste
|
||||
class Exception < StandardError
|
||||
end
|
||||
end
|
||||
65
lib/haste/uploader.rb
Normal file
65
lib/haste/uploader.rb
Normal file
@@ -0,0 +1,65 @@
|
||||
require 'json'
|
||||
require 'faraday'
|
||||
require 'uri'
|
||||
|
||||
module Haste
|
||||
|
||||
DEFAULT_URL = 'https://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 post_path
|
||||
parsed_uri = URI.parse(server_url)
|
||||
"#{parsed_uri.path}/documents"
|
||||
end
|
||||
|
||||
def do_post(data)
|
||||
connection.post(post_path, 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
|
||||
@@ -1,5 +1,5 @@
|
||||
module Haste
|
||||
|
||||
VERSION = '0.1.1'
|
||||
VERSION = '0.2.2'
|
||||
|
||||
end
|
||||
|
||||
210
spec/examples/uploader_spec.rb
Normal file
210
spec/examples/uploader_spec.rb
Normal file
@@ -0,0 +1,210 @@
|
||||
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)
|
||||
expect(uploader.send(:connection)).to receive(:post).with('/documents', data).and_return(ostruct)
|
||||
end
|
||||
|
||||
it 'should get the key' do
|
||||
expect(error_message).to be_nil # no error
|
||||
expect(@key).to eq('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)
|
||||
expect(uploader.send(:connection)).to receive(:post).with('/documents', data).and_return(ostruct)
|
||||
end
|
||||
|
||||
it 'should get an error' do
|
||||
expect(error_message).to start_with('failure parsing response: ')
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
context 'with a 404 response' do
|
||||
|
||||
before do
|
||||
ostruct = OpenStruct.new(:status => 404, :body => 'ohno')
|
||||
expect(uploader.send(:connection)).to receive(:post).with('/documents', data).and_return(ostruct)
|
||||
end
|
||||
|
||||
it 'should get an error' do
|
||||
expect(error_message).to eq('failure uploading: ohno')
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
context 'with a non-existent server' do
|
||||
|
||||
before do
|
||||
error = Errno::ECONNREFUSED
|
||||
expect(uploader.send(:connection)).to receive(:post).with('/documents', data).and_raise(error)
|
||||
end
|
||||
|
||||
it 'should get the key' do
|
||||
expect(error_message).to eq('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
|
||||
expect(error_message).to eq('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
|
||||
expect(error_message).to eq("#{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
|
||||
expect(uploader).to receive(:upload_raw).with(data) # check
|
||||
end
|
||||
|
||||
it 'should not receive an error' do
|
||||
expect(error_message).to be_nil
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
describe :post_path do
|
||||
|
||||
let(:post_path) { uploader.send(:post_path) }
|
||||
|
||||
context "when the server URL doesn't have a path" do
|
||||
|
||||
let(:base) { 'http://example.com/' }
|
||||
|
||||
it 'should return /documents' do
|
||||
expect(post_path).to eq('/documents')
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
context "when the server URL has a path" do
|
||||
|
||||
let(:base) { 'http://example.com/friend' }
|
||||
|
||||
it 'should return /documents' do
|
||||
expect(post_path).to eq('/friend/documents')
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
context "when the server URL has a path that ends with slash" do
|
||||
|
||||
let(:base) { 'http://example.com/friend/' }
|
||||
|
||||
it 'should return /documents appended to the path without a duplicate slash' do
|
||||
expect(post_path).to eq('/friend/documents')
|
||||
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
|
||||
expect(server_url).to eq(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
|
||||
expect(server_url).to eq(@string.chop)
|
||||
end
|
||||
|
||||
it 'should not modify the original' do
|
||||
expect(@string).to eq('hello/')
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
context 'with no trailing slash' do
|
||||
|
||||
let(:base) { 'hello' }
|
||||
|
||||
it 'should not chop the url' do
|
||||
expect(server_url).to eq(base)
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
end
|
||||
3
spec/spec_helper.rb
Normal file
3
spec/spec_helper.rb
Normal file
@@ -0,0 +1,3 @@
|
||||
require 'ostruct'
|
||||
|
||||
require File.dirname(__FILE__) + '/../lib/haste'
|
||||
Reference in New Issue
Block a user