frecon 0.1.3 → 0.1.4

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: 0c2b4116037363c5580518c012f3f9f539928742
4
- data.tar.gz: 25724f6f99f6a82ff6d559c77e4ef35e079091ff
3
+ metadata.gz: 1753ce0da52b68eee609e44cdd93d62a063d6728
4
+ data.tar.gz: dc9277e50cf48a1cff8b1429151b810b05b80712
5
5
  SHA512:
6
- metadata.gz: 46530c063a1616ea0e691a5fcf85c2bad581ca936a7bdee6ce5180084574b6387141fea2e5fc9106b43d82e1295464da897f65dfc37371e3ec182ac9b99d7fce
7
- data.tar.gz: 843fba889e81bf13534267dc43d929a919f891ca5d7dcaa6705b0c802456edef05c6378bfa021fae0ceba9eb27a16aba6fde10545846a4a0bdfdb809abe9b814
6
+ metadata.gz: 3a07b211ec464b371ab2cfd5b0cbe3809ecd78318054929d680e0739139d02004cbf056271f40fc486a827a445fccf6ff768ef10555c999cd044499d44ea197d
7
+ data.tar.gz: f4e2d91ad1766d12cfb27c3dadc0762f454fd2f03233ed85cedee66cab8b929dd0b7379133f6e46061029722065f20536606b730b37fb2aefa92776b552d55c0
data/bin/frecon CHANGED
@@ -10,7 +10,7 @@
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.class_eval('expand_path(join(dirname(__FILE__), "..", "lib"))')
13
+ lib_directory = File.join(File.dirname(__FILE__), "..", "lib")
14
14
  $LOAD_PATH.unshift(lib_directory) unless $LOAD_PATH.include?(lib_directory)
15
15
 
16
16
  require "frecon"
@@ -8,7 +8,7 @@
8
8
  # <http://opensource.org/licenses/MIT>.
9
9
 
10
10
  module FReCon
11
- VERSION = "0.1.3"
11
+ VERSION = "0.1.4"
12
12
 
13
13
  @environment_variable = :development
14
14
 
@@ -42,15 +42,15 @@ module FReCon
42
42
  rescue JSON::ParserError => e
43
43
  # If we have malformed JSON (JSON::ParserError is
44
44
  # raised), escape out of the function.
45
- return [400, ErrorFormatter.format(e.message)]
45
+ raise RequestError.new(400, e.message)
46
46
  end
47
47
 
48
- post_data.is_an?(Array) ? [422, ErrorFormatter.format("Must pass a JSON object!")] : post_data
48
+ raise RequestError.new(422, "Must pass a JSON object!") if post_data.is_an?(Array)
49
+ post_data
49
50
  end
50
51
 
51
52
  def self.create(request, params)
52
53
  post_data = process_request request
53
- return post_data if post_data.is_an?(Array)
54
54
 
55
55
  @model = model.new
56
56
  @model.attributes = post_data
@@ -58,24 +58,23 @@ module FReCon
58
58
  if @model.save
59
59
  [201, @model.to_json]
60
60
  else
61
- [422, ErrorFormatter.format(@model.errors.full_messages)]
61
+ raise RequestError.new(422, @model.errors.full_messages)
62
62
  end
63
63
  end
64
64
 
65
65
  def self.update(request, params)
66
- return [400, "Must supply a #{model_name.downcase}!"] unless params[:id]
66
+ raise RequestError.new(400, "Must supply a #{model_name.downcase}!") unless params[:id]
67
67
 
68
68
  post_data = process_request request
69
- return post_data if post_data.is_an?(Array)
70
69
 
71
70
  @model = model.find params[:id]
72
71
 
73
- return [404, ErrorFormatter.format(could_not_find(params[:id]))] unless @model
72
+ raise RequestError.new(404, could_not_find(params[:id])) unless @model
74
73
 
75
74
  if @model.update_attributes(post_data)
76
75
  @model.to_json
77
76
  else
78
- [422, ErrorFormatter.format(@model.errors.full_messages)]
77
+ raise RequestError.new(422, @model.errors.full_messages)
79
78
  end
80
79
  end
81
80
 
@@ -86,10 +85,10 @@ module FReCon
86
85
  if @model.destroy
87
86
  204
88
87
  else
89
- [422, ErrorFormatter.format(@model.errors.full_messages)]
88
+ raise RequestError.new(422, @model.errors.full_messages)
90
89
  end
