1
0
mirror of https://github.com/seejohnrun/haste-client.git synced 2025-12-12 23: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
*.swo
*.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.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'

View File

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