frecon 0.1.4 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1753ce0da52b68eee609e44cdd93d62a063d6728
4
- data.tar.gz: dc9277e50cf48a1cff8b1429151b810b05b80712
3
+ metadata.gz: 81f4fffe7a3472db021811019f754872f050fe01
4
+ data.tar.gz: 36accb11f3e90cf5a10a76bf42d178609afb5f80
5
5
  SHA512:
6
- metadata.gz: 3a07b211ec464b371ab2cfd5b0cbe3809ecd78318054929d680e0739139d02004cbf056271f40fc486a827a445fccf6ff768ef10555c999cd044499d44ea197d
7
- data.tar.gz: f4e2d91ad1766d12cfb27c3dadc0762f454fd2f03233ed85cedee66cab8b929dd0b7379133f6e46061029722065f20536606b730b37fb2aefa92776b552d55c0
6
+ metadata.gz: d45db224b7de1a264ae077633672d792b1570df83ef9ff514ded0db86fe69f28d975f45f79376733215500a60de1f2bb37215993ad80fefc2ac5c39e36c824bd
7
+ data.tar.gz: 273a7d1ee9d0e7556f6f74de074e8c384e9c5e784053942f5516d81c47fb6e065a3394a6103c879b964b0eb316ef04aad34ea98a6695d85c16e4ece665c89616
data/bin/frecon CHANGED
@@ -10,8 +10,8 @@
10
10
  # <http://opensource.org/licenses/MIT>.
11
11
 
12
12
  # Include rel-path ../lib/ in the $LOAD_PATH if it's not there already.
13
- lib_directory = File.join(File.dirname(__FILE__), "..", "lib")
14
- $LOAD_PATH.unshift(lib_directory) unless $LOAD_PATH.include?(lib_directory)
13
+ lib_directory = File.expand_path(File.join(File.dirname(__FILE__), "..", "lib"))
14
+ $LOAD_PATH.unshift(lib_directory) unless $LOAD_PATH.map { |directory| File.expand_path(directory) }.include?(lib_directory)
15
15
 
16
16
  require "frecon"
17
17
 
@@ -8,7 +8,7 @@
8
8
  # <http://opensource.org/licenses/MIT>.
9
9
 
10
10
  module FReCon
11
- VERSION = "0.1.4"
11
+ VERSION = "0.2.0"
12
12
 
13
13
  @environment_variable = :development
14
14
 
@@ -23,13 +23,19 @@ module FReCon
23
23
  self.name.gsub(/Controller\Z/, "").singularize.constantize
24
24
  end
25
25
 
26
+ # Some models have to find themselves in special ways,
27
+ # so this can be overridden with those ways.
28
+ def self.find_model(params)
29
+ model.find params[:id]
30
+ end
31
+
26
32
  # The 404 error message.
27
33
  def self.could_not_find(value, attribute = "id", model = model_name.downcase)
28
- "Could not find #{model_name.downcase} of #{attribute} #{value}!"
34
+ "Could not find #{model} of #{attribute} #{value}!"
29
35
  end
30
36
 
31
37
  # Processes a POST/PUT request and returns the post data.
32
- def self.process_request(request)
38
+ def self.process_json_object_request(request)
33
39
  # Rewind the request body (an IO object)
34
40
  # in case someone else has already played
35
41
  # through it.
@@ -49,8 +55,8 @@ module FReCon
49
55
  post_data
50
56
  end
51
57
 
52
- def self.create(request, params)
53
- post_data = process_request request
58
+ def self.create(request, params, post_data = nil)
59
+ post_data ||= process_json_object_request request
54
60
 
55
61
  @model = model.new
56
62
  @model.attributes = post_data
@@ -61,13 +67,13 @@ module FReCon
61
67
  raise RequestError.new(422, @model.errors.full_messages)
62
68
  end
63
69
  end
70
+
71
+ def self.update(request, params, post_data = nil)
72
+ raise RequestError.new(400, "Must supply a #{model_name.downcase} id!") unless params[:id]
64
73
 
