1
0
mirror of https://github.com/seejohnrun/haste-client.git synced 2025-12-15 00:05:56 +00:00

RestClient-based

This commit is contained in:
John Crepezzi
2013-11-16 08:54:48 -05:00
parent ea7750c3bb
commit 03d4f900de
4 changed files with 13 additions and 16 deletions

1
.gitignore vendored
View File

@@ -1,3 +1,4 @@
*.swp *.swp
*.swo *.swo
*.gem *.gem
Gemfile.lock

2
Gemfile Normal file
View File

@@ -0,0 +1,2 @@
source 'https://rubygems.org'
gemspec

View File

@@ -6,6 +6,7 @@ spec = Gem::Specification.new do |s|
s.author = 'John Crepezzi' s.author = 'John Crepezzi'
s.add_development_dependency('rspec') s.add_development_dependency('rspec')
s.add_dependency('json') s.add_dependency('json')
s.add_dependency('rest-client')
s.description = 'CLI Haste Client' s.description = 'CLI Haste Client'
s.email = 'john.crepezzi@gmail.com' s.email = 'john.crepezzi@gmail.com'
s.executables = 'haste' s.executables = 'haste'

View File

@@ -1,7 +1,7 @@
require 'bundler/setup'
require 'json' require 'json'
require 'net/http'
require 'net/https'
require 'uri' require 'uri'
require 'rest-client'
module Haste module Haste
@@ -25,28 +25,21 @@ module Haste
# Upload the and output the URL we get back # Upload the and output the URL we get back
def start def start
uri = URI.parse server raw_data = RestClient.post "#{server}/documents", @input
http = Net::HTTP.new uri.host, uri.port data = JSON.parse(raw_data)
if uri.scheme =~ /^https/ key = data['key']
http.use_ssl = true STDOUT.send (STDOUT.tty? ? :puts : :print), "#{server}/#{key}"
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
rescue JSON::ParserError => e rescue JSON::ParserError => e
abort "failure parsing response: #{e.message}" abort "failure parsing response: #{e.message}"
rescue RestClient::Exception => e
abort "failure uploading: #{e.message}"
rescue Errno::ECONNREFUSED => e rescue Errno::ECONNREFUSED => e
abort "failure connecting: #{e.message}" abort "failure connecting: #{e.message}"
end end
private private
# Get the server address used
def server def server
return @server if @server return @server if @server
@server = (ENV['HASTE_SERVER'] || Haste::DEFAULT_URL).dup @server = (ENV['HASTE_SERVER'] || Haste::DEFAULT_URL).dup