sample_core_api 0.0.2.dev → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/rest/rest_error_handler.rb +8 -5
- data/lib/rest/rest_response_model.rb +17 -0
- data/lib/rest/rest_service.rb +12 -3
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: edad072b851978a00e58622c6705a933f6b625d68ad124c226804a35757cbe7e
|
4
|
+
data.tar.gz: 7feaa4ee7d428f77c47aa76aaad13cfb02178d567dbd5e6307d4943d5abece74
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1cbf877c552d4090b6341c483ae0966c7db069e4e6739a1ae59d530c8dd830a94e6b000343393088ca515504f889457f8d79c4d57457e3a1ca02600d311a2812
|
7
|
+
data.tar.gz: f50b91c2dfc7dfe8b12076c9d7e1658a7c5eec63a4db9bce8a5d835a7a2f5f7a0954e27ebfbbf027a4e0e82633e2c39296b0579e7db5be78c7f45bc1d24c5a05
|
@@ -1,25 +1,28 @@
|
|
1
1
|
require 'sinatra/base'
|
2
2
|
require_relative 'rest_common_error'
|
3
|
+
require_relative 'rest_response_model'
|
4
|
+
|
3
5
|
|
4
6
|
class RestApplication < Sinatra::Base
|
7
|
+
extend ::RestResponseModel
|
8
|
+
|
5
9
|
error CommonError::InvalidToken do
|
6
10
|
status 401
|
7
|
-
JSON
|
11
|
+
JSON RestApplication.error_response('InvalidToken')
|
8
12
|
end
|
9
13
|
|
10
14
|
error CommonError::PermissionDenied do
|
11
15
|
status 403
|
12
|
-
JSON
|
16
|
+
JSON RestApplication.error_response('PermissionDenied')
|
13
17
|
end
|
14
18
|
|
15
19
|
error CommonError::EntityNotFound do
|
16
20
|
status 404
|
17
|
-
JSON
|
21
|
+
JSON RestApplication.error_response('EntityNotFound')
|
18
22
|
end
|
19
23
|
|
20
24
|
error CommonError::InvalidData do
|
21
25
|
status 422
|
22
|
-
JSON
|
23
|
-
data: @error_message
|
26
|
+
JSON RestApplication.error_response('InvalidData', @errors)
|
24
27
|
end
|
25
28
|
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module RestResponseModel
|
2
|
+
def success_response(data=nil)
|
3
|
+
response = {
|
4
|
+
status: 'OK'
|
5
|
+
}
|
6
|
+
response[:data] = data if data
|
7
|
+
response
|
8
|
+
end
|
9
|
+
|
10
|
+
def error_response(error_code='FAILED', errors=[])
|
11
|
+
response = {
|
12
|
+
status: error_code
|
13
|
+
}
|
14
|
+
response[:errors] = errors if errors.any?
|
15
|
+
response
|
16
|
+
end
|
17
|
+
end
|
data/lib/rest/rest_service.rb
CHANGED
@@ -3,6 +3,8 @@ require "sinatra/namespace"
|
|
3
3
|
require_relative 'rest_configuration'
|
4
4
|
require_relative 'rest_error_handler'
|
5
5
|
require_relative 'rest_common_error'
|
6
|
+
require_relative 'rest_response_model'
|
7
|
+
|
6
8
|
|
7
9
|
class RestApplication < Sinatra::Base
|
8
10
|
set :server, :puma
|
@@ -25,6 +27,7 @@ end
|
|
25
27
|
|
26
28
|
class RestService < RestApplication
|
27
29
|
extend ::RestConfiguration
|
30
|
+
extend ::RestResponseModel
|
28
31
|
|
29
32
|
# Health Check for Consul
|
30
33
|
get '/health-check' do
|
@@ -41,13 +44,19 @@ class RestService < RestApplication
|
|
41
44
|
# end
|
42
45
|
# end
|
43
46
|
|
44
|
-
# Example for handling error
|
47
|
+
# # Example for handling error
|
45
48
|
# get '/health' do
|
46
|
-
# @
|
49
|
+
# @errors = [
|
50
|
+
# {
|
51
|
+
# field: 'error field',
|
52
|
+
# code: 'error code',
|
53
|
+
# message: 'error message'
|
54
|
+
# }
|
55
|
+
# ]
|
47
56
|
# raise CommonError::InvalidData
|
48
57
|
# end
|
49
58
|
|
50
59
|
not_found do
|
51
|
-
JSON
|
60
|
+
JSON RestService.error_response('EntityNotFound')
|
52
61
|
end
|
53
62
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sample_core_api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Fusin Thang
|
@@ -185,6 +185,7 @@ files:
|
|
185
185
|
- lib/rest/rest_common_error.rb
|
186
186
|
- lib/rest/rest_configuration.rb
|
187
187
|
- lib/rest/rest_error_handler.rb
|
188
|
+
- lib/rest/rest_response_model.rb
|
188
189
|
- lib/rest/rest_service.rb
|
189
190
|
- lib/sample_core_api.rb
|
190
191
|
homepage: http://rubygems.org/gems/sample_core_api
|
@@ -202,9 +203,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
202
203
|
version: 2.3.0
|
203
204
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
204
205
|
requirements:
|
205
|
-
- - "
|
206
|
+
- - ">="
|
206
207
|
- !ruby/object:Gem::Version
|
207
|
-
version:
|
208
|
+
version: '0'
|
208
209
|
requirements: []
|
209
210
|
rubyforge_project:
|
210
211
|
rubygems_version: 2.7.7
|