frecon 0.2.1 → 0.2.2

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: db237e76c74ecaf128c7b986136516d29f780cca
4
- data.tar.gz: 82bb1cfe4f705c83a27f9ed22a3417fae8c18161
3
+ metadata.gz: a8a2e4e957492a0168c9e814b237ea59435aa800
4
+ data.tar.gz: 3051b10b6a8e7740370ffd75eebce4230b3ef252
5
5
  SHA512:
6
- metadata.gz: b47d5f45113e9a0d53dfe466b320c80347dcf7caf836f8daaca93d5ef4a557718e3a048ce2e5d1716fea3b53207b7e8a32baa021108fb564e89d0361187ca329
7
- data.tar.gz: 5472f1258969eb8ccf41c82b99741658ba5dc3a335b80a98374dfd8658eb0b888a8fe44fb14a6c4871575755bfb8c2fff34bdbe13be6ba1a66d035fcb8a7f9ea
6
+ metadata.gz: b579c98940cd2759ed748dbaea6b51ffeef679c6f08f958a1e7501fda4e5043e20b4fb987422ebb002b013fb679eb1ac4586a6027bfb02186b196507a0faafc7
7
+ data.tar.gz: a312a6a56950302d16e0075875e25ef9b830164eb9d7999820e364fa49c5b170a72b4f7bf96ff99a2a64e7cdaca836953b9811fab42a315102586fb03014a7fd
@@ -8,7 +8,7 @@
8
8
  # <http://opensource.org/licenses/MIT>.
9
9
 
10
10
  module FReCon
11
- VERSION = "0.2.1"
11
+ VERSION = "0.2.2"
12
12
 
13
13
  @environment_variable = :development
14
14
 
@@ -51,7 +51,7 @@ module FReCon
51
51
  raise RequestError.new(400, e.message)
52
52
  end
53
53
 
54
- raise RequestError.new(422, "Must pass a JSON object!") if post_data.is_an?(Array)
54
+ raise RequestError.new(422, "Must pass a JSON object!", {post_data: post_data}) if post_data.is_an?(Array)
55
55
  post_data
56
56
  end
57
57
 
@@ -64,10 +64,10 @@ module FReCon
64
64
  if @model.save
65
65
  [201, @model.to_json]
66
66
  else
67
- raise RequestError.new(422, @model.errors.full_messages)
67
+ raise RequestError.new(422, @model.errors.full_messages, {params: params, post_data: post_data})
68
68
  end
69
69
  end
70
-
70
+
71
71
  def self.update(request, params, post_data = nil)
72
72
  raise RequestError.new(400, "Must supply a #{model_name.downcase} id!") unless params[:id]
73
73
 
@@ -80,7 +80,7 @@ module FReCon
80
80
  if @model.update_attributes(post_data)
81
81
  @model.to_json
82
82
  else
83
- raise RequestError.new(422, @model.errors.full_messages)
83
+ raise RequestError.new(422, @model.errors.full_messages, {params: params, post_data: post_data})
84
84
  end
85
85
  end
86
86
 
@@ -94,7 +94,7 @@ module FReCon
94
94
  raise RequestError.new(422, @model.errors.full_messages)
95
95
  end
96
96
  else
97
- raise RequestError.new(404, could_not_find(params[:id]))
97
+ raise RequestError.new(404, could_not_find(params[:id]), {params: params})
98
98
  end
99
99
  end
100
100
 
@@ -104,7 +104,7 @@ module FReCon
104
104
  if @model
105
105
  @model.to_json
106
106
  else
107
- raise RequestError.new(404, could_not_find(params[:id]))
107
+ raise RequestError.new(404, could_not_find(params[:id]), {params: params})
108
108
  end
109
109
  end
110
110
 
@@ -122,7 +122,7 @@ module FReCon
122
122
  if @model
123
123
  @model.send(attribute).to_json
124
124
  else
125
- raise RequestError.new(404, could_not_find(params[:id]))
125
+ raise RequestError.new(404, could_not_find(params[:id]), {params: params, attribute: attribute})
126
126
  end
127
127
  end
128
128
 
@@ -135,7 +135,7 @@ module FReCon
135
135
 
136
136
  post_data
137
137
  else
138
- raise RequestError.new(404, could_not_find(post_data["team_number"], "number", "team"))
138
+ raise RequestError.new(404, could_not_find(post_data["team_number"], "number", "team"), {post_data: post_data})
139
139
  end
140
140
  end
141
141
  end
@@ -149,14 +149,14 @@ module FReCon
149
149
  begin
150
150
  match = competition.matches.find_by number: post_data["match_number"]
