Class: FReCon::Controller

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

Class Method Summary collapse

Class Method Details

.could_not_find(value, attribute = "id", model = model_name.downcase) ⇒ Object

The 404 error message.



33
34
35
# File 'lib/frecon/controller.rb', line 33

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



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/frecon/controller.rb', line 52

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



104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/frecon/controller.rb', line 104

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

Some models have to find themselves in special ways, so this can be overridden with those ways.



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

def self.find_model(params)
  model.find params[:id]
end

.index(params) ⇒ Object



128
129
130
131
132
133
134
# File 'lib/frecon/controller.rb', line 128

def self.index(params)
  params.delete("_")

  @models = params.empty? ? model.all : model.where(params)

  @models.to_json
end

.match_number_and_competition_to_match_id(post_data) ⇒ Object

This supports match_number and competition_name or match_number and competition (which is a Hash).



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/frecon/controller.rb', line 164

def self.match_number_and_competition_to_match_id(post_data)
  return post_data unless post_data.is_a?(Hash)

  if post_data["match_number"] && !post_data["match_id"]
    if post_data["competition_name"] && (competition = Competition.find_by name: post_data["competition_name"])
      # Try to set the match to the already existing match.
      begin
        match = competition.matches.find_by number: post_data["match_number"]
      rescue ArgumentError, TypeError => e
        raise RequestError.new(422, e.message, {post_data: post_data})
      end

      # Create the match if necessary.
      begin
        match ||= Match.create(number: post_data["match_number"], competition_id: competition.id)
      rescue ArgumentError, TypeError => e
        raise RequestError.new(422, e.message, {post_data: post_data, competition_id: competition.id})
      end

      post_data["match_id"] = match.id

      post_data.delete("match_number")
      post_data.delete("competition_name")

      post_data
    elsif post_data["competition"] && post_data["competition"]["_id"] && (competition = Competition.find_by(id: post_data["competition"]["_id"]))
      # Try to set the match to the already existing match.
      match = competition.matches.find_by number: post_data["match_number"]

      # Create the match if necessary.
      begin
        match ||= Match.create(number: post_data["match_number"], competition_id: competition.id)
      rescue ArgumentError, TypeError => e
        raise RequestError.new(422, e.message, {post_data: post_data, competition_id: competition.id})
      end

      post_data["match_id"] = match.id

      post_data.delete("match_number")
      post_data.delete("competition")

      post_data
    else
      raise RequestError.new(422, "A current competition is not set.  Please set it.", {post_data: post_data})
    end
  end
end

.modelObject



20
21
22
23
24
# File 'lib/frecon/controller.rb', line 20

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



14
15
16
17
18
# File 'lib/frecon/controller.rb', line 14

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



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/frecon/controller.rb', line 37

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



118
119
120
121
122
123
124
125
126
# File 'lib/frecon/controller.rb', line 118

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

.show_attribute(params, attribute) ⇒ Object



136
137
138
139
140
141
142
143
144
# File 'lib/frecon/controller.rb', line 136

def self.show_attribute(params, attribute)
  @model = find_model params

  if @model
    @model.send(attribute).to_json
  else
    raise RequestError.new(404, could_not_find(params[:id]), {params: params, attribute: attribute})
  end
end

.team_number_to_team_id(post_data) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/frecon/controller.rb', line 146

def self.team_number_to_team_id(post_data)
  return post_data unless post_data.is_a?(Hash)

  if post_data["team_number"] && !post_data["team_id"]
    unless (team = Team.number post_data["team_number"]).nil?
      post_data["team_id"] = team.id

      post_data.delete("team_number")

      post_data
    else
      raise RequestError.new(404, could_not_find(post_data["team_number"], "number", "team"), {post_data: post_data})
    end
  end
end

.update(request, params, post_data = nil) ⇒ Object

Raises:



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/frecon/controller.rb', line 88

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