Class: SwiftFile::SwiftUpload

Inherits:
Object
  • Object
show all
Defined in:
lib/swift_file/swift_upload.rb

Constant Summary collapse

SWIFTFILE_HOST =
'https://www.swiftfile.net'
SWIFTFILE_ENDPOINT =
''
'/tmp/swift_file.cookie'
SWIFTFILE_EXPIRY_OPTIONS =
%w(1m 1w 1d 1h)
SWIFTFILE_EXPIRY_DEFAULT =
'1d'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ SwiftUpload

Returns a new instance of SwiftUpload.



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/swift_file/swift_upload.rb', line 13

def initialize(params)
  @files    = params[:files] || []
  @group    = params[:group] || ''
  @password = params[:password] || ''

  if params[:expiry] && SWIFTFILE_EXPIRY_OPTIONS.include?(params[:expiry])
    @expiry = params[:expiry]
  else
    @expiry = SWIFTFILE_EXPIRY_DEFAULT
  end
end

Instance Attribute Details

#expiryObject

Returns the value of attribute expiry.



11
12
13
# File 'lib/swift_file/swift_upload.rb', line 11

def expiry
  @expiry
end

#filesObject

Returns the value of attribute files.



11
12
13
# File 'lib/swift_file/swift_upload.rb', line 11

def files
  @files
end

#groupObject

Returns the value of attribute group.



11
12
13
# File 'lib/swift_file/swift_upload.rb', line 11

def group
  @group
end

#urlObject

Returns the value of attribute url.



11
12
13
# File 'lib/swift_file/swift_upload.rb', line 11

def url
  @url
end

Instance Method Details

#add_file(file) ⇒ Object



25
26
27
# File 'lib/swift_file/swift_upload.rb', line 25

def add_file(file)
  @files << file unless @files.include?(file)
end

#transferObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/swift_file/swift_upload.rb', line 29

def transfer
  if @files.empty?
    raise 'No files supplied for uploed'
  end

  # start a session by requesting the url via GET
  begin
    client.perform
  rescue Curl::Err => e
    raise "Connection failed: #{e}"
  end

  # post the file and capture the resulting url
  begin
    client.multipart_form_post = true
    client.http_post(form)

    @url = client.last_effective_url
  rescue Curl::Err => e
    raise "Upload failed: #{e}"
  ensure
    client.close
  end

  @url
end