65
- def self.update(request, params)
66
- raise RequestError.new(400, "Must supply a #{model_name.downcase}!") unless params[:id]
74
+ post_data ||= process_json_object_request request
67
75
 
68
- post_data = process_request request
69
-
70
- @model = model.find params[:id]
76
+ @model = find_model params
71
77
 
72
78
  raise RequestError.new(404, could_not_find(params[:id])) unless @model
73
79
 
@@ -79,7 +85,7 @@ module FReCon
79
85
  end
80
86
 
81
87
  def self.delete(params)
82
- @model = model.find params[:id]
88
+ @model = find_model params
83
89
 
84
90
  if @model
85
91
  if @model.destroy
@@ -93,7 +99,7 @@ module FReCon
93
99
  end
94
100
 
95
101
  def self.show(params)
96
- @model = model.find params[:id]
102
+ @model = find_model params
97
103
 
98
104
  if @model
99
105
  @model.to_json
@@ -111,7 +117,7 @@ module FReCon
111
117
  end
112
118
 
113
119
  def self.show_attribute(params, attribute)
114
- @model = model.find params[:id]
120
+ @model = find_model params
115
121
 
116
122
  if @model
117
123
  @model.send(attribute).to_json
@@ -133,5 +139,49 @@ module FReCon
133
139
  end
134
140
  end
135
141
  end
142
+
143
+ # This supports match_number and competition_name
144
+ # or match_number and competition (which is a Hash).
145
+ def self.match_number_and_competition_to_match_id(post_data)
146
+ if post_data["match_number"] && !post_data["match_id"]
147
+ if post_data["competition_name"] && (competition = Competition.find_by name: post_data["competition_name"])
148
+ # Try to set the match to the already existing match.
149
+ begin
150
+ match = competition.matches.find_by number: post_data["match_number"]
151
+ rescue ArgumentError, TypeError => e
152
+ raise RequestError.new(422, e.message)
153
+ end
154
+
155
+ # Create the match if necessary.
156
+ begin
157
+ match ||= Match.create(number: post_data["match_number"], competition_id: competition.id)
158
+ rescue ArgumentError, TypeError => e
159
+ raise RequestError.new(422, e.message)
160
+ end
161
+
162
+ post_data["match_id"] = match.id
163
+
164
+ post_data.delete("match_number")
165
+ post_data.delete("competition_name")
166
+ elsif post_data["competition"] && post_data["competition"]["_id"] && post_data["competition"]["_id"]["$oid"] && (competition = Competition.find_by(id: post_data["competition"]["_id"]["$oid"]))
167
+ # Try to set the match to the already existing match.
168
+ match = competition.matches.find_by number: post_data["match_number"]
169
+
170
+ # Create the match if necessary.
171
+ begin
172
+ match ||= Match.create(number: post_data["match_number"], competition_id: competition.id)
173
+ rescue ArgumentError, TypeError => e
174
+ raise RequestError.new(422, e.message)
175
+ end
176
+
177
+ post_data["match_id"] = match.id
178
+
179
+ post_data.delete("match_number")
180
+ post_data.delete("competition")
181
+ else
182
+ raise RequestError.new(422, "A current competition is not set. Please set it.")
183
+ end
184
+ end
185
+ end
136
186
  end
137
187
  end
@@ -13,17 +13,13 @@ require "frecon/models"
13
13
  module FReCon
14
14
  class DumpController
15
15
  def self.full(params)
16
- competitions = Competition.all
17
- teams = Team.all
18
- matches = Match.all
19
- records = Record.all
16
+ dump = {}
20
17
 
21
- {
22
- "competitions" => competitions,
23
- "teams" => teams,
24
- "matches" => matches,
25
- "records" => records
26
- }.to_json
18
+ Model.descendants.each do |child|
19
+ dump[child.name.gsub(/FReCon::/, "").downcase.pluralize] = child.all
20
+ end
21
+
22
+ dump.to_json
27
23
  end