91
90
  else
92
- [404, ErrorFormatter.format(could_not_find(params[:id]))]
91
+ raise RequestError.new(404, could_not_find(params[:id]))
93
92
  end
94
93
  end
95
94
 
@@ -99,7 +98,7 @@ module FReCon
99
98
  if @model
100
99
  @model.to_json
101
100
  else
102
- [404, ErrorFormatter.format(could_not_find(params[:id]))]
101
+ raise RequestError.new(404, could_not_find(params[:id]))
103
102
  end
104
103
  end
105
104
 
@@ -117,7 +116,7 @@ module FReCon
117
116
  if @model
118
117
  @model.send(attribute).to_json
119
118
  else
120
- [404, ErrorFormatter.format(could_not_find(params[:id]))]
119
+ raise RequestError.new(404, could_not_find(params[:id]))
121
120
  end
122
121
  end
123
122
 
@@ -130,7 +129,7 @@ module FReCon
130
129
 
131
130
  post_data
132
131
  else
133
- return [404, ErrorFormatter.format(could_not_find(post_data["team_number"], "number", "team"))]
132
+ raise RequestError.new(404, could_not_find(post_data["team_number"], "number", "team"))
134
133
  end
135
134
  end
136
135
  end
@@ -11,7 +11,6 @@ module FReCon
11
11
  class ParticipationsController < Controller
12
12
  def self.create(request, params)
13
13
  post_data = process_request request
14
- return post_data if post_data.is_an?(Array)
15
14
 
16
15
  # Convert team number to team_id.
17
16
  post_data = team_number_to_team_id(post_data)
@@ -22,7 +21,7 @@ module FReCon
22
21
  if @model.save
23
22
  [201, @model.to_json]
24
23
  else
25
- [422, ErrorFormatter.format(@model.errors.full_messages)]
24
+ raise RequestError.new(422, @model.errors.full_messages)
26
25
  end
27
26
  end
28
27
 
@@ -15,12 +15,10 @@ module FReCon
15
15
  class RecordsController < Controller
16
16
  def self.create(request, params)
17
17
  post_data = process_request request
18
- return post_data if post_data.is_an?(Array)
19
18
 
20
19
  # Change special post_data attributes.
21
20
  # Convert team number to team id.
22
21
  post_data = team_number_to_team_id(post_data)
23
- return post_data if post_data.is_an?(Array)
24
22
 
25
23
  # Convert match number and competition name to match id.
26
24
  if post_data["match_number"] && !post_data["match_id"]
@@ -29,14 +27,14 @@ module FReCon
29
27
  begin
30
28
  match = competition.matches.find_by number: post_data["match_number"]
31
29
  rescue ArgumentError, TypeError => e
32
- return [422, ErrorFormatter.format(e.message)]
30
+ raise RequestError.new(422, e.message)
33
31
  end
34
32
 
35
33
  # Create the match if necessary.
36
34
  begin
37
35
  match ||= Match.create(number: post_data["match_number"], competition_id: competition.id)
38
36
  rescue ArgumentError, TypeError => e
39
- return [422, ErrorFormatter.format(e.message)]
37
+ raise RequestError.new(422, e.message)
40
38
  end
41
39
 
42
40
  post_data["match_id"] = match.id
@@ -51,7 +49,7 @@ module FReCon
51
49
  begin
52
50
  match ||= Match.create(number: post_data["match_number"], competition_id: competition.id)
53
51
  rescue ArgumentError, TypeError => e
54
- return [422, ErrorFormatter.format(e.message)]
52
+ raise RequestError.new(422, e.message)
55
53
  end
56
54
 
57
55
  post_data["match_id"] = match.id
@@ -59,7 +57,7 @@ module FReCon
59
57
  post_data.delete("match_number")
60
58
  post_data.delete("competition")
61
59
  else
62
- return [422, ErrorFormatter.format("A current competition is not set. Please set it.")]
60
+ raise RequestError.new(422, "A current competition is not set. Please set it.")
63
61
  end
64
62
  end
65
63
 
@@ -70,7 +68,7 @@ module FReCon
70
68
  # Use to_json for now; we can filter it later.
71
69
  [201, @record.to_json]
72
70
  else
73
- [422, ErrorFormatter.format(@record.errors.full_messages)]
71
+ raise RequestError.new(422, @record.errors.full_messages)
74
72
  end
