class Haste::Uploader
Attributes
server_url[R]
Public Class Methods
new(server_url = nil)
click to toggle source
# File lib/haste/uploader.rb, line 13 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
Public Instance Methods
upload_path(path)
click to toggle source
Take in a path and return a key
# File lib/haste/uploader.rb, line 20 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
upload_raw(data)
click to toggle source
Take in data and return a key
# File lib/haste/uploader.rb, line 27 def upload_raw(data) data.rstrip! 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 Errno::ECONNREFUSED => e fail_with "failure connecting: #{e.message}" end
Private Instance Methods
connection()
click to toggle source
# File lib/haste/uploader.rb, line 53 def connection @connection ||= Faraday.new(:url => server_url) do |c| c.adapter Faraday.default_adapter end end
do_post(data)
click to toggle source
# File lib/haste/uploader.rb, line 49 def do_post(data) connection.post(post_path, data) end
fail_with(msg)
click to toggle source
# File lib/haste/uploader.rb, line 59 def fail_with(msg) raise Exception.new(msg) end
post_path()
click to toggle source
# File lib/haste/uploader.rb, line 44 def post_path parsed_uri = URI.parse(server_url) "#{parsed_uri.path}/documents" end