Module: DeadDrop

Defined in:
lib/dead_drop.rb,
lib/dead_drop/engine.rb,
lib/dead_drop/version.rb,
app/helpers/dead_drop/application_helper.rb,
app/controllers/dead_drop/dead_drop_controller.rb,
app/controllers/dead_drop/application_controller.rb

Defined Under Namespace

Modules: ApplicationHelper Classes: ApplicationController, DeadDropController, Engine

Constant Summary collapse

VERSION =
"0.4.1.0"

Class Method Summary collapse

Class Method Details

.drop(resource, options = {}) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# 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 = {}) ⇒ Boolean

Returns:

  • (Boolean)


77
78
79
80
81
82
83
# 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 = {}) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# 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| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:

  • _self (DeadDrop)

    the object that the method was called on



26
27
28
# File 'lib/dead_drop.rb', line 26

def self.setup(&block)
  yield self
end