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

72 Commits

Author SHA1 Message Date
Filipe Chagas
6170ad7de6 Fix codeowners 2022-06-02 11:58:55 -03:00
Adam Dubnytskyy
06d4afb1ca add codeowners (#51) 2022-06-02 16:57:59 +03:00
Filipe Chagas
eeede13a3c Add GHA workflow to mark and close inactive PRs and Issues 2022-03-08 11:46:12 -03:00
Nick Otter
6e9801e547 Basic Authentication (#28)
* adding basic http authentication

* updating ruby style

* SSL support, variable name corrections
2022-02-14 14:25:49 -03:00
Konstantin Rybakov
fb77a2e9f5 Merge pull request #38 from xenithorb/patch-1
Haste lite-client improvement
2022-01-12 16:36:36 +03:00
Mike Goodwin
baeeb2874a Haste lite-client improvement
- Easily change variables for scheme, host, port, language
- Specify language on execution 
- Closes #30
2017-10-21 12:21:38 -04:00
John Crepezzi
32c4523e1c Update travis to reflect rubies actually used in the real world 2017-07-19 17:30:45 -04:00
John Crepezzi
c307e8c904 Remove transpec dependency 2017-07-19 17:27:06 -04:00
John Crepezzi
1037d899b9 Bump version to 0.2.3
For #27
2017-07-19 17:23:07 -04:00
John Crepezzi
b5aa8fbacc Added some tests for #27 2017-07-19 17:22:04 -04:00
John Crepezzi
1f3318adeb Merge pull request #27 from vaibhav92/master
Fix issue #26
2017-07-19 17:13:43 -04:00
John Crepezzi
4c0e5f311d Merge pull request #34 from seejohnrun/move_readme_to_https
Move README to HTTPS
2017-03-24 11:24:40 -04:00
John Crepezzi
dab2d08f42 Move README to HTTPS
Also remove a trailing whitespace
2017-03-24 11:23:53 -04:00
John Crepezzi
dfb69c5e0d Fix RSpec dependencies 2017-01-30 21:56:07 -05:00
John Crepezzi
f79b70da61 Bump version 2017-01-30 21:51:41 -05:00
John Crepezzi
898884f07c default URL https 2017-01-30 21:51:03 -05:00
John Crepezzi
3ba2c53587 Added another entry to the alternates section of the README 2015-09-17 10:29:12 -04:00
vajain21
0ea5e5c61c Fix issue #26 2015-01-21 14:59:06 +05:30
John Crepezzi
94b2a4fbec Upgrade to RSpec 3 2014-11-01 10:40:23 -04:00
John Crepezzi
3a1d4045b3 Some gemspec changes 2014-11-01 10:35:37 -04:00
John Crepezzi
82ae343455 Update specs & fix Rakefile 2014-11-01 10:31:58 -04:00
John Crepezzi
22ea2e9e3c Bump version to 0.2.1 2014-11-01 10:24:07 -04:00
John Crepezzi
b6113d9fd3 Merge branch 'master' of github.com:seejohnrun/haste-client 2014-11-01 10:22:53 -04:00
John Crepezzi
a98f4d765e Remove bundler/setup 2014-11-01 10:22:40 -04:00
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
John Crepezzi
e6532aa100 Bump to 0.1.6 2013-06-03 12:37:39 -04:00
John Crepezzi
82d191fc5e Merge pull request #13 from zertrin/master
Make HTTPS support to actually work (require 'net/https')
2013-06-03 09:36:29 -07:00
zertrin
2ef8f926af Make HTTPS support to actually work (require 'net/https') 2013-06-02 02:03:55 +02:00
John Crepezzi
6430635bfa Bump version to 0.1.5 2013-04-08 11:59:50 -04:00
John Crepezzi
028e7c4424 Merge pull request #12 from sairez/master
add ability to use SSL to contact haste-server
2013-04-08 08:58:18 -07:00
Sarah Zelechoski
b10fa4782a simplify ssl support 2013-04-02 20:26:31 -03:00
Sarah Zelechoski
d8c4be9744 Update haste.rb
add HTTPS capability
2013-04-02 20:15:41 -03:00
John Crepezzi
7913dd521d Added install note to README
Closes #7
2013-03-16 20:41:32 -04:00
John Crepezzi
25140d99f5 Bump version to 0.1.4 2013-03-16 20:40:06 -04:00
John Crepezzi
6760a64957 Avoid explosions (code a little different from PR)
Closes #11
2013-03-16 20:39:20 -04:00
John Crepezzi
98e72bce43 Added note to README 2012-12-19 08:10:00 -05:00
John Crepezzi
b53350c723 Alternative 2012-12-16 16:21:45 -05:00
John Crepezzi
be8d4bc5de Added support page 2012-10-22 14:41:51 -04:00
John Crepezzi
42bad59634 Bump version to 0.1.3 2012-05-01 16:04:31 -04:00
John Crepezzi
e156c031a8 Fix dup issue in 1.8 2012-05-01 16:04:14 -04:00
John Crepezzi
ca0c9b239f Bump version 2012-04-22 22:38:20 -04:00
John Crepezzi
5d88f11605 Remove () 2012-04-22 22:38:06 -04:00
John Crepezzi
859de525b1 Merge pull request #6 from jokull/master
`HASTE_SERVER` variable should be `dup`ed
2012-04-22 19:37:30 -07:00
Jökull Sólberg Auðunsson
75ddd39187 Remove runtime error handler, dup environment string. 2012-04-21 16:56:30 +00:00
John Crepezzi
b24842ac6f Bump version to 0.1.1 2012-01-28 10:10:39 -05:00
John Crepezzi
636eec2924 Change how the pathname is received in haste-client 2012-01-28 10:10:23 -05:00
John Crepezzi
cf4c274d67 Added a note on WinHaste to the README 2012-01-27 09:44:43 -05:00
16 changed files with 484 additions and 71 deletions

1
.github/CODEOWNERS vendored Normal file
View File

@@ -0,0 +1 @@
* @toptal/site-acquisition-eng

30
.github/workflows/close-inactive.yaml vendored Normal file
View File

@@ -0,0 +1,30 @@
name: Close inactive issues and PRs
on:
workflow_dispatch:
schedule:
- cron: "30 1 * * *"
jobs:
close-stale:
runs-on: ubuntu-latest
permissions:
issues: write
pull-requests: write
steps:
- uses: actions/stale@v3
with:
days-before-stale: 30
days-before-close: 14
stale-issue-label: "stale"
stale-pr-label: "stale"
exempt-issue-labels: backlog,triage,nostale
exempt-pr-labels: backlog,triage,nostale
stale-pr-message: "This PR is stale because it has been open for 30 days with no activity."
close-pr-message: "This PR was closed because it has been inactive for 14 days since being marked as stale."
stale-issue-message: "This issue is stale because it has been open for 30 days with no activity."
close-issue-message: "This issue was closed because it has been inactive for 14 days since being marked as stale."
repo-token: ${{ secrets.GITHUB_TOKEN }}

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.3
- 2.0.0
- 2.4.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`
@@ -8,31 +10,103 @@ 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.
### Authentication
If your haste installation requires http authentication,
add the following to your ~.bash_profile:
```bash
export HASTE_USER="myusername"
export HASTE_PASS="mypassword"
```
if you are using SSL, you will need to supply your certs path
```bash
export HASTE_SSL_CERTS="/System/Library/OpenSSL/certs"
```
## 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() {
local S="https" H="hastebin.com" P="" L="$1"
local SHP="${S}://${H}${P}/"
curl -X POST -s --data-binary @- "${SHP}documents" \
| awk -F '"' 'b{ b="."b }; {print a$4b}' a="${SHP}" b="${L}"
}
```
Where `S` is the scheme, `H` is the host, `P` is the port, and `L` is the language. Requires `curl`
Usage:
``` bash
cat file.txt | haste # cat file into hate, output url
haste sh < script.sh # Same as above, but ensure shell syntax highlighting
xsel -b | haste txt # Output clipboard buffer into haste, ensure no highlighting
```
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>

View File

@@ -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

View File

@@ -1,6 +1,8 @@
#!/usr/bin/env ruby
require File.dirname(__FILE__) + '/../lib/haste'
require 'pathname'
path = Pathname.new(__FILE__)
require File.expand_path(File.dirname(path.realpath) + '/../lib/haste')
cli = Haste::CLI.new
cli.start

View File

@@ -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']

View File

@@ -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

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

@@ -0,0 +1,40 @@
module Haste
class CLI
# Create a new uploader
def initialize
@uploader = Uploader.new(
ENV['HASTE_SERVER'],
ENV['HASTE_USER'],
ENV['HASTE_PASS'],
ENV['HASTE_SSL_CERTS'])
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

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

@@ -0,0 +1,89 @@
require 'json'
require 'faraday'
require 'uri'
module Haste
DEFAULT_URL = 'https://hastebin.com'
class Uploader
attr_reader :server_url, :server_user, :server_pass, :ssl_certs
def initialize(server_url = nil, server_user = nil, server_pass = nil, ssl_certs = nil)
@server_url = server_url || Haste::DEFAULT_URL
@server_url = @server_url.dup
@server_url = @server_url.chop if @server_url.end_with?('/')
@server_user = server_user
@server_pass = server_pass
@ssl_certs = ssl_certs
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 ||= connection_set
end
def connection_set
return connection_https if @ssl_certs
connection_http
end
def connection_http
Faraday.new(:url => server_url) do |c|
connection_config(c)
end
end
def connection_https
Faraday.new(:url => server_url, :ssl => { :ca_path => @ssl_certs }) do |c|
connection_config(c)
end
end
def connection_config(config)
config.basic_auth(@server_user, @server_pass) if @server_user
config.adapter Faraday.default_adapter
end
def fail_with(msg)
raise Exception.new(msg)
end
end
end

View File

@@ -1,5 +1,5 @@
module Haste
VERSION = '0.1.0'
VERSION = '0.2.3'
end

View 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
View File

@@ -0,0 +1,3 @@
require 'ostruct'
require File.dirname(__FILE__) + '/../lib/haste'