diff --git a/.gitignore b/.gitignore index d289004..fbb0a2a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ *.swp *.swo *.gem +Gemfile.lock diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..851fabc --- /dev/null +++ b/Gemfile @@ -0,0 +1,2 @@ +source 'https://rubygems.org' +gemspec diff --git a/haste.gemspec b/haste.gemspec index 479f791..79c5bca 100644 --- a/haste.gemspec +++ b/haste.gemspec @@ -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('rest-client') s.description = 'CLI Haste Client' s.email = 'john.crepezzi@gmail.com' s.executables = 'haste' diff --git a/lib/haste.rb b/lib/haste.rb index 229e88a..56f4aa8 100644 --- a/lib/haste.rb +++ b/lib/haste.rb @@ -1,7 +1,7 @@ +require 'bundler/setup' require 'json' -require 'net/http' -require 'net/https' require 'uri' +require 'rest-client' module Haste @@ -25,28 +25,21 @@ module Haste # 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 + 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