28
24
  end
29
25
  end
@@ -10,25 +10,13 @@
10
10
  module FReCon
11
11
  class ParticipationsController < Controller
12
12
  def self.create(request, params)
13
- post_data = process_request request
14
-
15
- # Convert team number to team_id.
16
- post_data = team_number_to_team_id(post_data)
17
-
18
- @model = model.new
19
- @model.attributes = post_data
20
-
21
- if @model.save
22
- [201, @model.to_json]
23
- else
24
- raise RequestError.new(422, @model.errors.full_messages)
25
- end
13
+ super(request, params, team_number_to_team_id(process_json_object_request(request)))
26
14
  end
27
-
15
+
28
16
  def self.competition(params)
29
17
  show_attribute params, :competition
30
18
  end
31
-
19
+
32
20
  def self.team(params)
33
21
  show_attribute params, :team
34
22
  end
@@ -14,62 +14,7 @@ require "frecon/models"
14
14
  module FReCon
15
15
  class RecordsController < Controller
16
16
  def self.create(request, params)
17
- post_data = process_request request
18
-
19
- # Change special post_data attributes.
20
- # Convert team number to team id.
21
- post_data = team_number_to_team_id(post_data)
22
-
23
- # Convert match number and competition name to match id.
24
- if post_data["match_number"] && !post_data["match_id"]
25
- if post_data["competition_name"] && (competition = Competition.find_by name: post_data["competition_name"])
26
- # Try to set the match to the already existing match.
27
- begin
28
- match = competition.matches.find_by number: post_data["match_number"]
29
- rescue ArgumentError, TypeError => e
30
- raise RequestError.new(422, e.message)
31
- end
32
-
33
- # Create the match if necessary.
34
- begin
35
- match ||= Match.create(number: post_data["match_number"], competition_id: competition.id)
36
- rescue ArgumentError, TypeError => e
37
- raise RequestError.new(422, e.message)
38
- end
39
-
40
- post_data["match_id"] = match.id
41
-
42
- post_data.delete("match_number")
43
- post_data.delete("competition_name")
44
- elsif post_data["competition"] && post_data["competition"]["_id"] && post_data["competition"]["_id"]["$oid"] && (competition = Competition.find_by(id: post_data["competition"]["_id"]["$oid"]))
45
- # Try to set the match to the already existing match.
46
- match = competition.matches.find_by number: post_data["match_number"]
47
-
48
- # Create the match if necessary.
49
- begin
50
- match ||= Match.create(number: post_data["match_number"], competition_id: competition.id)
51
- rescue ArgumentError, TypeError => e
52
- raise RequestError.new(422, e.message)
53
- end
54
-
55
- post_data["match_id"] = match.id
56
-
57
- post_data.delete("match_number")
58
- post_data.delete("competition")
59
- else
60
- raise RequestError.new(422, "A current competition is not set. Please set it.")
61
- end
62
- end
63
-
64
- @record = Record.new
65
- @record.attributes = post_data
66
-
67
- if @record.save
68
- # Use to_json for now; we can filter it later.
69
- [201, @record.to_json]
70
- else
71
- raise RequestError.new(422, @record.errors.full_messages)
72
- end
17
+ super(request, params, match_number_and_competition_to_match_id(team_number_to_team_id(process_json_object_request(request))))
73
18
  end
74
19
 
75
20
  def self.competition(params)
@@ -12,6 +12,10 @@ require "frecon/models/robot"
12
12
 
13
13
  module FReCon
14
14
  class RobotsController < Controller
15
+ def self.create(request, params)
16
+ super(request, params, team_number_to_team_id(process_json_object_request(request)))
17
+ end
18
+
15
19
  def self.competition(params)
16
20
  show_attribute params, :competition
17
21
  end
@@ -13,61 +13,23 @@ require "frecon/models"
13
13
 
