Class: FReCon::Controller
Overview
Public: A base class to represent a controller.
Direct Known Subclasses
CompetitionsController, MatchesController, ParticipationsController, RecordsController, RobotsController, TeamsController
Class Method Summary collapse
-
.could_not_find(value, attribute = 'id', model = model_name.downcase) ⇒ Object
Public: Generate a could-not-find message.
-
.create(request, params, post_data = nil) ⇒ Object
Public: Process a creation request (HTTP POST).
-
.delete(params) ⇒ Object
Public: Process a deletion request (HTTP DELETE).
-
.find_model(params) ⇒ Object
Public: Find a model.
-
.index(params) ⇒ Object
Public: Process an index request (HTTP GET) for all instances of a model.
-
.model ⇒ Object
Public: Converts the class’s name to a Model.
-
.model_name ⇒ Object
Public: Converts the class’s name to a Model name.
-
.process_json_request(request) ⇒ Object
Public: Process a JSON request.
-
.show(params) ⇒ Object
Public: Process a show request (HTTP GET) for a specific instance of a model.
-
.update(request, params, post_data = nil) ⇒ Object
Public: Process an update request (HTTP PUT).
Class Method Details
.could_not_find(value, attribute = 'id', model = model_name.downcase) ⇒ Object
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.
56 57 58 |
# 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) ⇒ Object
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.
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# 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., {params: params, post_data: post_data}) end end end |
.delete(params) ⇒ Object
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
174 175 176 177 178 179 180 181 182 183 184 185 186 |
# 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.) end else raise RequestError.new(404, could_not_find(params[:id]), {params: params}) end end |
.find_model(params) ⇒ Object
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.
45 46 47 |
# File 'lib/frecon/controller.rb', line 45 def self.find_model(params) model.find params.delete('id') end |
.index(params) ⇒ Object
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.
225 226 227 228 229 230 231 232 233 234 235 236 |
# 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 ⇒ Object
Public: Converts the class’s name to a Model.
Returns the Model’s class.
33 34 35 36 37 |
# 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 ⇒ Object
Public: Converts the class’s name to a Model name.
Returns a Symbol that is the Model name.
24 25 26 27 28 |
# 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) ⇒ Object
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.
67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# 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.) end post_data end |
.show(params) ⇒ Object
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.
202 203 204 205 206 207 208 209 210 |
# 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) ⇒ Object
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.
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# 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., {params: params, post_data: post_data}) end end |