Class: FReCon::Controller

Inherits:
Object show all
Defined in:
lib/frecon/controller.rb

Overview

Public: A base class to represent a controller.

Class Method Summary collapse

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.



50
51
52
# File 'lib/frecon/controller.rb', line 50

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.


90
91
92
93
94
95
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
# File 'lib/frecon/controller.rb', line 90

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) ⇒ 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


168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/frecon/controller.rb', line 168

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) ⇒ 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.



39
40
41
# File 'lib/frecon/controller.rb', line 39

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.



219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/frecon/controller.rb', line 219

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

.modelObject

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

Returns the Model’s class.



27
28
29
30
31
# File 'lib/frecon/controller.rb', line 27

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_nameObject

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

Returns a Symbol that is the Model name.



18
19
20
21
22
# File 'lib/frecon/controller.rb', line 18

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.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/frecon/controller.rb', line 61

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) ⇒ 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.


196
197
198
199
200
201
202
203
204
# File 'lib/frecon/controller.rb', line 196

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.

Raises:



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/frecon/controller.rb', line 139

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