Module: Picky::Sinatra::IndexActions

Defined in:
lib/picky/sinatra/index_actions.rb

Class Method Summary collapse

Class Method Details

.extended(base) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/picky/sinatra/index_actions.rb', line 6

def self.extended base
  # Updates the given item and returns HTTP codes:
  #  * 200 if the index has been updated or no error case has occurred.
  #  * 404 if the index cannot be found.
  #  * 400 if no data or item id has been provided in the data.
  #
  # Note: 200 returns no data yet.
  #
  base.put '/' do
    index_name = params['index']
    begin
      index = Picky::Indexes[index_name.to_sym]
      data = params['data']
      return 400 unless data
      data && index.replace_from(MultiJson.decode data) && 200
    rescue IdNotGivenException
      400
    rescue StandardError
      404
    end
  end
  
  # Deletes the given item and returns:
  #  * 200 if the index has been updated or no error case has occurred.
  #  * 404 if the index cannot be found.
  #  * 400 if no data or item id has been provided in the data.
  #
  # Note: 200 returns no data yet.
  #
  base.delete '/' do
    index_name = params['index']
    begin
      index = Picky::Indexes[index_name.to_sym]
      data = MultiJson.decode params['data']
      id = data['id']
      id ? index.remove(id) && 200 : 400
    rescue StandardError
      404
    end
  end
end