class FReCon::Controller

Public: A base class to represent a controller.

Public Class Methods

could_not_find(value, attribute = 'id', model = model_name.downcase) click to toggle source

Public: Generate a could-not-find message.

value - The value that was tested. attribute - The attribute that was used for the search. model - The model that the search was performed upon.

Returns a String containing the error message.

# File lib/frecon/controller.rb, line 56
def self.could_not_find(value, attribute = 'id', model = model_name.downcase)
        "Could not find #{model} of #{attribute} #{value}!"
end
create(request, params, post_data = nil) click to toggle source

Public: Process a creation request (HTTP POST)

If `post_data` is an Array, iterates through the array and calls itself with each element within. Otherwise, performs the creation using the attribute key-value pairings within the `post_data`.

request - The internal Sinatra request object that is available to

request handling.

params - The internal params Hash that is available to request

handling.

post_data - The data that was sent in the request body.

Returns an Array, a formatted response that can be passed back through

Sinatra's request processing.
# File lib/frecon/controller.rb, line 96
def self.create(request, params, post_data = nil)
        post_data ||= process_json_request request

        if post_data.is_an? Array
                results = post_data.map do |post_data_item|
                        begin
                                self.create(nil, nil, post_data_item)
                        rescue RequestError => e
                                e.return_value
                        end
                end

                status_code = 201

                if(results.map do |result|
                           result.is_an?(Array) ? result[0] : 422
                   end.select do |status_code|
                           status_code != 201
                   end.count > 0)

                        status_code = 422
                end

                [status_code, results.to_json]
        else
                @model = model.new
                @model.attributes = post_data

                if @model.save
                        [201, @model.to_json]
                else
                        raise RequestError.new(422, @model.errors.full_messages, {params: params, post_data: post_data})
                end
        end
end
delete(params) click to toggle source

Public: Process a deletion request (HTTP DELETE)

Processes the JSON request, finds the model, then deletes it.

request - The internal Sinatra request object that is available to

request handling.

params - The internal params Hash that is available to request

handling.

post_data - The data that was sent in the request body.

Returns 204 if successful. Raises a RequestError if the request is malformed or if the model can't be

destroyed
# File lib/frecon/controller.rb, line 174
def self.delete(params)
        @model = find_model params

        if @model
                if @model.destroy
                        204
                else
                        raise RequestError.new(422, @model.errors.full_messages)
                end
        else
                raise RequestError.new(404, could_not_find(params[:id]), {params: params})
        end
end
find_model(params) click to toggle source

Public: Find a model.

params - A Hash containing the parameters. This should contain an

'id' key, which is deleted and used for the find.

Returns either the found model value or nil.

# File lib/frecon/controller.rb, line 45
def self.find_model(params)
        model.find params.delete('id')
end
index(params) click to toggle source

Public: Process an index request (HTTP GET) for all instances of a model.

Processes the JSON request, and returns a filtered list of all of the models.

request - The internal Sinatra request object that is available to

request handling.

params - The internal params Hash that is available to request

handling.

post_data - The data that was sent in the request body.

Returns a String with the JSON representation of the list of models. Raises a RequestError if the request is malformed.

# File lib/frecon/controller.rb, line 225
def self.index(params)
        if params.empty?
                @models = model.all
        else
                params.delete('splat')
                params.delete('captures')

                @models = model.all.psv_filter(params)
        end

        @models.to_json
end
model() click to toggle source

Public: Converts the class's name to a Model.

Returns the Model's class.

# File lib/frecon/controller.rb, line 33
def self.model
        # Removes the trailing 'Controller' from the class name,
        # singularizes the result, and turns it into the class.
        self.name.gsub(/Controller\Z/, '').singularize.constantize
end
model_name() click to toggle source

Public: Converts the class's name to a Model name.

Returns a Symbol that is the Model name.

# File lib/frecon/controller.rb, line 24
def self.model_name
        # Removes the namespace 'FReCon::' and 'Controller' from
        # the class name, then singularizes the result.
        self.name.gsub(/FReCon::|Controller\Z/, '').singularize
end
process_json_request(request) click to toggle source

Public: Process a JSON request.

request - The internal Sinatra request object that is available to

request handling.

Returns a Hash corresponding to the request's body. Raises a RequestError if the JSON parse fails.

# File lib/frecon/controller.rb, line 67
def self.process_json_request(request)
        # Rewind the request body (an IO object)
        # in case someone else has already played
        # through it.
        request.body.rewind

        begin
                post_data = JSON.parse(request.body.read)
        rescue JSON::ParserError => e
                raise RequestError.new(400, e.message)
        end

        post_data
end
show(params) click to toggle source

Public: Process a show request (HTTP GET) for a specific instance of a model.

Processes the JSON request, finds the model, then shows it.

request - The internal Sinatra request object that is available to

request handling.

params - The internal params Hash that is available to request

handling.

post_data - The data that was sent in the request body.

Returns a String with the JSON representation of the model. Raises a RequestError if the request is malformed or if the model can't be

found.
# File lib/frecon/controller.rb, line 202
def self.show(params)
        @model = find_model params

        if @model
                @model.to_json
        else
                raise RequestError.new(404, could_not_find(params[:id]), {params: params})
        end
end
update(request, params, post_data = nil) click to toggle source

Public: Process an update request (HTTP PUT)

Processes the JSON request, finds the model, then updates it.

request - The internal Sinatra request object that is available to

request handling.

params - The internal params Hash that is available to request

handling.

post_data - The data that was sent in the request body.

Returns a String with the JSON representation of the model. Raises a RequestError if the request is malformed or if the attributes

can't be updated.
# File lib/frecon/controller.rb, line 145
def self.update(request, params, post_data = nil)
        raise RequestError.new(400, "Must supply a #{model_name.downcase} id!") unless params[:id]

        post_data ||= process_json_request request

        @model = find_model params

        raise RequestError.new(404, could_not_find(params[:id])) unless @model

        if @model.update_attributes(post_data)
                @model.to_json
        else
                raise RequestError.new(422, @model.errors.full_messages, {params: params, post_data: post_data})
        end
end