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.



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

def self.could_not_find(value, attribute = "id", model = model_name.downcase)
  "Could not find #{model_name.downcase} of #{attribute} #{value}!"
end

.create(request, params) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/frecon/controller.rb', line 51

def self.create(request, params)
  post_data = process_request request
  return post_data if post_data.is_an?(Array)

  @model = model.new
  @model.attributes = post_data

  if @model.save
    [201, @model.to_json]
  else
    [422, ErrorFormatter.format(@model.errors.full_messages)]
  end
end

.delete(params) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/frecon/controller.rb', line 82

def self.delete(params)
  @model = model.find params[:id]

  if @model
    if @model.destroy
      204
    else
      [422, ErrorFormatter.format(@model.errors.full_messages)]
    end
  else
    [404, ErrorFormatter.format(could_not_find(params[:id]))]
  end
end

.index(params) ⇒ Object



106
107
108
109
110
111
112
# File 'lib/frecon/controller.rb', line 106

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

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

  @models.to_json
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_request(request) ⇒ Object

Processes a POST/PUT request and returns the post data.



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

def self.process_request(request)
  # Rewind the request body (an IO object)
  # in case someone else has already played
  # through it.
  request.body.rewind

  begin
    # Parse the POST data as a JSON hash
    # (because that's what it is)
    post_data = JSON.parse(request.body.read)
  rescue JSON::ParserError => e
    # If we have malformed JSON (JSON::ParserError is
    # raised), escape out of the function.
    return [400, ErrorFormatter.format(e.message)]
  end

  post_data.is_an?(Array) ? [422, ErrorFormatter.format("Must pass a JSON object!")] : post_data
end

.show(params) ⇒ Object



96
97
98
99
100
101
102
103
104
# File 'lib/frecon/controller.rb', line 96

def self.show(params)
  @model = model.find params[:id]

  if @model
    @model.to_json
  else
    [404, ErrorFormatter.format(could_not_find(params[:id]))]
  end
end

.show_attribute(params, attribute) ⇒ Object



114
115
116
117
118
119
120
121
122
# File 'lib/frecon/controller.rb', line 114

def self.show_attribute(params, attribute)
  @model = model.find params[:id]

  if @model
    @model.send(attribute).to_json
  else
    [404, ErrorFormatter.format(could_not_find(params[:id]))]
  end
end

.team_number_to_team_id(post_data) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/frecon/controller.rb', line 124

def self.team_number_to_team_id(post_data)
  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
      return [404, ErrorFormatter.format(could_not_find(post_data["team_number"], "number", "team"))]
    end
  end
end

.update(request, params) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/frecon/controller.rb', line 65

def self.update(request, params)
  return [400, "Must supply a #{model_name.downcase}!"] unless params[:id]

  post_data = process_request request
  return post_data if post_data.is_an?(Array)

  @model = model.find params[:id]

  return [404, ErrorFormatter.format(could_not_find(params[:id]))] unless @model

  if @model.update_attributes(post_data)
    @model.to_json
  else
    [422, ErrorFormatter.format(@model.errors.full_messages)]
  end
end