mirror of
https://github.com/seejohnrun/haste-client.git
synced 2025-12-13 15:25:55 +00:00
This patch makes it possible to either pipe data into the haste client, or just give a filename as first argument. Signed-off-by: Peter Haza
60 lines
1.3 KiB
Ruby
60 lines
1.3 KiB
Ruby
require 'json'
|
|
require 'net/http'
|
|
require 'uri'
|
|
|
|
module Haste
|
|
|
|
DEFAULT_URL = 'http://hastebin.com'
|
|
|
|
class CLI
|
|
|
|
attr_reader :input
|
|
|
|
# Pull all of the data from STDIN
|
|
def initialize
|
|
if STDIN.tty?
|
|
if ARGV.empty?
|
|
puts "No input file given"
|
|
exit
|
|
end
|
|
|
|
file = ARGV[0]
|
|
abort "#{file} doesn't exist" unless File.exists?(file)
|
|
|
|
@input = open(file).read.strip
|
|
else
|
|
@input = STDIN.readlines.join
|
|
@input.strip!
|
|
end
|
|
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)
|
|
STDOUT.puts "#{server}/#{data['key']}"
|
|
else
|
|
STDERR.puts "failure uploading: #{res.status}"
|
|
end
|
|
rescue RuntimeError, JSON::ParserError => e
|
|
STDERR.puts "failure uploading: #{response.code}"
|
|
rescue Errno::ECONNREFUSED => e
|
|
STDERR.puts "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
|