Class: Haste::Uploader
- Inherits:
-
Object
- Object
- Haste::Uploader
- Defined in:
- lib/haste/uploader.rb
Instance Attribute Summary collapse
-
#server_url ⇒ Object
readonly
Returns the value of attribute server_url.
Instance Method Summary collapse
-
#initialize(server_url = nil) ⇒ Uploader
constructor
A new instance of Uploader.
-
#upload_path(path) ⇒ Object
Take in a path and return a key.
-
#upload_raw(data) ⇒ Object
Take in data and return a key.
Constructor Details
#initialize(server_url = nil) ⇒ Uploader
Returns a new instance of Uploader.
12 13 14 15 16 |
# File 'lib/haste/uploader.rb', line 12 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 |
Instance Attribute Details
#server_url ⇒ Object (readonly)
Returns the value of attribute server_url.
10 11 12 |
# File 'lib/haste/uploader.rb', line 10 def server_url @server_url end |
Instance Method Details
#upload_path(path) ⇒ Object
Take in a path and return a key
19 20 21 22 23 |
# File 'lib/haste/uploader.rb', line 19 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) ⇒ Object
Take in data and return a key
26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/haste/uploader.rb', line 26 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.}" rescue Errno::ECONNREFUSED => e fail_with "failure connecting: #{e.}" end |