75
73
  end
76
74
 
@@ -15,7 +15,6 @@ module FReCon
15
15
  class TeamsController < Controller
16
16
  def self.create(request, params)
17
17
  post_data = process_request request
18
- return post_data if post_data.is_an?(Array)
19
18
 
20
19
  @team = Team.new
21
20
  @team.attributes = post_data
@@ -24,26 +23,22 @@ module FReCon
24
23
  # Use to_json for now; we can filter it later.
25
24
  [201, @team.to_json]
26
25
  else
27
- [422, ErrorFormatter.format(@team.errors.full_messages)]
26
+ raise RequestError.new(422, @team.errors.full_messages)
28
27
  end
29
28
  end
30
29
 
31
30
  def self.update(request, params)
32
- return [400, "Must supply a team number!"] unless params[:number]
31
+ raise RequestError.new(400, "Must supply a team number!") unless params[:number]
33
32
 
34
33
  post_data = process_request request
35
- return post_data if post_data.is_an?(Array)
36
34
 
37
35
  @team = Team.find_by number: params[:number]
38
-
39
- if @team.nil?
40
- return [404, ErrorFormatter.format(could_not_find(params[:number], "number"))]
41
- end
36
+ raise RequestError.new(404, could_not_find(params[:number], "number")) if @team.nil?
42
37
 
43
38
  if @team.update_attributes(post_data)
44
39
  @team.to_json
45
40
  else
46
- [422, ErrorFormatter.format(@team.errors.full_messages)]
41
+ raise RequestError.new(422, @team.errors.full_messages)
47
42
  end
48
43
  end
49
44
 
@@ -54,10 +49,10 @@ module FReCon
54
49
  if @team.destroy
55
50
  204
56
51
  else
57
- [422, ErrorFormatter.format(@team.errors.full_messages)]
52
+ raise RequestError.new(422, @team.errors.full_messages)
58
53
  end
59
54
  else
60
- [404, ErrorFormatter.format(could_not_find(params[:number], "number"))]
55
+ raise RequestError.new(404, could_not_find(params[:number], "number"))
61
56
  end
62
57
  end
63
58
 
@@ -67,7 +62,7 @@ module FReCon
67
62
  if @team
68
63
  @team.to_json
69
64
  else
70
- [404, ErrorFormatter.format(could_not_find(params[:number], "number"))]
65
+ raise RequestError.new(404, could_not_find(params[:number], "number"))
71
66
  end
72
67
  end
73
68
 
@@ -81,13 +76,13 @@ module FReCon
81
76
  if @competition
82
77
  @team.records.in(match_id: @competition.matches.map { |match| match.id }).to_json
83
78
  else
84
- [404, ErrorFormatter.format(could_not_find(params[:competition_id], "id", "competition"))]
79
+ raise RequestError.new(404, could_not_find(params[:competition_id], "id", "competition"))
85
80
  end
86
81
  else
87
82
  @team.records.to_json
88
83
  end
89
84
  else
90
- [404, ErrorFormatter.format(could_not_find(params[:number], "number"))]
85
+ raise RequestError.new(404, could_not_find(params[:number], "number"))
91
86
  end
92
87
  end
93
88
 
@@ -99,12 +94,12 @@ module FReCon
99
94
  if params[:competition_id]
100
95
  @competition = Competition.find params[:competition_id]
101
96
 
102
- return [404, ErrorFormatter.format(could_not_find(params[:competition_id], "id", "competition"))] if @competition.nil?
97
+ raise RequestError.new(404, could_not_find(params[:competition_id], "id", "competition")) if @competition.nil?
103
98
  end
104
99
 
105
100
  @team.matches(params[:competition_id]).to_json
106
101
  else
107
- [404, ErrorFormatter.format(could_not_find(params[:number], "number"))]
102
+ raise RequestError.new(404, could_not_find(params[:number], "number"))
108
103
  end
109
104
  end
110
105
 
@@ -114,7 +109,7 @@ module FReCon
114
109
  if @team
115
110
  @team.competitions.to_json
116
111
  else
117
- [404, ErrorFormatter.format(could_not_find(params[:number], "number"))]
112
+ raise RequestError.new(404, could_not_find(params[:number], "number"))
118
113
  end
119
114
  end
120
115
  end
@@ -8,7 +8,7 @@
8
8
  # <http://opensource.org/licenses/MIT>.
9
9
 
