Sha256: c86ae7b24eba0b9af79ecdc8185c16731451fc29ad79fa53f54ce4bccc65d9cc

Contents?: true

Size: 1.46 KB

Versions: 49

Compression:

Stored size: 1.46 KB

Contents

module Picky
  module Sinatra
      
    module IndexActions
      
      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
      
    end
    
  end
end

Version data entries

49 entries across 49 versions & 1 rubygems

Version Path
picky-4.22.0 lib/picky/sinatra/index_actions.rb
picky-4.21.2 lib/picky/sinatra/index_actions.rb
picky-4.21.1 lib/picky/sinatra/index_actions.rb
picky-4.21.0 lib/picky/sinatra/index_actions.rb
picky-4.20.2 lib/picky/sinatra/index_actions.rb
picky-4.20.1 lib/picky/sinatra/index_actions.rb
picky-4.20.0 lib/picky/sinatra/index_actions.rb
picky-4.19.7 lib/picky/sinatra/index_actions.rb
picky-4.19.6 lib/picky/sinatra/index_actions.rb
picky-4.19.5 lib/picky/sinatra/index_actions.rb
picky-4.19.4 lib/picky/sinatra/index_actions.rb
picky-4.19.3 lib/picky/sinatra/index_actions.rb
picky-4.19.2 lib/picky/sinatra/index_actions.rb
picky-4.19.1 lib/picky/sinatra/index_actions.rb
picky-4.19.0 lib/picky/sinatra/index_actions.rb
picky-4.18.0 lib/picky/sinatra/index_actions.rb
picky-4.17.1 lib/picky/sinatra/index_actions.rb
picky-4.17.0 lib/picky/sinatra/index_actions.rb
picky-4.16.0 lib/picky/sinatra/index_actions.rb
picky-4.15.1 lib/picky/sinatra/index_actions.rb