Method: Fog::Storage::Google::Mock#get_bucket

Defined in:
lib/fog/google/requests/storage/get_bucket.rb

#get_bucket(bucket_name, options = {}) ⇒ Object


59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/fog/google/requests/storage/get_bucket.rb', line 59

def get_bucket(bucket_name, options = {})
  unless bucket_name
    raise ArgumentError.new('bucket_name is required')
  end
  response = Excon::Response.new
  name = /(\w+\.?)*/.match(bucket_name)
  if bucket_name == name.to_s
    if bucket = self.data[:buckets][bucket_name]
      contents = bucket[:objects].values.sort {|x,y| x['Key'] <=> y['Key']}.reject do |object|
          (options['prefix'] && object['Key'][0...options['prefix'].length] != options['prefix']) ||
          (options['marker'] && object['Key'] <= options['marker'])
        end.map do |object|
          data = object.reject {|key, value| !['ETag', 'Key'].include?(key)}
          data.merge!({
            'LastModified' => Time.parse(object['Last-Modified']),
            'Owner'        => bucket['Owner'],
            'Size'         => object['Content-Length'].to_i
          })
        data
      end
      max_keys = options['max-keys'] || 1000
      size = [max_keys, 1000].min
      truncated_contents = contents[0...size]

      response.status = 200
      response.body = {
        'CommonPrefixes'  => [],
        'Contents'        => truncated_contents,
        'IsTruncated'     => truncated_contents.size != contents.size,
        'Marker'          => options['marker'],
        'Name'            => bucket['Name'],
        'Prefix'          => options['prefix']
      }
      if options['max-keys'] && options['max-keys'] < response.body['Contents'].length
          response.body['IsTruncated'] = true
          response.body['Contents'] = response.body['Contents'][0...options['max-keys']]
      end
    else
      response.status = 404
      raise(Excon::Errors.status_error({:expects => 200}, response))
    end
  else
      response.status = 400
      raise(Excon::Errors.status_error({:expects => 200}, response))
  end
  response
end