Checks if the storage conforms to Shrine’s specification.
Shrine::Storage::Linter.new(storage)
.call
If the check fails, by default it raises a Shrine::LintError
, but you can also specify action: :warn
:
Shrine::Storage::Linter.new
(storage, action: :warn).call
You can also specify an IO factory which the storage will use:
Shrine::Storage::Linter.new(storage)
.call(->{File.open(“test/fixtures/image.jpg”)})
Methods
Public Class
Public Instance
Classes and Modules
Public Class methods
call(*args)
[show source]
# File lib/shrine/storage/linter.rb 27 def self.call(*args) 28 new(*args).call 29 end
new(storage, action: :error, nonexisting: "nonexisting")
[show source]
# File lib/shrine/storage/linter.rb 31 def initialize(storage, action: :error, nonexisting: "nonexisting") 32 @storage = storage 33 @action = action 34 @nonexisting = nonexisting 35 end
Public Instance methods
call(io_factory = default_io_factory)
[show source]
# File lib/shrine/storage/linter.rb 37 def call(io_factory = default_io_factory) 38 storage.upload(io_factory.call, id = "foo", shrine_metadata: { "foo" => "bar" }) 39 40 lint_open(id) 41 lint_exists(id) 42 lint_url(id) 43 lint_delete(id) 44 45 if storage.respond_to?(:delete_prefixed) 46 storage.upload(io_factory.call, id1 = "a/a/a") 47 storage.upload(io_factory.call, id2 = "a/a/b") 48 storage.upload(io_factory.call, id3 = "a/aaa/a") 49 50 lint_delete_prefixed(prefix: "a/a/", 51 expect_deleted: [id1, id2], 52 expect_remaining: [id3]) 53 54 storage.delete(id3) 55 end 56 57 if storage.respond_to?(:clear!) 58 storage.upload(io_factory.call, id = "quux".dup) 59 lint_clear(id) 60 end 61 62 if storage.respond_to?(:presign) 63 lint_presign(id) 64 end 65 66 true 67 end
lint_clear(id)
[show source]
# File lib/shrine/storage/linter.rb 104 def lint_clear(id) 105 storage.clear! 106 error :clear!, "file still #exists? after clearing" if storage.exists?(id) 107 end
lint_delete(id)
[show source]
# File lib/shrine/storage/linter.rb 94 def lint_delete(id) 95 storage.delete(id) 96 error :delete, "file still #exists? after deleting" if storage.exists?(id) 97 begin 98 storage.delete(id) 99 rescue => exception 100 error :delete, "shouldn't fail if the file doesn't exist, but raised #{exception.class}" 101 end 102 end
lint_delete_prefixed(prefix:, expect_deleted:, expect_remaining:)
[show source]
# File lib/shrine/storage/linter.rb 116 def lint_delete_prefixed(prefix:, expect_deleted:, expect_remaining:) 117 storage.delete_prefixed(prefix) 118 119 expect_deleted.each do |key| 120 next unless storage.exists?(key) 121 error :delete_prefixed, "#{key} still #exists? after #clear_prefix('a/a/')" 122 end 123 124 expect_remaining.each do |key| 125 next if storage.exists?(key) 126 error :delete_prefixed, "#{key} doesn't #exists? but should after #clear_prefix('a/a/')" 127 end 128 end
lint_exists(id)
[show source]
# File lib/shrine/storage/linter.rb 84 def lint_exists(id) 85 error :exists?, "returns false for a file that was uploaded" if !storage.exists?(id) 86 end
lint_open(id)
[show source]
# File lib/shrine/storage/linter.rb 69 def lint_open(id) 70 opened = storage.open(id) 71 error :open, "doesn't return a valid IO object" if !io?(opened) 72 error :open, "returns an empty IO object" if opened.read.empty? 73 opened.close 74 75 begin 76 storage.open(@nonexisting) 77 error :open, "should raise an exception on nonexisting file" 78 rescue Shrine::FileNotFound 79 rescue => exception 80 error :open, "should raise Shrine::FileNotFound on nonexisting file" 81 end 82 end
lint_presign(id)
[show source]
# File lib/shrine/storage/linter.rb 109 def lint_presign(id) 110 data = storage.presign(id) 111 error :presign, "result should be a Hash" unless data.respond_to?(:to_h) 112 error :presign, "result should include :method key" unless data.to_h.key?(:method) 113 error :presign, "result should include :url key" unless data.to_h.key?(:url) 114 end
lint_url(id)
[show source]
# File lib/shrine/storage/linter.rb 88 def lint_url(id) 89 # just assert #url exists, it isn't required to return anything 90 url = storage.url(id) 91 error :url, "should return either nil or a string" if !(url.nil? || url.is_a?(String)) 92 end