14
14
  module FReCon
15
15
  class TeamsController < Controller
16
- def self.create(request, params)
17
- post_data = process_request request
18
-
19
- @team = Team.new
20
- @team.attributes = post_data
21
-
22
- if @team.save
23
- # Use to_json for now; we can filter it later.
24
- [201, @team.to_json]
25
- else
26
- raise RequestError.new(422, @team.errors.full_messages)
27
- end
28
- end
29
-
30
- def self.update(request, params)
31
- raise RequestError.new(400, "Must supply a team number!") unless params[:number]
32
-
33
- post_data = process_request request
34
-
35
- @team = Team.find_by number: params[:number]
36
- raise RequestError.new(404, could_not_find(params[:number], "number")) if @team.nil?
37
-
38
- if @team.update_attributes(post_data)
39
- @team.to_json
40
- else
41
- raise RequestError.new(422, @team.errors.full_messages)
42
- end
16
+ # The `id` param will be a number or id.
17
+ def self.find_model(params)
18
+ (Team.find_by id: params[:id]) || (Team.find_by number: params[:id])
43
19
  end
44
20
 
45
- def self.delete(params)
46
- @team = Team.find_by number: params[:number]
47
-
48
- if @team
49
- if @team.destroy
50
- 204
51
- else
52
- raise RequestError.new(422, @team.errors.full_messages)
53
- end
54
- else
55
- raise RequestError.new(404, could_not_find(params[:number], "number"))
56
- end
57
- end
58
-
59
- def self.show(params)
60
- @team = Team.find_by number: params[:number]
61
-
62
- if @team
63
- @team.to_json
21
+ # Since Team has a special way of finding itself, we can make
22
+ # the error message reflect this.
23
+ def self.could_not_find(value, attribute = "id", model = model_name.downcase)
24
+ if attribute == "id" && model == "team"
25
+ "Could not find team of id or number #{value}!"
64
26
  else
65
- raise RequestError.new(404, could_not_find(params[:number], "number"))
27
+ "Could not find #{model} of #{attribute} #{value}!"
66
28
  end
67
29
  end
68
30
 
69
31
  def self.records(params)
70
- @team = Team.find_by number: params[:number]
32
+ @team = find_model params
71
33
 
72
34
  if @team
73
35
  if params[:competition_id]
@@ -82,12 +44,12 @@ module FReCon
82
44
  @team.records.to_json
83
45
  end
84
46
  else
85
- raise RequestError.new(404, could_not_find(params[:number], "number"))
47
+ raise RequestError.new(404, could_not_find(params[:id], "id or number"))
86
48
  end
87
49
  end
88
50
 
89
51
  def self.matches(params)
90
- @team = Team.find_by number: params[:number]
52
+ @team = find_model params
91
53
 
92
54
  if @team
93
55
  # Ensure that the competition ID is valid.
@@ -99,17 +61,17 @@ module FReCon
99
61
 
100
62
  @team.matches(params[:competition_id]).to_json
101
63
  else
102
- raise RequestError.new(404, could_not_find(params[:number], "number"))
64
+ raise RequestError.new(404, could_not_find(params[:id], "id or number"))
103
65
  end
104
66
  end
105
67
 
106
68
  def self.competitions(params)
107
- @team = Team.find_by number: params[:number]
69
+ @team = find_model params
108
70
 
109
71
  if @team
110
72
  @team.competitions.to_json
111
73
  else
112
- raise RequestError.new(404, could_not_find(params[:number], "number"))
74
+ raise RequestError.new(404, could_not_find(params[:id], "id or number"))
113
75
  end
114
76
  end
115
77
  end
@@ -28,11 +28,13 @@ module FReCon
28
28
  Mongoid.load!(File.join(File.dirname(__FILE__), "mongoid.yml"), environment)
29
29
  end
30
30
 
