@@ -9,6 +9,7 @@ module Blobstore
99 class FakeStorage
1010 FakeFile = Struct . new ( :key , :body , :content_type , :public , :content_length , :bucket , keyword_init : true ) do
1111 def public_url = public ? "http://fake/#{ key } " : nil
12+ def url ( _expires ) = "http://fake/#{ key } "
1213
1314 def copy ( dest_bucket , dest_key , _opts = { } )
1415 storage . store_file ( dest_bucket , dest_key , body , content_type )
@@ -61,10 +62,10 @@ def create_bucket(key)
6162 class DirectoryProxy
6263 def initialize ( storage ) = @storage = storage
6364
64- def get ( key , ** )
65+ def get ( key , _options = nil , prefix : nil , ** _kwargs )
6566 return nil unless @storage . bucket_exists? ( key )
6667
67- BucketProxy . new ( @storage , key )
68+ BucketProxy . new ( @storage , key , prefix : )
6869 end
6970
7071 def create ( key :, **) = BucketProxy . new ( @storage , @storage . create_bucket ( key ) && key )
@@ -74,23 +75,27 @@ def new(key:, **) = BucketProxy.new(@storage, key)
7475 class BucketProxy
7576 attr_reader :key
7677
77- def initialize ( storage , key )
78+ def initialize ( storage , key , prefix : nil )
7879 @storage = storage
7980 @key = key
81+ @prefix = prefix
8082 @storage . create_bucket ( key )
8183 end
8284
83- def files = FilesProxy . new ( @storage , @key )
85+ def files = FilesProxy . new ( @storage , @key , prefix : @prefix )
8486 end
8587
8688 class FilesProxy
8789 include Enumerable
8890
89- def initialize ( storage , bucket )
91+ def initialize ( storage , bucket , prefix : nil )
9092 @storage = storage
9193 @bucket = bucket
94+ @prefix = prefix
9295 end
9396
97+ def length = all . length
98+
9499 def head ( key ) = @storage . get_file ( @bucket , key )
95100
96101 def get ( key , &block )
@@ -105,12 +110,17 @@ def get(key, &block)
105110 file
106111 end
107112
108- def create ( key :, body :, content_type : 'application/zip' , **)
113+ def create ( options_or_key = nil , key : nil , body : nil , content_type : 'application/zip' , **)
114+ if options_or_key . is_a? ( Hash )
115+ key = options_or_key [ :key ]
116+ body = options_or_key [ :body ]
117+ content_type = options_or_key [ :content_type ] || 'application/zip'
118+ end
109119 @storage . store_file ( @bucket , key , body , content_type )
110120 end
111121
112122 def each ( &) = all . each ( &)
113- def all = @storage . list_files ( @bucket )
123+ def all = @all ||= @ storage. list_files ( @bucket , prefix : @prefix )
114124
115125 def select ( prefix : nil )
116126 @storage . list_files ( @bucket , prefix :)
@@ -635,7 +645,7 @@ def upload_tmpfile(client, key='abcdef')
635645
636646 describe '#ensure_bucket_exists' do
637647 it 'gets the bucket' do
638- expect ( fake_storage . directories ) . to receive ( :get ) . with ( directory_key , max_keys : 1 ) . and_call_original
648+ expect ( fake_storage . directories ) . to receive ( :get ) . with ( directory_key , { max_keys : 1 } ) . and_call_original
639649 subject . ensure_bucket_exists
640650 end
641651
@@ -649,7 +659,7 @@ def upload_tmpfile(client, key='abcdef')
649659
650660 context 'the bucket does not exist' do
651661 it 'creates the bucket' do
652- allow ( fake_storage . directories ) . to receive ( :get ) . with ( directory_key , max_keys : 1 ) . and_return ( nil )
662+ allow ( fake_storage . directories ) . to receive ( :get ) . with ( directory_key , { max_keys : 1 } ) . and_return ( nil )
653663 expect ( fake_storage . directories ) . to receive ( :create ) . with ( key : directory_key , public : false ) . and_call_original
654664 subject . ensure_bucket_exists
655665 end
0 commit comments