shogun 6.2.1 → 6.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/shogun/version.rb +1 -1
- data/scaffold/generate/resource.rb +15 -0
- data/scaffold/generate/resource/create.rb +10 -0
- data/scaffold/generate/resource/create/authorizer.rb +21 -0
- data/scaffold/generate/resource/create/control.rb +27 -0
- data/scaffold/generate/resource/create/denormalizer.rb +31 -0
- data/scaffold/generate/resource/create/normalizer.rb +22 -0
- data/scaffold/generate/resource/create/validator.rb +14 -0
- data/scaffold/generate/resource/destroy.rb +11 -0
- data/scaffold/generate/resource/destroy/authorizer.rb +21 -0
- data/scaffold/generate/resource/destroy/control.rb +37 -0
- data/scaffold/generate/resource/destroy/denormalizer.rb +31 -0
- data/scaffold/generate/resource/destroy/normalizer.rb +28 -0
- data/scaffold/generate/resource/destroy/validator.rb +20 -0
- data/scaffold/generate/resource/endpoint.rb +16 -0
- data/scaffold/generate/resource/list.rb +10 -0
- data/scaffold/generate/resource/list/authorizer.rb +21 -0
- data/scaffold/generate/resource/list/control.rb +26 -0
- data/scaffold/generate/resource/list/denormalizer.rb +33 -0
- data/scaffold/generate/resource/list/normalizer.rb +58 -0
- data/scaffold/generate/resource/list/validator.rb +24 -0
- data/scaffold/generate/resource/model.rb +21 -0
- data/scaffold/generate/resource/null.rb +11 -0
- data/scaffold/generate/resource/presenter.rb +24 -0
- data/scaffold/generate/resource/show.rb +10 -0
- data/scaffold/generate/resource/show/authorizer.rb +21 -0
- data/scaffold/generate/resource/show/control.rb +31 -0
- data/scaffold/generate/resource/show/denormalizer.rb +18 -0
- data/scaffold/generate/resource/show/normalizer.rb +37 -0
- data/scaffold/generate/resource/show/validator.rb +21 -0
- data/scaffold/generate/resource/update.rb +11 -0
- data/scaffold/generate/resource/update/authorizer.rb +21 -0
- data/scaffold/generate/resource/update/control.rb +37 -0
- data/scaffold/generate/resource/update/denormalizer.rb +31 -0
- data/scaffold/generate/resource/update/normalizer.rb +37 -0
- data/scaffold/generate/resource/update/validator.rb +14 -0
- data/scaffold/new/project/Envfile +17 -0
- data/scaffold/new/project/Gemfile +18 -0
- data/scaffold/new/project/LICENSE +22 -0
- data/scaffold/new/project/Procfile +1 -0
- data/scaffold/new/project/README.md +32 -0
- data/scaffold/new/project/Rakefile +9 -0
- data/scaffold/new/project/config.ru +8 -0
- data/scaffold/new/project/config/puma.rb +167 -0
- data/scaffold/new/project/gitignore +39 -0
- data/scaffold/new/project/lib/namespace.rb +29 -0
- data/scaffold/new/project/lib/namespace/accounts.rb +15 -0
- data/scaffold/new/project/lib/namespace/accounts/create.rb +10 -0
- data/scaffold/new/project/lib/namespace/accounts/create/control.rb +27 -0
- data/scaffold/new/project/lib/namespace/accounts/create/denormalizer.rb +35 -0
- data/scaffold/new/project/lib/namespace/accounts/create/normalizer.rb +42 -0
- data/scaffold/new/project/lib/namespace/accounts/create/validator.rb +34 -0
- data/scaffold/new/project/lib/namespace/accounts/destroy.rb +11 -0
- data/scaffold/new/project/lib/namespace/accounts/destroy/authorizer.rb +21 -0
- data/scaffold/new/project/lib/namespace/accounts/destroy/control.rb +37 -0
- data/scaffold/new/project/lib/namespace/accounts/destroy/denormalizer.rb +35 -0
- data/scaffold/new/project/lib/namespace/accounts/destroy/normalizer.rb +28 -0
- data/scaffold/new/project/lib/namespace/accounts/destroy/validator.rb +20 -0
- data/scaffold/new/project/lib/namespace/accounts/endpoint.rb +16 -0
- data/scaffold/new/project/lib/namespace/accounts/list.rb +10 -0
- data/scaffold/new/project/lib/namespace/accounts/list/control.rb +26 -0
- data/scaffold/new/project/lib/namespace/accounts/list/denormalizer.rb +37 -0
- data/scaffold/new/project/lib/namespace/accounts/list/normalizer.rb +58 -0
- data/scaffold/new/project/lib/namespace/accounts/list/validator.rb +24 -0
- data/scaffold/new/project/lib/namespace/accounts/model.rb +49 -0
- data/scaffold/new/project/lib/namespace/accounts/null.rb +11 -0
- data/scaffold/new/project/lib/namespace/accounts/presenter.rb +40 -0
- data/scaffold/new/project/lib/namespace/accounts/show.rb +10 -0
- data/scaffold/new/project/lib/namespace/accounts/show/control.rb +31 -0
- data/scaffold/new/project/lib/namespace/accounts/show/denormalizer.rb +22 -0
- data/scaffold/new/project/lib/namespace/accounts/show/normalizer.rb +37 -0
- data/scaffold/new/project/lib/namespace/accounts/show/validator.rb +21 -0
- data/scaffold/new/project/lib/namespace/accounts/update.rb +11 -0
- data/scaffold/new/project/lib/namespace/accounts/update/authorizer.rb +21 -0
- data/scaffold/new/project/lib/namespace/accounts/update/control.rb +37 -0
- data/scaffold/new/project/lib/namespace/accounts/update/denormalizer.rb +35 -0
- data/scaffold/new/project/lib/namespace/accounts/update/normalizer.rb +52 -0
- data/scaffold/new/project/lib/namespace/accounts/update/validator.rb +47 -0
- data/scaffold/new/project/lib/namespace/errors.rb +8 -0
- data/scaffold/new/project/lib/namespace/errors/denormalizer.rb +24 -0
- data/scaffold/new/project/lib/namespace/errors/presenter.rb +16 -0
- data/scaffold/new/project/lib/namespace/server.rb +54 -0
- data/scaffold/new/project/lib/namespace/sessions.rb +11 -0
- data/scaffold/new/project/lib/namespace/sessions/create.rb +11 -0
- data/scaffold/new/project/lib/namespace/sessions/create/authorizer.rb +19 -0
- data/scaffold/new/project/lib/namespace/sessions/create/control.rb +26 -0
- data/scaffold/new/project/lib/namespace/sessions/create/denormalizer.rb +30 -0
- data/scaffold/new/project/lib/namespace/sessions/create/normalizer.rb +31 -0
- data/scaffold/new/project/lib/namespace/sessions/create/validator.rb +21 -0
- data/scaffold/new/project/lib/namespace/sessions/destroy.rb +10 -0
- data/scaffold/new/project/lib/namespace/sessions/destroy/control.rb +32 -0
- data/scaffold/new/project/lib/namespace/sessions/destroy/denormalizer.rb +29 -0
- data/scaffold/new/project/lib/namespace/sessions/destroy/normalizer.rb +20 -0
- data/scaffold/new/project/lib/namespace/sessions/destroy/validator.rb +16 -0
- data/scaffold/new/project/lib/namespace/sessions/endpoint.rb +13 -0
- data/scaffold/new/project/lib/namespace/sessions/model.rb +25 -0
- data/scaffold/new/project/lib/namespace/sessions/presenter.rb +40 -0
- data/scaffold/new/project/spec/lib/namespace/accounts/create/control_spec.rb +1 -0
- data/scaffold/new/project/spec/lib/namespace/accounts/create/denormalizer_spec.rb +1 -0
- data/scaffold/new/project/spec/lib/namespace/accounts/create/normalizer_spec.rb +1 -0
- data/scaffold/new/project/spec/lib/namespace/accounts/create/validator_spec.rb +1 -0
- data/scaffold/new/project/spec/lib/namespace/accounts/destroy/control_spec.rb +1 -0
- data/scaffold/new/project/spec/lib/namespace/accounts/destroy/denormalizer_spec.rb +1 -0
- data/scaffold/new/project/spec/lib/namespace/accounts/destroy/normalizer_spec.rb +1 -0
- data/scaffold/new/project/spec/lib/namespace/accounts/destroy/validator_spec.rb +1 -0
- data/scaffold/new/project/spec/lib/namespace/accounts/endpoint_spec.rb +1 -0
- data/scaffold/new/project/spec/lib/namespace/accounts/list/control_spec.rb +1 -0
- data/scaffold/new/project/spec/lib/namespace/accounts/list/denormalizer_spec.rb +1 -0
- data/scaffold/new/project/spec/lib/namespace/accounts/list/normalizer_spec.rb +1 -0
- data/scaffold/new/project/spec/lib/namespace/accounts/list/validator_spec.rb +1 -0
- data/scaffold/new/project/spec/lib/namespace/accounts/model_spec.rb +1 -0
- data/scaffold/new/project/spec/lib/namespace/accounts/presenter_spec.rb +1 -0
- data/scaffold/new/project/spec/lib/namespace/accounts/show/control_spec.rb +1 -0
- data/scaffold/new/project/spec/lib/namespace/accounts/show/denormalizer_spec.rb +1 -0
- data/scaffold/new/project/spec/lib/namespace/accounts/show/normalizer_spec.rb +1 -0
- data/scaffold/new/project/spec/lib/namespace/accounts/show/validator_spec.rb +1 -0
- data/scaffold/new/project/spec/lib/namespace/accounts/update/control_spec.rb +1 -0
- data/scaffold/new/project/spec/lib/namespace/accounts/update/denormalizer_spec.rb +1 -0
- data/scaffold/new/project/spec/lib/namespace/accounts/update/normalizer_spec.rb +1 -0
- data/scaffold/new/project/spec/lib/namespace/accounts/update/validator_spec.rb +1 -0
- data/scaffold/new/project/spec/lib/namespace/errors/denormalizer_spec.rb +1 -0
- data/scaffold/new/project/spec/lib/namespace/errors/presenter_spec.rb +1 -0
- data/scaffold/new/project/spec/lib/namespace/errors/presenters_spec.rb +1 -0
- data/scaffold/new/project/spec/lib/namespace/model_spec.rb +1 -0
- data/scaffold/new/project/spec/lib/namespace/server_spec.rb +1 -0
- data/scaffold/new/project/spec/lib/namespace/sessions/create/control_spec.rb +1 -0
- data/scaffold/new/project/spec/lib/namespace/sessions/create/denormalizer_spec.rb +1 -0
- data/scaffold/new/project/spec/lib/namespace/sessions/create/normalizer_spec.rb +1 -0
- data/scaffold/new/project/spec/lib/namespace/sessions/create/validator_spec.rb +1 -0
- data/scaffold/new/project/spec/lib/namespace/sessions/destroy/control_spec.rb +1 -0
- data/scaffold/new/project/spec/lib/namespace/sessions/destroy/denormalizer_spec.rb +1 -0
- data/scaffold/new/project/spec/lib/namespace/sessions/destroy/normalizer_spec.rb +1 -0
- data/scaffold/new/project/spec/lib/namespace/sessions/destroy/validator_spec.rb +1 -0
- data/scaffold/new/project/spec/lib/namespace/sessions/endpoint_spec.rb +1 -0
- data/scaffold/new/project/spec/lib/namespace/sessions/model_spec.rb +1 -0
- data/scaffold/new/project/spec/lib/namespace/sessions/presenter_spec.rb +1 -0
- data/scaffold/new/project/spec/lib/namespace_spec.rb +7 -0
- data/scaffold/new/project/spec/spec_helper.rb +3 -0
- metadata +138 -1
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
module {{namespace_module}}
|
|
2
|
+
module Accounts
|
|
3
|
+
module Create
|
|
4
|
+
class Normalizer
|
|
5
|
+
include Shogun::Normalizer
|
|
6
|
+
include Accounts
|
|
7
|
+
|
|
8
|
+
ATTRIBUTES = %w|signature username name email password|
|
|
9
|
+
|
|
10
|
+
def initialize(raw:)
|
|
11
|
+
if raw[namespace]
|
|
12
|
+
@signature = raw[namespace]["signature"]
|
|
13
|
+
@name = raw[namespace]["name"]
|
|
14
|
+
@email = raw[namespace]["email"]
|
|
15
|
+
@username = raw[namespace]["username"]
|
|
16
|
+
@password = raw[namespace]["password"]
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def signature
|
|
21
|
+
@signature.strip if @signature.is_a?(String)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def name
|
|
25
|
+
@name.strip if @name.is_a?(String)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def email
|
|
29
|
+
@email.strip if @email.is_a?(String)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def username
|
|
33
|
+
@username.strip if @username.is_a?(String)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def password
|
|
37
|
+
@password
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
module {{namespace_module}}
|
|
2
|
+
module Accounts
|
|
3
|
+
module Create
|
|
4
|
+
class Validator
|
|
5
|
+
include Shogun::Validator
|
|
6
|
+
include Accounts
|
|
7
|
+
|
|
8
|
+
EMAIL_MATCH = /^[^@\s]+@([^@\s]+\.)+[^@\s]+$/
|
|
9
|
+
PASSWORD_RANGE = 8..1024
|
|
10
|
+
SIGNATURE_RANGE = 2..64
|
|
11
|
+
USERNAME_MATCH = /^\S*$/
|
|
12
|
+
USERNAME_RANGE = 2..64
|
|
13
|
+
|
|
14
|
+
def initialize(data:)
|
|
15
|
+
@verifiers = [
|
|
16
|
+
Shogun::Verifier::Presence.new(data: data.email, name: "email"),
|
|
17
|
+
Shogun::Verifier::Match.new(data: data.email, name: "email", comparison: EMAIL_MATCH),
|
|
18
|
+
Shogun::Verifier::Uniqueness.new(data: data.email, name: "email", list: Model.pluck(:email)),
|
|
19
|
+
Shogun::Verifier::Presence.new(data: data.name, name: "name"),
|
|
20
|
+
Shogun::Verifier::Presence.new(data: data.password, name: "password"),
|
|
21
|
+
Shogun::Verifier::Range.new(data: data.password, name: "password", range: PASSWORD_RANGE),
|
|
22
|
+
Shogun::Verifier::Presence.new(data: data.signature, name: "signature"),
|
|
23
|
+
Shogun::Verifier::Range.new(data: data.signature, name: "signature", range: SIGNATURE_RANGE),
|
|
24
|
+
Shogun::Verifier::Uniqueness.new(data: data.signature, name: "signature", list: Model.pluck(:signature)),
|
|
25
|
+
Shogun::Verifier::Match.new(data: data.username, name: "username", comparison: USERNAME_MATCH),
|
|
26
|
+
Shogun::Verifier::Range.new(data: data.username, name: "username", range: USERNAME_RANGE),
|
|
27
|
+
Shogun::Verifier::Presence.new(data: data.username, name: "username"),
|
|
28
|
+
Shogun::Verifier::Uniqueness.new(data: data.username, name: "username", list: Model.pluck(:username))
|
|
29
|
+
]
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
module {{namespace_module}}
|
|
2
|
+
module Accounts
|
|
3
|
+
module Destroy
|
|
4
|
+
require_relative "destroy/control"
|
|
5
|
+
require_relative "destroy/authorizer"
|
|
6
|
+
require_relative "destroy/denormalizer"
|
|
7
|
+
require_relative "destroy/normalizer"
|
|
8
|
+
require_relative "destroy/validator"
|
|
9
|
+
end
|
|
10
|
+
end
|
|
11
|
+
end
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
module {{namespace_module}}
|
|
2
|
+
module Accounts
|
|
3
|
+
module Destroy
|
|
4
|
+
class Authorizer
|
|
5
|
+
include Shogun::Authorizer
|
|
6
|
+
include Accounts
|
|
7
|
+
|
|
8
|
+
def initialize(requester:, resource:)
|
|
9
|
+
if requester
|
|
10
|
+
@requester = requester.account
|
|
11
|
+
end
|
|
12
|
+
@resource = resource
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def allowed?
|
|
16
|
+
@requester == @resource
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
module {{namespace_module}}
|
|
2
|
+
module Accounts
|
|
3
|
+
module Destroy
|
|
4
|
+
class Control
|
|
5
|
+
include Shogun::Control
|
|
6
|
+
include Accounts
|
|
7
|
+
|
|
8
|
+
def initialize(payload:)
|
|
9
|
+
@normalizer = Normalizer.new(raw: payload.to_hash)
|
|
10
|
+
@validator = Validator.new(data: @normalizer)
|
|
11
|
+
|
|
12
|
+
if @validator.valid?
|
|
13
|
+
@session = Sessions::Model.find_by(id: @normalizer.session)
|
|
14
|
+
@model = Model.find_by(id: @normalizer.id)
|
|
15
|
+
@authorizer = Authorizer.new(requester: @session, resource: @model)
|
|
16
|
+
|
|
17
|
+
case
|
|
18
|
+
when @model && @authorizer.allowed?
|
|
19
|
+
@model.destroy
|
|
20
|
+
@presenter = Presenter.new(resource: @model, meta: @normalizer.as_meta)
|
|
21
|
+
@denormalizer = Denormalizer.new(object: @presenter)
|
|
22
|
+
@status = OK
|
|
23
|
+
when @model && @authorizer.denied?
|
|
24
|
+
@status = UNAUTHORIZED
|
|
25
|
+
else
|
|
26
|
+
@status = NOT_FOUND
|
|
27
|
+
end
|
|
28
|
+
else
|
|
29
|
+
@presenters = Shogun::Presenters.new(resources: @validator.invalids, cast: Errors::Presenter)
|
|
30
|
+
@denormalizer = Errors::Denormalizer.new(object: @presenters)
|
|
31
|
+
@status = UNPROCESSABLE_ENTITY
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
module {{namespace_module}}
|
|
2
|
+
module Accounts
|
|
3
|
+
module Destroy
|
|
4
|
+
class Denormalizer
|
|
5
|
+
include Shogun::Denormalizer
|
|
6
|
+
include Accounts
|
|
7
|
+
|
|
8
|
+
MAPPING = {
|
|
9
|
+
"id" => "id",
|
|
10
|
+
"signature" => "signature",
|
|
11
|
+
"username" => "username",
|
|
12
|
+
"email" => "email",
|
|
13
|
+
"name" => "name",
|
|
14
|
+
"created_at" => "created_at",
|
|
15
|
+
"destroyed_at" => "destroyed_at",
|
|
16
|
+
"updated_at" => "updated_at",
|
|
17
|
+
"href" => "href"
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
def initialize(object:)
|
|
21
|
+
@object = object
|
|
22
|
+
@meta = object.meta
|
|
23
|
+
@links = object.links
|
|
24
|
+
@linked = object.linked
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
private def document
|
|
28
|
+
mapping.inject({}) do |hash, (field, name)|
|
|
29
|
+
hash.merge(field => @object.public_send(name))
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
module {{namespace_module}}
|
|
2
|
+
module Accounts
|
|
3
|
+
module Destroy
|
|
4
|
+
class Normalizer
|
|
5
|
+
include Shogun::Normalizer
|
|
6
|
+
include Accounts
|
|
7
|
+
|
|
8
|
+
METADATA = %w|id|
|
|
9
|
+
|
|
10
|
+
def initialize(raw:)
|
|
11
|
+
@id = raw["id"]
|
|
12
|
+
|
|
13
|
+
if raw["rack.authentication"] && raw["rack.authentication"]["sessions"]
|
|
14
|
+
@session = raw["rack.authentication"]["sessions"]["id"]
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def id
|
|
19
|
+
@id
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def session
|
|
23
|
+
@session
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
module {{namespace_module}}
|
|
2
|
+
module Accounts
|
|
3
|
+
module Destroy
|
|
4
|
+
class Validator
|
|
5
|
+
include Shogun::Validator
|
|
6
|
+
include Accounts
|
|
7
|
+
|
|
8
|
+
ID_MATCH = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/
|
|
9
|
+
|
|
10
|
+
def initialize(data:)
|
|
11
|
+
@verifiers = [
|
|
12
|
+
Shogun::Verifier::Presence.new(data: data.id, name: "id"),
|
|
13
|
+
Shogun::Verifier::Match.new(data: data.id, name: "id", comparison: ID_MATCH),
|
|
14
|
+
Shogun::Verifier::Presence.new(data: data.session, name: "session")
|
|
15
|
+
]
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
module {{namespace_module}}
|
|
2
|
+
module Accounts
|
|
3
|
+
class Endpoint
|
|
4
|
+
include Accounts
|
|
5
|
+
include Shogun::Endpoint
|
|
6
|
+
|
|
7
|
+
def initialize(router:)
|
|
8
|
+
router.push route: route(verb: :get, control: List::Control)
|
|
9
|
+
router.push route: route(verb: :post, control: Create::Control)
|
|
10
|
+
router.push route: route(verb: :get, matcher: /(?<id>.+)/, control: Show::Control)
|
|
11
|
+
router.push route: route(verb: :patch, matcher: /(?<id>.+)/, control: Update::Control)
|
|
12
|
+
router.push route: route(verb: :delete, matcher: /(?<id>.+)/, control: Destroy::Control)
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
module {{namespace_module}}
|
|
2
|
+
module Accounts
|
|
3
|
+
module List
|
|
4
|
+
class Control
|
|
5
|
+
include Shogun::Control
|
|
6
|
+
include Accounts
|
|
7
|
+
|
|
8
|
+
def initialize(payload:)
|
|
9
|
+
@normalizer = Normalizer.new(raw: payload.to_hash)
|
|
10
|
+
@validator = Validator.new(data: @normalizer)
|
|
11
|
+
|
|
12
|
+
if @validator.valid?
|
|
13
|
+
@models = Model.limit(@normalizer.limit).where(@normalizer.filters).includes(@normalizer.includes)
|
|
14
|
+
@presenters = Shogun::Presenters.new(resources: @models, cast: Presenter, includes: @normalizer.includes, meta: @normalizer.as_meta)
|
|
15
|
+
@denormalizer = Denormalizer.new(object: @presenters, fields: @normalizer.fields)
|
|
16
|
+
@status = OK
|
|
17
|
+
else
|
|
18
|
+
@presenters = Shogun::Presenters.new(resources: @validator.invalids, cast: Errors::Presenter)
|
|
19
|
+
@denormalizer = Errors::Denormalizer.new(object: @presenters)
|
|
20
|
+
@status = UNPROCESSABLE_ENTITY
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
module {{namespace_module}}
|
|
2
|
+
module Accounts
|
|
3
|
+
module List
|
|
4
|
+
class Denormalizer
|
|
5
|
+
include Shogun::Denormalizer
|
|
6
|
+
include Accounts
|
|
7
|
+
|
|
8
|
+
MAPPING = {
|
|
9
|
+
"id" => "id",
|
|
10
|
+
"signature" => "signature",
|
|
11
|
+
"username" => "username",
|
|
12
|
+
"email" => "email",
|
|
13
|
+
"name" => "name",
|
|
14
|
+
"created_at" => "created_at",
|
|
15
|
+
"destroyed_at" => "destroyed_at",
|
|
16
|
+
"updated_at" => "updated_at",
|
|
17
|
+
"href" => "href",
|
|
18
|
+
LINKS_KEY => "links",
|
|
19
|
+
META_KEY => "meta",
|
|
20
|
+
LINKED_KEY => "linked"
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
private def document
|
|
24
|
+
@object.map do |object|
|
|
25
|
+
if @fields.one?
|
|
26
|
+
object.public_send(@fields.first)
|
|
27
|
+
else
|
|
28
|
+
@fields.inject({}) do |hash, field|
|
|
29
|
+
hash.merge(field => object.public_send(mapping[field]))
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
module {{namespace_module}}
|
|
2
|
+
module Accounts
|
|
3
|
+
module List
|
|
4
|
+
class Normalizer
|
|
5
|
+
include Shogun::Normalizer
|
|
6
|
+
include Accounts
|
|
7
|
+
|
|
8
|
+
METADATA = %w|limit filters includes fields|
|
|
9
|
+
LIST_DELIMITER = /\s*,\s*/
|
|
10
|
+
DEFAULT_FIELD = "id"
|
|
11
|
+
AUTOMATIC_FIELDS = %w|id href meta links linked|
|
|
12
|
+
|
|
13
|
+
def initialize(raw:)
|
|
14
|
+
@limit = raw["limit"]
|
|
15
|
+
@filters = raw["filter"]
|
|
16
|
+
@includes = raw["include"]
|
|
17
|
+
@fields = raw["fields"]
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def limit
|
|
21
|
+
Integer(@limit) rescue 10
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def filters
|
|
25
|
+
if @filters.is_a?(Hash)
|
|
26
|
+
@filters
|
|
27
|
+
else
|
|
28
|
+
Hash.new
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def includes
|
|
33
|
+
case @includes
|
|
34
|
+
when String
|
|
35
|
+
@includes.split(LIST_DELIMITER)
|
|
36
|
+
when Array
|
|
37
|
+
@includes
|
|
38
|
+
else
|
|
39
|
+
Array.new
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def fields
|
|
44
|
+
case
|
|
45
|
+
when @fields.is_a?(Array)
|
|
46
|
+
@fields + AUTOMATIC_FIELDS
|
|
47
|
+
when @fields.is_a?(String) && @fields.match(LIST_DELIMITER)
|
|
48
|
+
@fields.split(LIST_DELIMITER) + AUTOMATIC_FIELDS
|
|
49
|
+
when @fields.is_a?(String)
|
|
50
|
+
[@fields.strip] + AUTOMATIC_FIELDS
|
|
51
|
+
else
|
|
52
|
+
[DEFAULT_FIELD]
|
|
53
|
+
end.uniq
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
module {{namespace_module}}
|
|
2
|
+
module Accounts
|
|
3
|
+
module List
|
|
4
|
+
class Validator
|
|
5
|
+
include Shogun::Validator
|
|
6
|
+
include Accounts
|
|
7
|
+
|
|
8
|
+
INCLUDES_ALLOWED = %w||
|
|
9
|
+
FIELDS_ALLOWED = %w|updated_at destroyed_at href created_at name email username signature id meta links linked|
|
|
10
|
+
FILTERS_ALLOWED = %w|username signature created_at name|
|
|
11
|
+
LIMIT_RANGE = 1..100
|
|
12
|
+
|
|
13
|
+
def initialize(data:)
|
|
14
|
+
@verifiers = [
|
|
15
|
+
Shogun::Verifier::Range.new(data: data.limit, name: "limit", range: LIMIT_RANGE),
|
|
16
|
+
Shogun::Verifier::Allowed.new(data: data.filters.keys, name: "filters", list: FILTERS_ALLOWED),
|
|
17
|
+
Shogun::Verifier::Allowed.new(data: data.includes, name: "includes", list: INCLUDES_ALLOWED),
|
|
18
|
+
Shogun::Verifier::Allowed.new(data: data.fields, name: "fields", list: FIELDS_ALLOWED)
|
|
19
|
+
]
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
module {{namespace_module}}
|
|
2
|
+
module Accounts
|
|
3
|
+
class Model < ActiveRecord::Base
|
|
4
|
+
include BCrypt
|
|
5
|
+
include Accounts
|
|
6
|
+
|
|
7
|
+
self.table_name = NAMESPACE
|
|
8
|
+
|
|
9
|
+
has_many :sessions, class_name: "{{namespace_module}}::Sessions::Model", foreign_key: "account_id", dependent: :destroy
|
|
10
|
+
|
|
11
|
+
def self.setup!
|
|
12
|
+
ActiveRecord::Migration.create_table table_name, force: true, id: :uuid do |table|
|
|
13
|
+
table.string :name, null: false
|
|
14
|
+
table.index :name, unique: true
|
|
15
|
+
|
|
16
|
+
table.string :username, null: false
|
|
17
|
+
table.index :username, unique: true
|
|
18
|
+
|
|
19
|
+
table.string :email, null: false
|
|
20
|
+
table.index :email, unique: true
|
|
21
|
+
|
|
22
|
+
table.string :signature, null: false
|
|
23
|
+
table.index :signature, unique: true
|
|
24
|
+
|
|
25
|
+
table.text :bio, default: "", null: false
|
|
26
|
+
|
|
27
|
+
table.string :password, null: false
|
|
28
|
+
|
|
29
|
+
table.datetime :created_at, null: false
|
|
30
|
+
table.index :created_at
|
|
31
|
+
|
|
32
|
+
table.datetime :destroyed_at
|
|
33
|
+
table.index :destroyed_at
|
|
34
|
+
|
|
35
|
+
table.datetime :updated_at
|
|
36
|
+
table.index :updated_at
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def password
|
|
41
|
+
Password.new(super)
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def password=(value)
|
|
45
|
+
super(Password.create(value))
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|