31
- Mongoid.logger.level = Logger::DEBUG
32
- Mongoid.logger = Logger.new($stdout)
31
+ if environment == :development
32
+ Mongoid.logger.level = Logger::DEBUG
33
+ Mongoid.logger = Logger.new($stdout)
33
34
 
34
- Moped.logger.level = Logger::DEBUG
35
- Moped.logger = Logger.new($stdout)
35
+ Moped.logger.level = Logger::DEBUG
36
+ Moped.logger = Logger.new($stdout)
37
+ end
36
38
  end
37
39
  end
38
40
  end
data/lib/frecon/model.rb CHANGED
@@ -21,6 +21,11 @@ module FReCon
21
21
  end
22
22
  end
23
23
 
24
+ def self.descendants
25
+ # Basically lists all of the models in this database.
26
+ ObjectSpace.each_object(Class).select { |possibleChild| possibleChild < self }
27
+ end
28
+
24
29
  def no_invalid_relations
25
30
  # Get all of the belongs_to fields (ends with "_id" and not "_id" because that is the id).
26
31
  attributes.keys.select { |attribute| attribute.end_with?("_id") && attribute != "_id" }.each do |relation|
@@ -11,9 +11,13 @@ require "frecon/model"
11
11
 
12
12
  module FReCon
13
13
  class Robot < Model
14
+ # This is an optional field we included for organization.
14
15
  field :name, type: String
15
16
 
16
17
  belongs_to :competition
17
18
  belongs_to :team
19
+
20
+ validates :competition_id, :team_id, presence: true
21
+ validates :team_id, uniqueness: { scope: :competition_id }
18
22
  end
19
23
  end
@@ -26,4 +26,13 @@ class RequestError < StandardError
26
26
  JSON.generate({ errors: message })
27
27
  end
28
28
  end
29
+
30
+ # A Sinatra-compliant return value.
31
+ def return_value
32
+ if @message
33
+ [@code, @message]
34
+ else
35
+ @code
36
+ end
37
+ end
29
38
  end
data/lib/frecon/routes.rb CHANGED
@@ -17,7 +17,7 @@ module FReCon
17
17
  begin
18
18
  controller.create request, params
19
19
  rescue RequestError => e
20
- [e.code, e.message]
20
+ e.return_value
21
21
  end
22
22
  end
23
23
  end
@@ -27,7 +27,7 @@ module FReCon
27
27
  begin
28
28
  controller.update request, params
29
29
  rescue RequestError => e
30
- [e.code, e.message]
30
+ e.return_value
31
31
  end
32
32
  end
33
33
  end
@@ -37,7 +37,7 @@ module FReCon
37
37
  begin
38
38
  controller.delete params
39
39
  rescue RequestError => e
40
- [e.code, e.message]
40
+ e.return_value
41
41
  end
42
42
  end
43
43
  end
@@ -47,7 +47,7 @@ module FReCon
47
47
  begin
48
48
  controller.show params
49
49
  rescue RequestError => e
50
- [e.code, e.message]
50
+ e.return_value
51
51
  end
52
52
  end
53
53
  end
@@ -57,60 +57,36 @@ module FReCon
57
57
  begin
58
58
  controller.index params
59
59
  rescue RequestError => e
60
- [e.code, e.message]
60
+ e.return_value
61
61
  end
62
62
  end
63
63
  end
64
64
  end
65
65
 
66
66
  def self.included(base)
67
- resource_routes base, "teams", TeamsController, [:create, :index]
67
+ resource_routes base, "teams", TeamsController
68
68
 
69
- base.put "/teams/:number" do
70
- begin
71
- TeamsController.update request, params
72
- rescue RequestError => e
73
- [e.code, e.message]
74
- end
75
- end
76
-
77
- base.delete "/teams/:number" do
78
- begin
79
- TeamsController.delete params
80
- rescue RequestError => e
81
- [e.code, e.message]
82
- end
83
- end
84
-
85
- base.get "/teams/:number" do
86
- begin
87
- TeamsController.show params
88
- rescue RequestError => e
89
- [e.code, e.message]
90
- end
91
- end
92
-
93
- base.get "/teams/:number/records/?:competition_id?" do
69
+ base.get "/teams/:id/records/?:competition_id?" do
94
70
  begin
