Class: Stacktor::Swift::V1::ContainerObject

Inherits:
Core::Resource show all
Defined in:
lib/stacktor/swift/v1/container_object.rb

Instance Attribute Summary

Attributes inherited from Core::Resource

#client, #data

Instance Method Summary collapse

Methods inherited from Core::Resource

#[], #[]=, #handle_data, #headers, #initialize, #method_missing

Constructor Details

This class inherits a constructor from Stacktor::Core::Resource

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Stacktor::Core::Resource

Instance Method Details

#container_nameObject



13
14
15
# File 'lib/stacktor/swift/v1/container_object.rb', line 13

def container_name
  data['container_name']
end

#content_lengthObject



21
22
23
24
# File 'lib/stacktor/swift/v1/container_object.rb', line 21

def content_length
  bytes = data['bytes'] || headers['Content-Length']
  bytes ? bytes.to_i : nil
end

#content_typeObject



26
27
28
29
30
# File 'lib/stacktor/swift/v1/container_object.rb', line 26

def content_type
  type = data['content_type'] || headers['Content-Type']
  return nil if type.nil?
  return type.split(";").first
end

#deleteObject



89
90
91
92
93
94
95
96
# File 'lib/stacktor/swift/v1/container_object.rb', line 89

def delete
  result = @client.delete_object(container_name: self.container_name, object_name: self.name)
  if result[:success]
    return result
  else
    raise result[:body]
  end
end

#etagObject



17
18
19
# File 'lib/stacktor/swift/v1/container_object.rb', line 17

def etag
  data['hash'] || headers['ETag']
end

#last_modifiedObject



32
33
34
35
36
# File 'lib/stacktor/swift/v1/container_object.rb', line 32

def last_modified
  t = data['last_modified'] || headers['Last-Modified']
  return nil if t.nil?
  return Time.parse(t)
end

#metadataObject



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/stacktor/swift/v1/container_object.rb', line 38

def 
  ret = {}
  headers.each do |k,v|
    h = k.downcase.strip
    if h.start_with?("x-object-meta-")
      mk = h.split('-').last
      ret[mk] = v
    end
  end
  return ret
end

#nameObject



9
10
11
# File 'lib/stacktor/swift/v1/container_object.rb', line 9

def name
  data['name']
end

#readObject

TRANSACTIONS



52
53
54
55
56
57
58
59
# File 'lib/stacktor/swift/v1/container_object.rb', line 52

def read
  result = @client.get_object_content(container_name: self.container_name, object_name: self.name)
  if result[:success]
    return result[:body]
  else
    raise result[:body]
  end
end

#reloadObject



80
81
82
83
84
85
86
87
# File 'lib/stacktor/swift/v1/container_object.rb', line 80

def reload
  result = @client.(container_name: self.container_name, object_name: self.name)
  if result[:success]
    self.handle_data(result[:body], headers: result[:response])
  else
    raise result[:body]
  end
end

#stream(&block) ⇒ Object



61
62
63
64
65
66
67
68
69
70
# File 'lib/stacktor/swift/v1/container_object.rb', line 61

def stream(&block)
  result = @client.get_object_content(container_name: self.container_name, object_name: self.name) do |bytes|
    block.call(bytes)
  end
  if result[:success]
    return result[:body]
  else
    raise result[:body]
  end
end

#write_to_file(path) ⇒ Object



72
73
74
75
76
77
78
# File 'lib/stacktor/swift/v1/container_object.rb', line 72

def write_to_file(path)
  File.open(path, 'wb') do |fw|
    self.stream do |bytes|
      fw.write bytes
    end
  end
end