tzispa 0.7.5 → 0.7.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/lib/tzispa/api/handler.rb +8 -29
- data/lib/tzispa/api/handler_error.rb +76 -0
- data/lib/tzispa/controller/api.rb +12 -24
- data/lib/tzispa/controller/base.rb +12 -11
- data/lib/tzispa/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 13911ddd5b938320efb3e6aa971d996be88581e5
|
4
|
+
data.tar.gz: a054ae603791f5a57a66b40a837ea5fd95b254cb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 28359f1534f2799ab711386a2caab2641bb047b663a864de73c815851192e772e520acc3d71831c2cabe3143e4706b3fa4702975a9d8b7e147f85212b55c4135
|
7
|
+
data.tar.gz: 976f8d1b4b7e18af9887e3400d56320de2a45e300822b696846441dc7f2557f35eed6a56eb649722d02f711dcc91f88f6b7e611aa0004385ff073c48f6b6cd77
|
data/CHANGELOG.md
CHANGED
data/lib/tzispa/api/handler.rb
CHANGED
@@ -1,13 +1,11 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'forwardable'
|
4
|
-
require 'json'
|
5
|
-
require 'i18n'
|
6
4
|
require 'tzispa/helpers/provider'
|
7
5
|
require 'tzispa/helpers/sign_requirer'
|
8
6
|
require 'tzispa/helpers/hooks/before'
|
9
7
|
require 'tzispa/helpers/hooks/after'
|
10
|
-
|
8
|
+
require_relative 'handler_error'
|
11
9
|
|
12
10
|
module Tzispa
|
13
11
|
module Api
|
@@ -23,6 +21,7 @@ module Tzispa
|
|
23
21
|
class Handler
|
24
22
|
extend Forwardable
|
25
23
|
|
24
|
+
include Tzispa::Api::HandlerError
|
26
25
|
include Tzispa::Helpers::Provider
|
27
26
|
include Tzispa::Helpers::SignRequirer
|
28
27
|
include Tzispa::Helpers::Hooks::Before
|
@@ -34,30 +33,15 @@ module Tzispa
|
|
34
33
|
def_delegators :@context, :request, :response, :app, :repository,
|
35
34
|
:config, :logger, :unauthorized_but_logged, :login_redirect
|
36
35
|
|
37
|
-
attr_writer :error
|
38
|
-
|
39
|
-
HANDLER_OK = :ok
|
40
|
-
HANDLER_MISSING_PARAMETER = :missing_parameter
|
41
|
-
|
42
36
|
def initialize(context)
|
43
37
|
@context = context
|
44
38
|
@error = nil
|
45
39
|
@status = nil
|
46
40
|
end
|
47
41
|
|
48
|
-
def
|
49
|
-
@error && @error != HANDLER_OK
|
50
|
-
end
|
51
|
-
|
52
|
-
def error_status(error, status = nil)
|
53
|
-
@error = error
|
54
|
-
@status = status
|
55
|
-
end
|
56
|
-
|
57
|
-
def result(type:, data: nil, error: nil)
|
42
|
+
def result(type:, data: nil)
|
58
43
|
@type = type
|
59
44
|
@data = data
|
60
|
-
@error = error if error
|
61
45
|
end
|
62
46
|
|
63
47
|
def result_json(data)
|
@@ -72,17 +56,12 @@ module Tzispa
|
|
72
56
|
result type: :redirect, data: data
|
73
57
|
end
|
74
58
|
|
75
|
-
def
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
def message
|
80
|
-
I18n.t(error_id, default: error.to_s) if error
|
81
|
-
end
|
82
|
-
|
83
|
-
def error_id
|
84
|
-
"#{self.class.name.dottize}.#{error}" if error
|
59
|
+
def error_status(code, http_status = nil)
|
60
|
+
@error = code
|
61
|
+
@status = http_status
|
62
|
+
result_json error_message: message, error_code: code
|
85
63
|
end
|
64
|
+
alias result_error error_status
|
86
65
|
|
87
66
|
def run!(verb, predicate = nil)
|
88
67
|
raise UnknownHandlerVerb.new(verb, self.class.name) unless provides? verb
|
@@ -0,0 +1,76 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'i18n'
|
4
|
+
require 'tzispa/utils/string'
|
5
|
+
|
6
|
+
module Tzispa
|
7
|
+
module Api
|
8
|
+
module HandlerError
|
9
|
+
|
10
|
+
using Tzispa::Utils::TzString
|
11
|
+
|
12
|
+
HANDLER_OK = :ok
|
13
|
+
|
14
|
+
def error?
|
15
|
+
@error && @error != HANDLER_OK
|
16
|
+
end
|
17
|
+
|
18
|
+
def message
|
19
|
+
I18n.t(error_id, default: error.to_s) if error
|
20
|
+
end
|
21
|
+
|
22
|
+
def error_id
|
23
|
+
"#{self.class.name.dottize}.#{error}" if error
|
24
|
+
end
|
25
|
+
|
26
|
+
def http_bad_request(error = nil)
|
27
|
+
error_status error || :bad_requuest, 400
|
28
|
+
end
|
29
|
+
|
30
|
+
def http_unauthorized(error = nil)
|
31
|
+
error_status error || :unauthorized, 401
|
32
|
+
end
|
33
|
+
|
34
|
+
def http_forbidden(error = nil)
|
35
|
+
error_status error || :forbidden, 403
|
36
|
+
end
|
37
|
+
|
38
|
+
def http_not_found(error = nil)
|
39
|
+
error_status error || :not_found, 404
|
40
|
+
end
|
41
|
+
|
42
|
+
def http_not_aceptable(error = nil)
|
43
|
+
error_status error || :not_acceptable, 406
|
44
|
+
end
|
45
|
+
|
46
|
+
def http_conflict(error = nil)
|
47
|
+
error_status error || :conflict, 409
|
48
|
+
end
|
49
|
+
|
50
|
+
def http_gone(error = nil)
|
51
|
+
error_status error || :gone, 410
|
52
|
+
end
|
53
|
+
|
54
|
+
def http_token_required(error = nil)
|
55
|
+
error_status error || :token_required, 499
|
56
|
+
end
|
57
|
+
|
58
|
+
def http_server_error(error = nil)
|
59
|
+
error_status error || :internal_server_error, 500
|
60
|
+
end
|
61
|
+
|
62
|
+
def http_not_implemented(error = nil)
|
63
|
+
error_status error || :not_implemented, 501
|
64
|
+
end
|
65
|
+
|
66
|
+
def http_bad_gateway(error = nil)
|
67
|
+
error_status error || :bad_gateway, 502
|
68
|
+
end
|
69
|
+
|
70
|
+
def http_service_unavailable(error = nil)
|
71
|
+
error_status error || :service_unavailable, 503
|
72
|
+
end
|
73
|
+
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
@@ -17,6 +17,10 @@ module Tzispa
|
|
17
17
|
|
18
18
|
include Tzispa::Helpers::Response
|
19
19
|
|
20
|
+
def initialize(app, callmethod = :dispatch!)
|
21
|
+
super(app, callmethod, false)
|
22
|
+
end
|
23
|
+
|
20
24
|
def dispatch!
|
21
25
|
verb = context.router_params[:verb]
|
22
26
|
predicate = context.router_params[:predicate]
|
@@ -47,7 +51,7 @@ module Tzispa
|
|
47
51
|
end
|
48
52
|
|
49
53
|
def empty(handler)
|
50
|
-
api_response
|
54
|
+
api_response handler.status
|
51
55
|
end
|
52
56
|
|
53
57
|
def redirect(handler)
|
@@ -56,23 +60,18 @@ module Tzispa
|
|
56
60
|
end
|
57
61
|
|
58
62
|
def html(handler)
|
59
|
-
content = handler.
|
60
|
-
api_response
|
63
|
+
content = handler.data unless handler.error?
|
64
|
+
api_response handler.status, content, :htm
|
61
65
|
end
|
62
66
|
|
63
67
|
def json(handler)
|
64
|
-
content =
|
65
|
-
|
66
|
-
error_code: handler.error }.to_json
|
67
|
-
else
|
68
|
-
handler.data.is_a?(::String) ? JSON.parse(handler.data) : handler.data.to_json
|
69
|
-
end
|
70
|
-
api_response :json, content, handler.status, handler.error
|
68
|
+
content = handler.data.is_a?(::String) ? JSON.parse(handler.data) : handler.data.to_json
|
69
|
+
api_response handler.status, content, :json
|
71
70
|
end
|
72
71
|
|
73
72
|
def text(handler)
|
74
|
-
content = handler.
|
75
|
-
api_response
|
73
|
+
content = handler.data unless handler.error?
|
74
|
+
api_response handler.status, content, :text
|
76
75
|
end
|
77
76
|
|
78
77
|
def download(handler)
|
@@ -83,11 +82,10 @@ module Tzispa
|
|
83
82
|
context.flash << message if config.sessions&.enabled
|
84
83
|
end
|
85
84
|
|
86
|
-
def api_response(
|
85
|
+
def api_response(status, content = nil, type = nil)
|
87
86
|
content_type(type) if type
|
88
87
|
response.body = content if content
|
89
88
|
response.status = status if status
|
90
|
-
api_headers error
|
91
89
|
end
|
92
90
|
|
93
91
|
def request_method
|
@@ -113,16 +111,6 @@ module Tzispa
|
|
113
111
|
.constantize
|
114
112
|
end
|
115
113
|
end
|
116
|
-
|
117
|
-
private
|
118
|
-
|
119
|
-
def api_headers(error = nil)
|
120
|
-
handler = context.router_params[:handler]
|
121
|
-
verb = context.router_params[:verb]
|
122
|
-
predicate = context.router_params[:predicate]
|
123
|
-
response['X-API'] = "#{handler}:#{verb}:#{predicate}"
|
124
|
-
response['X-API-SR'] = error.to_s if error
|
125
|
-
end
|
126
114
|
end
|
127
115
|
|
128
116
|
end
|
@@ -17,13 +17,14 @@ module Tzispa
|
|
17
17
|
include Tzispa::Helpers::Hooks::Before
|
18
18
|
include Tzispa::Helpers::Hooks::After
|
19
19
|
|
20
|
-
attr_reader :context, :application, :callmethod
|
20
|
+
attr_reader :context, :application, :callmethod, :custom_error
|
21
21
|
def_delegators :@context, :request, :response, :config,
|
22
22
|
:login_redirect, :unauthorized_but_logged
|
23
23
|
|
24
|
-
def initialize(app, callmethod = nil)
|
24
|
+
def initialize(app, callmethod = nil, custom_error = true)
|
25
25
|
@callmethod = callmethod
|
26
26
|
@application = app
|
27
|
+
@custom_error = custom_error
|
27
28
|
end
|
28
29
|
|
29
30
|
def call(env)
|
@@ -46,32 +47,32 @@ module Tzispa
|
|
46
47
|
prepare_response(500, error: ex)
|
47
48
|
end
|
48
49
|
|
49
|
-
def prepare_response(status,
|
50
|
+
def prepare_response(status, error: nil)
|
50
51
|
response.status = status if status.is_a?(Integer)
|
51
52
|
if context.client_error?
|
52
53
|
prepare_client_error(status, error)
|
53
54
|
elsif context.server_error?
|
54
55
|
prepare_server_error(status, error)
|
55
|
-
elsif content
|
56
|
-
response.body = content
|
57
56
|
end
|
58
57
|
end
|
59
58
|
|
60
59
|
def prepare_client_error(status, error = nil)
|
61
60
|
status.tap do |code|
|
62
61
|
context.logger.info log_format(code, error.to_s) if error
|
63
|
-
response.body = error_page(context.domain, status: code)
|
62
|
+
response.body = error_page(context.domain, status: code) if custom_error
|
64
63
|
end
|
65
64
|
end
|
66
65
|
|
67
66
|
def prepare_server_error(status, error = nil)
|
68
67
|
status.tap do |code|
|
69
68
|
context.logger.error log_format(code, error_log(error)) if error
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
69
|
+
if custom_error
|
70
|
+
response.body = if error && Tzispa::Environment.development?
|
71
|
+
debug_info(error)
|
72
|
+
else
|
73
|
+
error_page(context.domain, status: code)
|
74
|
+
end
|
75
|
+
end
|
75
76
|
end
|
76
77
|
end
|
77
78
|
|
data/lib/tzispa/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tzispa
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.7.
|
4
|
+
version: 0.7.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Juan Antonio Piñero
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-06-
|
11
|
+
date: 2017-06-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -184,6 +184,7 @@ files:
|
|
184
184
|
- bin/tzispa
|
185
185
|
- lib/tzispa.rb
|
186
186
|
- lib/tzispa/api/handler.rb
|
187
|
+
- lib/tzispa/api/handler_error.rb
|
187
188
|
- lib/tzispa/app.rb
|
188
189
|
- lib/tzispa/bin/tzispa
|
189
190
|
- lib/tzispa/cli.rb
|