1
0
mirror of https://github.com/seejohnrun/haste-client.git synced 2026-02-10 09:31:33 +00:00

Tests and major refactor

[#16]
This commit is contained in:
John Crepezzi
2013-11-16 09:51:13 -05:00
parent 03d4f900de
commit c995ad95ef
8 changed files with 266 additions and 45 deletions

View File

@@ -2,51 +2,9 @@ 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'
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.rstrip!
end
# Upload the and output the URL we get back
def start
raw_data = RestClient.post "#{server}/documents", @input
data = JSON.parse(raw_data)
key = data['key']
STDOUT.send (STDOUT.tty? ? :puts : :print), "#{server}/#{key}"
rescue JSON::ParserError => e
abort "failure parsing response: #{e.message}"
rescue RestClient::Exception => e
abort "failure uploading: #{e.message}"
rescue Errno::ECONNREFUSED => e
abort "failure connecting: #{e.message}"
end
private
# Get the server address used
def server
return @server if @server
@server = (ENV['HASTE_SERVER'] || Haste::DEFAULT_URL).dup
@server.chop! if server.end_with?('/')
@server
end
end
end

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

@@ -0,0 +1,31 @@
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
key = if STDIN.tty?
@uploader.upload_path ARGV.first
else
@uploader.upload_raw STDIN.readlines.join
end
# And write data out
url = "#{@uploader.server_url}/#{key}"
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

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

@@ -0,0 +1,44 @@
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!
raw_data = RestClient.post "#{self.server_url}/documents", data
data = JSON.parse(raw_data)
data['key']
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 fail_with(msg)
raise Exception.new(msg)
end
end
end