10
10
  require "json"
11
- require "frecon/error_formatter"
11
+ require "frecon/request_error"
12
12
 
13
13
  require "frecon/controller"
14
14
 
@@ -1,4 +1,4 @@
1
- # lib/frecon/error_formatter.rb
1
+ # lib/frecon/request_error.rb
2
2
  #
3
3
  # Copyright (C) 2014 Christopher Cooper, Sam Craig, Tiger Huang, Vincent Mai, Sam Mercier, and Kristofer Rye
4
4
  #
@@ -9,15 +9,21 @@
9
9
 
10
10
  require "json"
11
11
 
12
- module FReCon
13
- class ErrorFormatter
14
- def self.format(message)
15
- case message
16
- when String
17
- JSON.generate({ errors: [ message ] })
18
- when Array
19
- JSON.generate({ errors: message })
20
- end
12
+ class RequestError < StandardError
13
+ attr_reader :code
14
+ attr_reader :message
15
+
16
+ def initialize(code, message = nil)
17
+ @code = code
18
+ @message = format_error_message(message)
19
+ end
20
+
21
+ def format_error_message(message)
22
+ case message
23
+ when String
24
+ JSON.generate({ errors: [ message ] })
25
+ when Array
26
+ JSON.generate({ errors: message })
21
27
  end
22
28
  end
23
29
  end
data/lib/frecon/routes.rb CHANGED
@@ -14,31 +14,51 @@ module FReCon
14
14
  def self.resource_routes(base, name, controller, methods = [:create, :update, :delete, :show, :index])
15
15
  if methods.include?(:create)
16
16
  base.post "/#{name}" do
17
- controller.create request, params
17
+ begin
18
+ controller.create request, params
19
+ rescue RequestError => e
20
+ [e.code, e.message]
21
+ end
18
22
  end
19
23
  end
20
24
 
21
25
  if methods.include?(:update)
22
26
  base.put "/#{name}/:id" do
23
- controller.update request, params
27
+ begin
28
+ controller.update request, params
29
+ rescue RequestError => e
30
+ [e.code, e.message]
31
+ end
24
32
  end
25
33
  end
26
34
 
27
35
  if methods.include?(:delete)
28
36
  base.delete "/#{name}/:id" do
29
- controller.delete params
37
+ begin
38
+ controller.delete params
39
+ rescue RequestError => e
40
+ [e.code, e.message]
41
+ end
30
42
  end
31
43
  end
32
44
 
33
45
  if methods.include?(:show)
34
46
  base.get "/#{name}/:id" do
35
- controller.show params
47
+ begin
48
+ controller.show params
49
+ rescue RequestError => e
50
+ [e.code, e.message]
51
+ end
36
52
  end
37
53
  end
38
54
 
39
55
  if methods.include?(:index)
40
56
  base.get "/#{name}" do
41
- controller.index params
57
+ begin
58
+ controller.index params
59
+ rescue RequestError => e
60
+ [e.code, e.message]
61
+ end
42
62
  end
43
63
  end
44
64
  end
@@ -47,81 +67,149 @@ module FReCon
47
67
  resource_routes base, "teams", TeamsController, [:create, :index]
48
68
 
49
69
  base.put "/teams/:number" do
50
- TeamsController.update request, params
70
+ begin
71
+ TeamsController.update request, params
72
+ rescue RequestError => e
73
+ [e.code, e.message]
74
+ end
51
75
  end
52
76
 
53
77
  base.delete "/teams/:number" do
54
- TeamsController.delete params
78
+ begin
79
+ TeamsController.delete params
80
+ rescue RequestError => e
81
+ [e.code, e.message]
82
+ end
55
83
  end
56
84
 
57
85
  base.get "/teams/:number" do
58
- TeamsController.show params
86
+ begin
87
+ TeamsController.show params
88
+ rescue RequestError => e
89
+ [e.code, e.message]
90
+ end
59
91
  end
60
92
 
61
93
  base.get "/teams/:number/records/?:competition_id?" do
62
- TeamsController.records params
94
+ begin
95
+ TeamsController.records params
96
+ rescue RequestError => e
97
+ [e.code, e.message]
98
+ end
63
99
  end
64
100
 
65
101
  base.get "/teams/:number/matches/?:competition_id?" do
66
- TeamsController.matches params
102
+ begin
103
+ TeamsController.matches params
104
+ rescue RequestError => e
105
+ [e.code, e.message]
106
+ end
67
107
  end