95
71
  TeamsController.records params
96
72
  rescue RequestError => e
97
- [e.code, e.message]
73
+ e.return_value
98
74
  end
99
75
  end
100
76
 
101
- base.get "/teams/:number/matches/?:competition_id?" do
77
+ base.get "/teams/:id/matches/?:competition_id?" do
102
78
  begin
103
79
  TeamsController.matches params
104
80
  rescue RequestError => e
105
- [e.code, e.message]
81
+ e.return_value
106
82
  end
107
83
  end
108
84
 
109
- base.get "/teams/:number/competitions" do
85
+ base.get "/teams/:id/competitions" do
110
86
  begin
111
87
  TeamsController.competitions params
112
88
  rescue RequestError => e
113
- [e.code, e.message]
89
+ e.return_value
114
90
  end
115
91
  end
116
92
 
@@ -120,7 +96,7 @@ module FReCon
120
96
  begin
121
97
  CompetitionsController.teams params
122
98
  rescue RequestError => e
123
- [e.code, e.message]
99
+ e.return_value
124
100
  end
125
101
  end
126
102
 
@@ -128,7 +104,7 @@ module FReCon
128
104
  begin
129
105
  CompetitionsController.matches params
130
106
  rescue RequestError => e
131
- [e.code, e.message]
107
+ e.return_value
132
108
  end
133
109
  end
134
110
 
@@ -136,7 +112,7 @@ module FReCon
136
112
  begin
137
113
  CompetitionsController.records params
138
114
  rescue RequestError => e
139
- [e.code, e.message]
115
+ e.return_value
140
116
  end
141
117
  end
142
118
 
@@ -146,7 +122,7 @@ module FReCon
146
122
  begin
147
123
  MatchesController.records params
148
124
  rescue RequestError => e
149
- [e.code, e.message]
125
+ e.return_value
150
126
  end
151
127
  end
152
128
 
@@ -154,7 +130,7 @@ module FReCon
154
130
  begin
155
131
  MatchesController.competition params
156
132
  rescue RequestError => e
157
- [e.code, e.message]
133
+ e.return_value
158
134
  end
159
135
  end
160
136
 
@@ -164,7 +140,7 @@ module FReCon
164
140
  begin
165
141
  RecordsController.competition params
166
142
  rescue RequestError => e
167
- [e.code, e.message]
143
+ e.return_value
168
144
  end
169
145
  end
170
146
 
@@ -174,7 +150,7 @@ module FReCon
174
150
  begin
175
151
  RobotsController.competition params
176
152
  rescue RequestError => e
177
- [e.code, e.message]
153
+ e.return_value
178
154
  end
179
155
  end
180
156
 
@@ -182,7 +158,7 @@ module FReCon
182
158
  begin
183
159
  RobotsController.team params
184
160
  rescue RequestError => e
185
- [e.code, e.message]
161
+ e.return_value
186
162
  end
187
163
  end
188
164
 
@@ -192,7 +168,7 @@ module FReCon
192
168
  begin
193
169
  ParticipationsController.competition params
194
170
  rescue RequestError => e
195
- [e.code, e.message]
171
+ e.return_value
196
172
  end
197
173
  end
198
174
 
@@ -200,7 +176,7 @@ module FReCon
200
176
  begin
201
177
  ParticipationsController.team params
202
178
  rescue RequestError => e
203
- [e.code, e.message]
179
+ e.return_value
204
180
  end
205
181
  end
206
182
 
@@ -208,7 +184,7 @@ module FReCon
208
184
  begin
209
185
  DumpController.full params
210
186
  rescue RequestError => e
211
- [e.code, e.message]
187
+ e.return_value
212
188
  end
213
189
  end
214
190
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: frecon
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sam Craig