Class: Seaweed::File

Inherits:
Object
  • Object
show all
Defined in:
lib/seaweed/file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fid, volume_url: nil, attachment: nil) ⇒ File

Returns a new instance of File.



5
6
7
8
9
10
# File 'lib/seaweed/file.rb', line 5

def initialize(fid, volume_url: nil, attachment: nil)
  @id = fid
  @volume_id, @key, @cookie = fid.match(/^(\d),(\w\w)(\w+)$/).captures
  @volume_url = volume_url
  @attachment = attachment
end

Instance Attribute Details

#attachmentObject (readonly)

Returns the value of attribute attachment.



3
4
5
# File 'lib/seaweed/file.rb', line 3

def attachment
  @attachment
end

#idObject (readonly)

Returns the value of attribute id.



3
4
5
# File 'lib/seaweed/file.rb', line 3

def id
  @id
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/seaweed/file.rb', line 3

def name
  @name
end

#sizeObject (readonly)

Returns the value of attribute size.



3
4
5
# File 'lib/seaweed/file.rb', line 3

def size
  @size
end

#volume_idObject (readonly)

Returns the value of attribute volume_id.



3
4
5
# File 'lib/seaweed/file.rb', line 3

def volume_id
  @volume_id
end

#volume_urlObject (readonly)

Returns the value of attribute volume_url.



3
4
5
# File 'lib/seaweed/file.rb', line 3

def volume_url
  @volume_url
end

Instance Method Details

#delete!Object



20
21
22
23
# File 'lib/seaweed/file.rb', line 20

def delete!
  res = Seaweed::HTTP.delete url
  !(Seaweed::HTTP.parse(res)[:size].nil?)
end

#pretty_urlObject



33
34
35
# File 'lib/seaweed/file.rb', line 33

def pretty_url
  "#{@volume_url}/#{@volume_id}/#{@key}#{@cookie}/#{@name}"
end

#readObject



25
26
27
# File 'lib/seaweed/file.rb', line 25

def read
  Seaweed::HTTP.get url
end

#upload!Object



12
13
14
15
16
17
18
# File 'lib/seaweed/file.rb', line 12

def upload!
  response = Seaweed::HTTP.put url, file: File.new(@attachment, 'rb')
  data = Seaweed::HTTP.parse response
  @name = data[:name]
  @size = data[:size]
  self
end

#urlObject



29
30
31
# File 'lib/seaweed/file.rb', line 29

def url
  "#{@volume_url}/#{@id}"
end