68
108
 
69
109
  base.get "/teams/:number/competitions" do
70
- TeamsController.competitions params
110
+ begin
111
+ TeamsController.competitions params
112
+ rescue RequestError => e
113
+ [e.code, e.message]
114
+ end
71
115
  end
72
116
 
73
117
  resource_routes base, "competitions", CompetitionsController
74
118
 
75
119
  base.get "/competitions/:id/teams" do
76
- CompetitionsController.teams params
120
+ begin
121
+ CompetitionsController.teams params
122
+ rescue RequestError => e
123
+ [e.code, e.message]
124
+ end
77
125
  end
78
126
 
79
127
  base.get "/competitions/:id/matches" do
80
- CompetitionsController.matches params
128
+ begin
129
+ CompetitionsController.matches params
130
+ rescue RequestError => e
131
+ [e.code, e.message]
132
+ end
81
133
  end
82
134
 
83
135
  base.get "/competitions/:id/records" do
84
- CompetitionsController.records params
136
+ begin
137
+ CompetitionsController.records params
138
+ rescue RequestError => e
139
+ [e.code, e.message]
140
+ end
85
141
  end
86
142
 
87
143
  resource_routes base, "matches", MatchesController
88
144
 
89
145
  base.get "/matches/:id/records" do
90
- MatchesController.records params
146
+ begin
147
+ MatchesController.records params
148
+ rescue RequestError => e
149
+ [e.code, e.message]
150
+ end
91
151
  end
92
152
 
93
153
  base.get "/matches/:id/competition" do
94
- MatchesController.competition params
154
+ begin
155
+ MatchesController.competition params
156
+ rescue RequestError => e
157
+ [e.code, e.message]
158
+ end
95
159
  end
96
160
 
97
161
  resource_routes base, "records", RecordsController
98
162
 
99
163
  base.get "/records/:id/competition" do
100
- RecordsController.competition params
164
+ begin
165
+ RecordsController.competition params
166
+ rescue RequestError => e
167
+ [e.code, e.message]
168
+ end
101
169
  end
102
170
 
103
171
  resource_routes base, "robots", RobotsController
104
172
 
105
173
  base.get "/robots/:id/competition" do
106
- RobotsController.competition params
174
+ begin
175
+ RobotsController.competition params
176
+ rescue RequestError => e
177
+ [e.code, e.message]
178
+ end
107
179
  end
108
180
 
109
181
  base.get "/robots/:id/team" do
110
- RobotsController.team params
182
+ begin
183
+ RobotsController.team params
184
+ rescue RequestError => e
185
+ [e.code, e.message]
186
+ end
111
187
  end
112
188
 
113
189
  resource_routes base, "participations", ParticipationsController
114
190
 
115
191
  base.get "/participations/:id/competition" do
116
- ParticipationsController.competition params
192
+ begin
193
+ ParticipationsController.competition params
194
+ rescue RequestError => e
195
+ [e.code, e.message]
196
+ end
117
197
  end
118
198
 
119
199
  base.get "/participations/:id/team" do
120
- ParticipationsController.team params
200
+ begin
201
+ ParticipationsController.team params
202
+ rescue RequestError => e
203
+ [e.code, e.message]
204
+ end
121
205
  end
122
206
 
123
207
  base.get "/dump" do
124
- DumpController.full params
208
+ begin
209
+ DumpController.full params
210
+ rescue RequestError => e
211
+ [e.code, e.message]
212
+ end
125
213
  end
126
214
  end
127
215
  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.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sam Craig
@@ -81,7 +81,6 @@ files:
81
81
  - lib/frecon/controllers/robots_controller.rb
82
82
  - lib/frecon/controllers/teams_controller.rb
83
83
  - lib/frecon/database.rb
84
- - lib/frecon/error_formatter.rb
85
84
  - lib/frecon/match_number.rb
86
85
  - lib/frecon/model.rb
87
86
  - lib/frecon/models.rb
@@ -93,6 +92,7 @@ files:
93
92
  - lib/frecon/models/team.rb
94
93
  - lib/frecon/mongoid.yml
95
94
  - lib/frecon/position.rb
95
+ - lib/frecon/request_error.rb
96
96
  - lib/frecon/routes.rb
97
97
  - lib/frecon/server.rb
98
98
  homepage: https://github.com/frc-frecon/frecon