Class: Progstr::Filer::Uploader

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

Instance Method Summary collapse

Instance Method Details

#delete_attachment(attachment) ⇒ Object

Raises:



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/filer/uploader.rb', line 49

def delete_attachment(attachment)
  raise ApiError.new("message" => "Deleting a blank attachment.") if attachment.blank?
  url = "#{Progstr::Filer.url_prefix}files/#{attachment.id}"
  begin
    response = RestClient.delete(url, json_headers)
    UploadStatus.new MultiJson.decode(response)
  rescue MultiJson::DecodeError => unknown_error
    raise ApiError.new("message" => "Unknown server error when requesting #{url}:\r\n#{unknown_error}")
  rescue => e
    raise ApiError.new(MultiJson.decode(e.response))
  end
end

#file_info(attachment) ⇒ Object

Raises:



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/filer/uploader.rb', line 62

def file_info(attachment)
  raise ApiError.new("message" => "Getting info for a blank attachment.") if attachment.blank?

  url = "#{Progstr::Filer.url_prefix}files/info/#{attachment.id}"
  begin
    response = RestClient.get(url, json_headers)
    FileInfo.new MultiJson.decode(response)
  rescue MultiJson::DecodeError => unknown_error
    raise ApiError.new("message" => "Unknown server error when requesting #{url}:\r\n#{unknown_error}")
  rescue => e
    raise ApiError.new(MultiJson.decode(e.response))
  end
end

#json_headersObject



20
21
22
23
24
25
# File 'lib/filer/uploader.rb', line 20

def json_headers
  {
    :accept => "application/json",
    :"X-Progstr-Filer-Auth-Token" => Progstr::Filer.generate_auth_token,
  }
end

#upload_attachment(attachment) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/filer/uploader.rb', line 27

def upload_attachment(attachment)
  url = "#{Progstr::Filer.url_prefix}upload/new"
  puts "upload to: #{url}"
  begin
    response = RestClient.post(url, {
                :upload1 => attachment.file,
                :authToken => Progstr::Filer.generate_auth_token,
                :uploader => self.class.name,
                :id => attachment.id
              }, json_headers)
    result = UploadStatus.new MultiJson.decode(response)
    attachment.mark_uploaded!
    result
  rescue => e
    if e.respond_to?(:response) && !e.response.nil?
      raise ApiError.new(MultiJson.decode(e.response))
    else
      raise
    end
  end
end