module DeadDrop

Constants

VERSION

Public Class Methods

drop(resource, options = {}) click to toggle source
# File lib/dead_drop.rb, line 31
def self.drop(resource, options = {})
  options = { expiration: DeadDrop.default_expiration,
              limit: DeadDrop.default_access_limit,
              salt: DeadDrop.default_salt,
              filename: "",
              mime_type: nil
            }.merge(options)

  options[:limit] = 0 if options[:limit].nil? || options[:limit] < 0

  data = {resource: resource, filename: options[:filename], mime_type: options[:mime_type]}

  token = ""
  loop do
    token = SecureRandom.urlsafe_base64((DeadDrop.token_length*3)/4)
    break unless DeadDrop.exists?(token, salt: options[:salt])
  end

  packs_key = DeadDrop.packs_key(token, options[:salt])
  count_key = DeadDrop.count_key(token, options[:salt])

  DeadDrop.cache.write(packs_key, data, expires_in: options[:expiration])
  DeadDrop.cache.write(count_key, options[:limit]+1, expires_in: options[:expiration], raw: true)

  token
end
exists?(token, options = {}) click to toggle source
# File lib/dead_drop.rb, line 77
def self.exists?(token, options = {})
  defaults = { salt: DeadDrop.default_salt }
  options = defaults.merge(options)

  packs_key = DeadDrop.packs_key(token, options[:salt])
  DeadDrop.cache.exist?(packs_key)
end
pick(token, options = {}) click to toggle source
# File lib/dead_drop.rb, line 58
def self.pick(token, options = {})
  defaults = {ignore_limit: false, salt: DeadDrop.default_salt}
  options = defaults.merge(options)

  packs_key = DeadDrop.packs_key(token, options[:salt])
  count_key = DeadDrop.count_key(token, options[:salt])

  ret = DeadDrop.cache.read(packs_key)

  if options[:ignore_limit] == false
    if 1 == DeadDrop.cache.decrement(count_key, 1, initial: nil)
      DeadDrop.cache.delete(packs_key)
      DeadDrop.cache.delete(count_key)
    end
  end

  ret
end
setup() { |self| ... } click to toggle source
# File lib/dead_drop.rb, line 26
def self.setup(&block)
  yield self
end

Private Class Methods

count_key(token, salt) click to toggle source
# File lib/dead_drop.rb, line 91
def self.count_key(token, salt)
  '_count_'+Digest::SHA256.send(DeadDrop.cache_key_creation, salt+token)
end
packs_key(token, salt) click to toggle source
# File lib/dead_drop.rb, line 87
def self.packs_key(token, salt)
  '_packs_'+Digest::SHA256.send(DeadDrop.cache_key_creation, salt+token)
end