Sha256: db8f316d96a173a953a6df90cfdf2db81b8a5f3fa25bd3cd3133c6e511a097b9

Contents?: true

Size: 733 Bytes

Versions: 4

Compression:

Stored size: 733 Bytes

Contents

require "securerandom"
require "moneta"

require "urb/middleware"
require "urb/engine" if defined?(Rails::Engine)
require "urb/version"

module URB
  extend self

  CHARS = [*0..9, *"A".."Z", *"a".."z"]
  PATH = "/-/"
  PREFIX = "@"
  MAXLENGTH = 2000

  def config(*args)
    @config = args
  end

  def fetch(key)
    paths[key]
  end

  def store(path)
    value = "#{PREFIX}#{path}"
    paths[value] || begin
      generate_key.tap do |key|
        paths[key] = path
        paths[value] = key
      end
    end
  end

private

  def paths
    @paths ||= Moneta.new *(@config || [:Memory])
  end

  def generate_key
    SecureRandom.urlsafe_base64(6).gsub(/[_-]/){ CHARS.sample }
  end

end

def URB(*args)
  URB.config *args
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
urb-0.1.3 lib/urb.rb
urb-0.1.2 lib/urb.rb
urb-0.1.1 lib/urb.rb
urb-0.1.0 lib/urb.rb