Module: Bucketrb::XMLResponse

Defined in:
lib/bucketrb/xml_response.rb

Class Method Summary collapse

Class Method Details

.content(entry) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
# File 'lib/bucketrb/xml_response.rb', line 104

def content(entry)
  <<~XML
    <Contents>
      <Key>#{escape(entry.key)}</Key>
      <LastModified>#{timestamp(entry.last_modified)}</LastModified>
      <ETag>#{entry.quoted_etag}</ETag>
      <Size>#{entry.size}</Size>
      <StorageClass>STANDARD</StorageClass>
    </Contents>
  XML
end

.continuation_token_xml(result) ⇒ Object



130
131
132
133
134
# File 'lib/bucketrb/xml_response.rb', line 130

def continuation_token_xml(result)
  return "" unless result.next_continuation_token

  "<NextContinuationToken>#{escape(result.next_continuation_token)}</NextContinuationToken>"
end

.copy_result(entry) ⇒ Object



83
84
85
86
87
88
89
90
91
# File 'lib/bucketrb/xml_response.rb', line 83

def copy_result(entry)
  <<~XML
    <?xml version="1.0" encoding="UTF-8"?>
    <CopyObjectResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
      <LastModified>#{timestamp(entry.last_modified)}</LastModified>
      <ETag>#{entry.quoted_etag}</ETag>
    </CopyObjectResult>
  XML
end

.delete_result(keys) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/bucketrb/xml_response.rb', line 66

def delete_result(keys)
  deleted = keys.map do |key|
    <<~XML
      <Deleted>
        <Key>#{escape(key)}</Key>
      </Deleted>
    XML
  end.join

  <<~XML
    <?xml version="1.0" encoding="UTF-8"?>
    <DeleteResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
      #{deleted}
    </DeleteResult>
  XML
end

.error(code, message) ⇒ Object



93
94
95
96
97
98
99
100
101
102
# File 'lib/bucketrb/xml_response.rb', line 93

def error(code, message)
  <<~XML
    <?xml version="1.0" encoding="UTF-8"?>
    <Error>
      <Code>#{escape(code)}</Code>
      <Message>#{escape(message)}</Message>
      <RequestId>bucketrb</RequestId>
    </Error>
  XML
end

.escape(value) ⇒ Object



120
121
122
# File 'lib/bucketrb/xml_response.rb', line 120

def escape(value)
  CGI.escapeHTML(value.to_s)
end

.list_all_my_buckets(buckets) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/bucketrb/xml_response.rb', line 10

def list_all_my_buckets(buckets)
  bucket_xml = buckets.map do |bucket|
    <<~XML
      <Bucket>
        <Name>#{escape(bucket)}</Name>
        <CreationDate>#{timestamp(Time.now.utc)}</CreationDate>
      </Bucket>
    XML
  end.join

  <<~XML
    <?xml version="1.0" encoding="UTF-8"?>
    <ListAllMyBucketsResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
      <Owner>
        <ID>bucketrb</ID>
        <DisplayName>bucketrb</DisplayName>
      </Owner>
      <Buckets>
        #{bucket_xml}
      </Buckets>
    </ListAllMyBucketsResult>
  XML
end

.list_bucket(result, version: 1) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/bucketrb/xml_response.rb', line 34

def list_bucket(result, version: 1)
  contents = result.objects.map { |entry| content(entry) }.join

  if version == 2
    <<~XML
      <?xml version="1.0" encoding="UTF-8"?>
      <ListBucketResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
        <Name>#{escape(result.bucket)}</Name>
        <Prefix>#{escape(result.prefix)}</Prefix>
        <KeyCount>#{result.objects.length}</KeyCount>
        <MaxKeys>#{result.max_keys}</MaxKeys>
        <IsTruncated>#{result.is_truncated}</IsTruncated>
        #{continuation_token_xml(result)}
        #{contents}
      </ListBucketResult>
    XML
  else
    <<~XML
      <?xml version="1.0" encoding="UTF-8"?>
      <ListBucketResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
        <Name>#{escape(result.bucket)}</Name>
        <Prefix>#{escape(result.prefix)}</Prefix>
        <Marker>#{escape(result.marker)}</Marker>
        <MaxKeys>#{result.max_keys}</MaxKeys>
        <IsTruncated>#{result.is_truncated}</IsTruncated>
        #{next_marker_xml(result)}
        #{contents}
      </ListBucketResult>
    XML
  end
end

.next_marker_xml(result) ⇒ Object



124
125
126
127
128
# File 'lib/bucketrb/xml_response.rb', line 124

def next_marker_xml(result)
  return "" unless result.next_marker

  "<NextMarker>#{escape(result.next_marker)}</NextMarker>"
end

.timestamp(time) ⇒ Object



116
117
118
# File 'lib/bucketrb/xml_response.rb', line 116

def timestamp(time)
  time.utc.iso8601(3)
end