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

1.8.7 support

Closes #19
This commit is contained in:
John Crepezzi
2013-11-16 11:06:21 -05:00
parent b153c49aa1
commit 326ac91e2a
5 changed files with 31 additions and 17 deletions

View File

@@ -1,3 +1,6 @@
require 'json'
require 'faraday'
module Haste
DEFAULT_URL = 'http://hastebin.com'
@@ -22,19 +25,31 @@ module Haste
# 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']
response = do_post data
if response.status == 200
data = JSON.parse(response.body)
data['key']
else
fail_with "failure uploading: #{response.body}"
end
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 do_post(data)
connection.post('/documents', data)
end
def connection
@connection ||= Faraday.new(:url => server_url) do |c|
c.adapter Faraday.default_adapter
end
end
def fail_with(msg)
raise Exception.new(msg)
end