toast 1.0.13 → 1.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/lib/toast/canonical_request.rb +6 -3
- data/lib/toast/collection_request.rb +6 -7
- data/lib/toast/engine.rb +11 -8
- data/lib/toast/errors.rb +3 -1
- data/lib/toast/plural_assoc_request.rb +13 -10
- data/lib/toast/rack_app.rb +1 -1
- data/lib/toast/request_helpers.rb +3 -3
- data/lib/toast/single_request.rb +2 -1
- data/lib/toast/singular_assoc_request.rb +7 -4
- data/lib/toast/version.rb +1 -1
- metadata +3 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 3ffc230d6241fe54b81431776232f63395f8149f3632e01ef7151ea353de46bd
|
4
|
+
data.tar.gz: f04101583f930052ad231149b6fb8026784eea410c01f170caea81729026deb7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ee500860d0639ae2c7e8e9e767d8acd6d9a1edd005237a83adb85401800bd68124f2d8add562039439c04833444ddcccc9fd88e7c744775feda96379d28e15d7
|
7
|
+
data.tar.gz: ea3d957809f004f730786b4182ddaa16a7616e1089bd0a1c7cd4141e68f6e603e4feaf4ab1e23f3b6a18920fcedbdff05c82fdc85d5e5c1c88d4671d92aec1d6
|
@@ -51,7 +51,8 @@ class Toast::CanonicalRequest
|
|
51
51
|
response :not_found, msg: "#{@base_config.model_class}##{@id} not found"
|
52
52
|
|
53
53
|
rescue BadRequest => error
|
54
|
-
response :bad_request, msg: "`#{error.message}' in: #{error.source_location}"
|
54
|
+
response :bad_request, msg: "`#{error.message}' in: #{error.source_location}",
|
55
|
+
headers: {'X-Toast-Error' => error.code}
|
55
56
|
|
56
57
|
rescue AllowError => error
|
57
58
|
response :internal_server_error,
|
@@ -117,7 +118,8 @@ class Toast::CanonicalRequest
|
|
117
118
|
response :not_found, msg: error.message
|
118
119
|
|
119
120
|
rescue BadRequest => error
|
120
|
-
response :bad_request, msg: "`#{error.message}' in: #{error.source_location}"
|
121
|
+
response :bad_request, msg: "`#{error.message}' in: #{error.source_location}",
|
122
|
+
headers: {'X-Toast-Error' => error.code}
|
121
123
|
|
122
124
|
rescue AllowError => error
|
123
125
|
response :internal_server_error,
|
@@ -165,7 +167,8 @@ class Toast::CanonicalRequest
|
|
165
167
|
msg: "exception raised in allow block: `#{error.orig_error.message}' in #{error.source_location}"
|
166
168
|
|
167
169
|
rescue BadRequest => error
|
168
|
-
response :bad_request, msg: "`#{error.message}' in: #{error.source_location}"
|
170
|
+
response :bad_request, msg: "`#{error.message}' in: #{error.source_location}",
|
171
|
+
headers: {'X-Toast-Error' => error.code}
|
169
172
|
|
170
173
|
rescue HandlerError => error
|
171
174
|
return response :internal_server_error,
|
@@ -49,14 +49,11 @@ class Toast::CollectionRequest
|
|
49
49
|
@auth, relation, @uri_params)
|
50
50
|
|
51
51
|
if relation.is_a?(ActiveRecord::Relation) and
|
52
|
-
relation.model
|
52
|
+
relation.model== @config.base_model_class
|
53
53
|
|
54
54
|
result = relation.limit(window).offset(range_start)
|
55
55
|
|
56
|
-
|
57
|
-
# fix problematic select extensions for counting (-> { select(...) })
|
58
|
-
# this fails if the where clause depends on the the extended select
|
59
|
-
count = relation.count_by_sql relation.to_sql.sub(/SELECT.+FROM/,'SELECT COUNT(*) FROM')
|
56
|
+
count = relation.unscope(:select).select(:id).count
|
60
57
|
headers = {"Content-Type" => @config.media_type}
|
61
58
|
|
62
59
|
if count > 0
|
@@ -80,7 +77,8 @@ class Toast::CollectionRequest
|
|
80
77
|
msg: "not authorized by allow block in: #{error.source_location}"
|
81
78
|
|
82
79
|
rescue BadRequest => error
|
83
|
-
response :bad_request, msg: "`#{error.message}' in: #{error.source_location}"
|
80
|
+
response :bad_request, msg: "`#{error.message}' in: #{error.source_location}",
|
81
|
+
headers: {'X-Toast-Error' => error.code}
|
84
82
|
|
85
83
|
rescue HandlerError => error
|
86
84
|
return response :internal_server_error,
|
@@ -139,7 +137,8 @@ class Toast::CollectionRequest
|
|
139
137
|
msg: "not authorized by allow block in: #{error.source_location}"
|
140
138
|
|
141
139
|
rescue BadRequest => error
|
142
|
-
response :bad_request, msg: "`#{error.message}' in: #{error.source_location}"
|
140
|
+
response :bad_request, msg: "`#{error.message}' in: #{error.source_location}",
|
141
|
+
headers: {'X-Toast-Error' => error.code}
|
143
142
|
|
144
143
|
rescue HandlerError => error
|
145
144
|
return response :internal_server_error,
|
data/lib/toast/engine.rb
CHANGED
@@ -19,16 +19,19 @@ module Toast
|
|
19
19
|
|
20
20
|
# skip init if in test mode: Toast.init should be called in each test
|
21
21
|
unless Rails.env == 'test'
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
22
|
+
# defer initilization after auto-loader has registered all models
|
23
|
+
app.config.after_initialize do
|
24
|
+
begin
|
25
|
+
Toast.info 'Loading Toast'
|
26
|
+
Toast.init
|
27
|
+
Toast.info "Exposed model classes: #{Toast.expositions.map{|e| e.model_class.name}.join(' ')}"
|
26
28
|
|
27
|
-
|
28
|
-
|
29
|
-
|
29
|
+
rescue Toast::ConfigError => error
|
30
|
+
error.message.split("\n").each do |line|
|
31
|
+
Toast.info line
|
32
|
+
end
|
33
|
+
Toast.disable
|
30
34
|
end
|
31
|
-
Toast.disable
|
32
35
|
end
|
33
36
|
end
|
34
37
|
end
|
data/lib/toast/errors.rb
CHANGED
@@ -24,9 +24,11 @@ module Toast::Errors
|
|
24
24
|
|
25
25
|
class BadRequest < StandardError
|
26
26
|
attr_accessor :source_location
|
27
|
+
attr_accessor :code
|
27
28
|
|
28
|
-
def initialize message, source_location
|
29
|
+
def initialize message, source_location, code = nil
|
29
30
|
@source_location = source_location
|
31
|
+
@code = code
|
30
32
|
super message
|
31
33
|
end
|
32
34
|
end
|
@@ -67,8 +67,7 @@ class Toast::PluralAssocRequest
|
|
67
67
|
# this fails if the where clause depends on the the extended select
|
68
68
|
# also remove any ORDER clauses
|
69
69
|
|
70
|
-
|
71
|
-
count = relation.count_by_sql relation.to_sql.sub(/SELECT.+FROM/,'SELECT COUNT(*) FROM').sub(/ORDER BY.*/,'')
|
70
|
+
count = relation.unscope(:select).select(:id).count
|
72
71
|
headers = {"Content-Type" => @config.media_type}
|
73
72
|
|
74
73
|
if count > 0
|
@@ -90,7 +89,8 @@ class Toast::PluralAssocRequest
|
|
90
89
|
msg: "exception raised in allow block: `#{error.orig_error.message}' in #{error.source_location}"
|
91
90
|
|
92
91
|
rescue BadRequest => error
|
93
|
-
response :bad_request, msg: "`#{error.message}' in: #{error.source_location}"
|
92
|
+
response :bad_request, msg: "`#{error.message}' in: #{error.source_location}",
|
93
|
+
headers: {'X-Toast-Error' => error.code}
|
94
94
|
|
95
95
|
rescue HandlerError => error
|
96
96
|
return response :internal_server_error,
|
@@ -159,7 +159,8 @@ class Toast::PluralAssocRequest
|
|
159
159
|
msg: "exception raised in allow block: `#{error.orig_error.message}' in #{error.source_location}"
|
160
160
|
|
161
161
|
rescue BadRequest => error
|
162
|
-
response :bad_request, msg: "`#{error.message}' in: #{error.source_location}"
|
162
|
+
response :bad_request, msg: "`#{error.message}' in: #{error.source_location}",
|
163
|
+
headers: {'X-Toast-Error' => error.code}
|
163
164
|
|
164
165
|
rescue HandlerError => error
|
165
166
|
return response :internal_server_error,
|
@@ -206,14 +207,15 @@ class Toast::PluralAssocRequest
|
|
206
207
|
call_handler(@config.via_link.handler, source, target, @uri_params)
|
207
208
|
|
208
209
|
response :ok,
|
209
|
-
msg: "linked #{target_model_class.name}##{
|
210
|
+
msg: "linked #{target_model_class.name}##{target_id} with #{source.class}##{source.id}.#{@config.assoc_name}",
|
210
211
|
body: Toast.settings.link_unlink_via_post ? '' : nil
|
211
212
|
|
212
213
|
rescue ActiveRecord::RecordNotFound => error
|
213
214
|
response :not_found, msg: error.message
|
214
215
|
|
215
216
|
rescue BadRequest => error
|
216
|
-
response :bad_request, msg: "`#{error.message}' in: #{error.source_location}"
|
217
|
+
response :bad_request, msg: "`#{error.message}' in: #{error.source_location}",
|
218
|
+
headers: {'X-Toast-Error' => error.code}
|
217
219
|
|
218
220
|
rescue AllowError => error
|
219
221
|
return response :internal_server_error,
|
@@ -245,7 +247,7 @@ class Toast::PluralAssocRequest
|
|
245
247
|
return response :bad_request, msg: "Link header missing or invalid"
|
246
248
|
end
|
247
249
|
|
248
|
-
name,
|
250
|
+
name, target_id = split_link_header(link)
|
249
251
|
target_model_class = name.singularize.classify.constantize
|
250
252
|
|
251
253
|
unless is_active_record? target_model_class
|
@@ -253,10 +255,10 @@ class Toast::PluralAssocRequest
|
|
253
255
|
end
|
254
256
|
|
255
257
|
call_allow(@config.via_unlink.permissions, @auth, source, @uri_params)
|
256
|
-
call_handler(@config.via_unlink.handler, source, target_model_class.find(
|
258
|
+
call_handler(@config.via_unlink.handler, source, target_model_class.find(target_id), @uri_params)
|
257
259
|
|
258
260
|
response :ok,
|
259
|
-
msg: "unlinked #{target_model_class.name}##{
|
261
|
+
msg: "unlinked #{target_model_class.name}##{target_id} from #{source.class}##{source.id}.#{@config.assoc_name}",
|
260
262
|
body: Toast.settings.link_unlink_via_post ? '' : nil
|
261
263
|
|
262
264
|
rescue ActiveRecord::RecordNotFound => error
|
@@ -267,7 +269,8 @@ class Toast::PluralAssocRequest
|
|
267
269
|
msg: "exception raised in allow block: `#{error.orig_error.message}' in #{error.source_location}"
|
268
270
|
|
269
271
|
rescue BadRequest => error
|
270
|
-
response :bad_request, msg: "`#{error.message}' in: #{error.source_location}"
|
272
|
+
response :bad_request, msg: "`#{error.message}' in: #{error.source_location}",
|
273
|
+
headers: {'X-Toast-Error' => error.code}
|
271
274
|
|
272
275
|
rescue HandlerError => error
|
273
276
|
return response :internal_server_error,
|
data/lib/toast/rack_app.rb
CHANGED
@@ -18,7 +18,7 @@ class Toast::RackApp
|
|
18
18
|
request = ActionDispatch::Request.new(env)
|
19
19
|
Toast.request = request
|
20
20
|
|
21
|
-
Toast.logger.info "
|
21
|
+
Toast.logger.info "processing: <#{request.method} #{URI.decode(request.fullpath)}>"
|
22
22
|
|
23
23
|
# Authentication: respond with 401 on exception or falsy return value:
|
24
24
|
begin
|
@@ -90,7 +90,7 @@ module Toast::RequestHelpers
|
|
90
90
|
#
|
91
91
|
# Return: Rack conform response
|
92
92
|
def response status_sym, headers: {}, msg: nil, body: nil
|
93
|
-
Toast.logger.info "
|
93
|
+
Toast.logger.info "done: #{msg}"
|
94
94
|
|
95
95
|
unless Rails.env == 'production'
|
96
96
|
# put message in body, too, if body is free
|
@@ -122,8 +122,8 @@ module Toast::RequestHelpers
|
|
122
122
|
|
123
123
|
begin
|
124
124
|
context = Object.new
|
125
|
-
context.define_singleton_method(:bad_request) do |message|
|
126
|
-
raise Toast::Errors::BadRequest.new message, caller.first.sub(/:in.*/,'')
|
125
|
+
context.define_singleton_method(:bad_request) do |message, options = {}|
|
126
|
+
raise Toast::Errors::BadRequest.new message, caller.first.sub(/:in.*/,''), options[:code]
|
127
127
|
end
|
128
128
|
|
129
129
|
result = context.instance_exec *args, &proc
|
data/lib/toast/single_request.rb
CHANGED
@@ -49,7 +49,8 @@ class Toast::SingleRequest
|
|
49
49
|
msg: "exception raised in allow block: `#{error.orig_error.message}' in #{error.source_location}"
|
50
50
|
|
51
51
|
rescue BadRequest => error
|
52
|
-
response :bad_request, msg: "`#{error.message}' in: #{error.source_location}"
|
52
|
+
response :bad_request, msg: "`#{error.message}' in: #{error.source_location}",
|
53
|
+
headers: {'X-Toast-Error' => error.code}
|
53
54
|
|
54
55
|
rescue HandlerError => error
|
55
56
|
return response :internal_server_error,
|
@@ -50,7 +50,7 @@ class Toast::SingularAssocRequest
|
|
50
50
|
response :ok,
|
51
51
|
headers: {"Content-Type" => @config.media_type},
|
52
52
|
body: represent(model_instance, target_config),
|
53
|
-
msg: "sent
|
53
|
+
msg: "sent #{target_config.model_class}##{model_instance.id}"
|
54
54
|
end
|
55
55
|
|
56
56
|
rescue NotAllowed => error
|
@@ -58,7 +58,8 @@ class Toast::SingularAssocRequest
|
|
58
58
|
|
59
59
|
|
60
60
|
rescue BadRequest => error
|
61
|
-
response :bad_request, msg: "`#{error.message}' in: #{error.source_location}"
|
61
|
+
response :bad_request, msg: "`#{error.message}' in: #{error.source_location}",
|
62
|
+
headers: {'X-Toast-Error' => error.code}
|
62
63
|
|
63
64
|
rescue HandlerError => error
|
64
65
|
return response :internal_server_error,
|
@@ -120,7 +121,8 @@ class Toast::SingularAssocRequest
|
|
120
121
|
return response :unauthorized, msg: "not authorized by allow block in: #{error.source_location}"
|
121
122
|
|
122
123
|
rescue BadRequest => error
|
123
|
-
response :bad_request, msg: "`#{error.message}' in: #{error.source_location}"
|
124
|
+
response :bad_request, msg: "`#{error.message}' in: #{error.source_location}",
|
125
|
+
headers: {'X-Toast-Error' => error.code}
|
124
126
|
|
125
127
|
rescue HandlerError => error
|
126
128
|
return response :internal_server_error,
|
@@ -191,7 +193,8 @@ class Toast::SingularAssocRequest
|
|
191
193
|
msg: "not authorized by allow block in: #{error.source_location}"
|
192
194
|
|
193
195
|
rescue BadRequest => error
|
194
|
-
response :bad_request, msg: "`#{error.message}' in: #{error.source_location}"
|
196
|
+
response :bad_request, msg: "`#{error.message}' in: #{error.source_location}",
|
197
|
+
headers: {'X-Toast-Error' => error.code}
|
195
198
|
|
196
199
|
rescue HandlerError => error
|
197
200
|
return response :internal_server_error,
|
data/lib/toast/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: toast
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- robokopp (Robert Annies)
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-01-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -94,8 +94,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
94
94
|
- !ruby/object:Gem::Version
|
95
95
|
version: '0'
|
96
96
|
requirements: []
|
97
|
-
|
98
|
-
rubygems_version: 2.5.2.3
|
97
|
+
rubygems_version: 3.0.3
|
99
98
|
signing_key:
|
100
99
|
specification_version: 4
|
101
100
|
summary: Toast exposes ActiveRecord models as a web service (REST API).
|