Class: RAWS::S3::Metadata

Inherits:
Hash
  • Object
show all
Defined in:
lib/raws/s3/metadata.rb

Constant Summary collapse

X_AMZ_META =
'x-amz-meta-'

Instance Method Summary collapse

Constructor Details

#initialize(header) ⇒ Metadata

Returns a new instance of Metadata.



4
5
6
7
8
# File 'lib/raws/s3/metadata.rb', line 4

def initialize(header)
  super()
  @header = header
  decode(header)
end

Instance Method Details

#decode(header) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/raws/s3/metadata.rb', line 15

def decode(header)
  clear
  header.select do |key, val|
    key.match(/^#{X_AMZ_META}/)
  end.each do |key, val|
    self[key.sub(X_AMZ_META, '')] = begin
      a = val.split(',').map do |val|
        RAWS.unescape(val)
      end
      1 < a.size ? a : a.first
    end
  end
  self
end

#encodeObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/raws/s3/metadata.rb', line 30

def encode
  self.inject({}) do |ret, (key, val)|
    key = X_AMZ_META + key

    if val.is_a? Array
      ret[key] = val.map do |v|
        RAWS.escape v
      end.join(',')
    else
      ret[key] = RAWS.escape val
    end

    ret
  end
end

#reloadObject



10
11
12
13
# File 'lib/raws/s3/metadata.rb', line 10

def reload
  @header.reload
  decode(@header)
end