Class: Mneme

Inherits:
Goliath::API
  • Object
show all
Includes:
Mnemosyne::Helper
Defined in:
lib/mneme.rb

Instance Method Summary collapse

Methods included from Mnemosyne::Helper

#epoch, #epoch_name

Instance Method Details

#query_filters(keys) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/mneme.rb', line 34

def query_filters(keys)
  found, missing = [], []
  keys.each do |key|

    present = false
    config['periods'].to_i.times do |n|
      if filter(n).key?(key)
        present = true
        break
      end
    end

    if present
      found << key
    else
      missing << key
    end
  end

  code = case keys.size
    when found.size then 200
    when missing.size then 404
    else 206
  end

  [code, {}, {found: found, missing: missing}]
end

#response(env) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/mneme.rb', line 23

def response(env)
  keys = [params.delete('key') || params.delete('key[]')].flatten.compact
  return [400, {}, {error: 'no key specified'}] if keys.empty?

  logger.debug "Processing: #{keys}"
  case env[Goliath::Request::REQUEST_METHOD]
    when 'GET'  then query_filters(keys)
    when 'POST' then update_filters(keys)
  end
end

#update_filters(keys) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/mneme.rb', line 62

def update_filters(keys)
  keys.each do |key|
    filter(0).insert key
    logger.debug "Inserted new key: #{key}"
  end

  [201, {}, '']
end