wiki:DeveloperNotes

Developer Notes

Arrow::Service class

A derivative of Arrow::Applet that makes it easy to create a REST service. Instead of defining freeform actions, you define methods that map to HTTP verbs and whether or not the path_info is empty:

Request Service Method
GET /«resource» #fetch_all( txn )
GET /«resource»/«id» #fetch( txn, id )
POST /«resource» #create( txn )
PUT /«resource» #update_all( txn )
PUT /«resource»/«id» #update( txn, id )
DELETE /«resource» #delete_all( txn )
DELETE /«resource»/«id» #delete( txn, id )
require 'arrow/service'
require 'user'

class UserService < Arrow::Service

    ### Support for POST /«resource»
    def create( txn )
        fields = self.get_validated_body( txn )
        new_user = User.create( fields )
        uid = new_user.id

        txn.headers_out[:location] = txn.applet_uri + '/' + 
        finish_with( Apache::CREATED, "Create employee #{uid}" )
    end


    ### Support for GET /«resource»
    def fetch_all( txn )
        return User.all
    end


    ### Support for GET /«resource»/«id»
    def fetch( txn, uid )
        employee = User[ uid ] or
            finish_with( Apache::NOT_FOUND, "No such employee '#{uid}'" )

        return employee
    end


    # No #update_all, so PUT /«resource» will return a NOT_ALLOWED response

    ### Support for PUT /«resource»/«id»
    def update( txn, uid )
        employee = User[ uid ] or 
            finish_with( Apache::NOT_FOUND, "No such employee '#{uid}'" )
            
        fields = self.get_validated_body( txn )
        employee.update( fields )
        
        return "User #{uid} updated."
    end


    # No #delete_all either.

    ### Support for DELETE /«resource»/«id»
    def delete( txn, uid )
        employee = User[ uid ] or 
            finish_with( Apache::NOT_FOUND, "No such employee '#{uid}'" )
        employee.delete
        
        return "User #{uid} deleted."
    end
    
end