151
151
  rescue ArgumentError, TypeError => e
152
- raise RequestError.new(422, e.message)
152
+ raise RequestError.new(422, e.message, {post_data: post_data})
153
153
  end
154
154
 
155
155
  # Create the match if necessary.
156
156
  begin
157
157
  match ||= Match.create(number: post_data["match_number"], competition_id: competition.id)
158
158
  rescue ArgumentError, TypeError => e
159
- raise RequestError.new(422, e.message)
159
+ raise RequestError.new(422, e.message, {post_data: post_data, competition_id: competition.id})
160
160
  end
161
161
 
162
162
  post_data["match_id"] = match.id
@@ -173,7 +173,7 @@ module FReCon
173
173
  begin
174
174
  match ||= Match.create(number: post_data["match_number"], competition_id: competition.id)
175
175
  rescue ArgumentError, TypeError => e
176
- raise RequestError.new(422, e.message)
176
+ raise RequestError.new(422, e.message, {post_data: post_data, competition_id: competition.id})
177
177
  end
178
178
 
179
179
  post_data["match_id"] = match.id
@@ -183,7 +183,7 @@ module FReCon
183
183
 
184
184
  post_data
185
185
  else
186
- raise RequestError.new(422, "A current competition is not set. Please set it.")
186
+ raise RequestError.new(422, "A current competition is not set. Please set it.", {post_data: post_data})
187
187
  end
188
188
  end
189
189
  end
@@ -38,13 +38,13 @@ module FReCon
38
38
  if @competition
39
39
  @team.records.in(match_id: @competition.matches.map { |match| match.id }).to_json
40
40
  else
41
- raise RequestError.new(404, could_not_find(params[:competition_id], "id", "competition"))
41
+ raise RequestError.new(404, could_not_find(params[:competition_id], "id", "competition"), {params: params, team: @team})
42
42
  end
43
43
  else
44
44
  @team.records.to_json
45
45
  end
46
46
  else
47
- raise RequestError.new(404, could_not_find(params[:id], "id or number"))
47
+ raise RequestError.new(404, could_not_find(params[:id], "id or number"), {params: params})
48
48
  end
49
49
  end
50
50
 
@@ -56,12 +56,12 @@ module FReCon
56
56
  if params[:competition_id]
57
57
  @competition = Competition.find params[:competition_id]
58
58
 
59
- raise RequestError.new(404, could_not_find(params[:competition_id], "id", "competition")) if @competition.nil?
59
+ raise RequestError.new(404, could_not_find(params[:competition_id], "id", "competition"), {params: params, team: @team}) if @competition.nil?
60
60
  end
61
61
 
62
62
  @team.matches(params[:competition_id]).to_json
63
63
  else
64
- raise RequestError.new(404, could_not_find(params[:id], "id or number"))
64
+ raise RequestError.new(404, could_not_find(params[:id], "id or number"), {params: params})
65
65
  end
66
66
  end
67
67
 
@@ -71,7 +71,7 @@ module FReCon
71
71
  if @team
72
72
  @team.competitions.to_json
73
73
  else
74
- raise RequestError.new(404, could_not_find(params[:id], "id or number"))
74
+ raise RequestError.new(404, could_not_find(params[:id], "id or number"), {params: params})
75
75
  end
76
76
  end
77
77
  end
@@ -10,29 +10,32 @@
10
10
  require "json"
11
11
 
12
12
  class RequestError < StandardError
13
- attr_reader :code
14
- attr_reader :message
13
+ attr_reader :return_value
15
14
 
16
- def initialize(code, message = nil)
15
+ def initialize(code, message = nil, context = nil)
17
16
  @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 })
27
- end
28
- end
17
+ @message = message
18
+ @context = context
29
19
 
30
- # A Sinatra-compliant return value.
31
- def return_value
32
- if @message
33
- [@code, @message]
34
- else
35
- @code
36
- end
20
+ # When @message is a String or an Array,
21
+ # the return_value is set to a Sinatra-compliant
22
+ # Array with @code being the first element and the
23
+ # response body being the stringification of the
24
+ # JSON stringification of @context and @message.
25
+ #
26
+ # If @message is a String, it is first put into an
27
+ # array.
28
+ #
29
+ # If @message is neither a String nor an Array,
30
+ # @return_value becomes simply @code.
31
+ @return_value =
32
+ case @message
33
+ when String
34
+ [@code, JSON.generate({ context: @context, errors: [ @message ] })]
35
+ when Array
36
+ [@code, JSON.generate({ context: @context, errors: @message })]
37
+ else
38
+ @code
39
+ end
37
40
  end
38
41
  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.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sam Craig