Sha256: 36ba1ccc7a09f7311a13c19a9f220bdb6b60309b2ecdb445546aa30978fd3a9d

Contents?: true

Size: 692 Bytes

Versions: 1

Compression:

Stored size: 692 Bytes

Contents

require "uri"

module Blobby

  # Defines the keys we allow for use in BLOB-store implementations.
  #
  # Basically, we allow anything that would be a valid URI "path" component.
  #
  module KeyConstraint

    BAD_PATTERNS = [
      %r{\A\Z}, # blank
      %r{\A/}, # leading slash
      %r{/\Z}, # trailing slash
      %r{//+}, # multiple slashes
      %r{:} # colon
    ].freeze

    module_function

    def allows?(key)
      BAD_PATTERNS.none? { |pattern| pattern =~ key } &&
        URI.parse(key).path == key
    rescue URI::InvalidURIError
      false
    end

    def must_allow!(key)
      fail ArgumentError, "invalid key: #{key.inspect}" unless allows?(key)
    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
blobby-1.1.0 lib/blobby/key_constraint.rb