Class: Haste::Uploader

Inherits:
Object
  • Object
show all
Defined in:
lib/haste/uploader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server_url = nil) ⇒ Uploader

Returns a new instance of Uploader.



13
14
15
16
17
# 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

Instance Attribute Details

#server_urlObject (readonly)

Returns the value of attribute server_url.



11
12
13
# File 'lib/haste/uploader.rb', line 11

def server_url
  @server_url
end

Instance Method Details

#upload_path(path) ⇒ Object

Take in a path and return a key



20
21
22
23
24
# 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) ⇒ Object

Take in data and return a key



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# 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