tramway-api 1.6.0.1 → 1.6.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Rakefile +3 -1
- data/app/controllers/tramway/api/application_controller.rb +6 -3
- data/app/controllers/tramway/api/v1/application_controller.rb +2 -0
- data/app/controllers/tramway/api/v1/records_controller.rb +21 -21
- data/app/controllers/tramway/api/v1/user_tokens_controller.rb +2 -1
- data/app/controllers/tramway/api/v1/users_controller.rb +6 -3
- data/app/forms/tramway/api/authentication_form.rb +2 -0
- data/app/helpers/tramway/api/application_helper.rb +2 -0
- data/app/jobs/tramway/api/application_job.rb +2 -0
- data/app/mailers/tramway/api/application_mailer.rb +2 -0
- data/app/models/tramway/api/application_record.rb +2 -0
- data/app/serializers/tramway/api/v1/error_serializer.rb +12 -14
- data/config/initializers/json_param_key_transform.rb +3 -1
- data/config/routes.rb +4 -2
- data/lib/tasks/tramway/api_tasks.rake +2 -0
- data/lib/tramway/api.rb +2 -0
- data/lib/tramway/api/engine.rb +2 -0
- data/lib/tramway/api/version.rb +3 -1
- metadata +15 -15
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c0ea77a64960468cd8a38627fe4400e78d591810d3123dd5b59672fcd1f37da3
|
4
|
+
data.tar.gz: 6e952b9555a2be85d5a0676ac527a7c8fff3027dfcb984a38fce99d2e145089b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3bdd5d17648fac5814452ff4d1873b6085cdff3f776a37b9ac73224a56189989876832d9ad841d082ca1b2cdb6acf05c51eb4fb2cd1ec87d0db84f64b1b5286e
|
7
|
+
data.tar.gz: 7004e9ade3569be69ad1ca94058533d425b9dea39e746ccc189034e95374be2553682658ff2768208f1f41e97f1e26f2c6937a7c12eb457c7f0413d3bc3d79ce
|
data/Rakefile
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
begin
|
2
4
|
require 'bundler/setup'
|
3
5
|
rescue LoadError
|
@@ -14,7 +16,7 @@ RDoc::Task.new(:rdoc) do |rdoc|
|
|
14
16
|
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
17
|
end
|
16
18
|
|
17
|
-
APP_RAKEFILE = File.expand_path(
|
19
|
+
APP_RAKEFILE = File.expand_path('spec/dummy/Rakefile', __dir__)
|
18
20
|
load 'rails/tasks/engine.rake'
|
19
21
|
|
20
22
|
load 'rails/tasks/statistics.rake'
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Tramway
|
2
4
|
module Api
|
3
5
|
class ApplicationController < ::Tramway::Core::ApplicationController
|
@@ -15,12 +17,13 @@ module Tramway
|
|
15
17
|
head :unauthorized
|
16
18
|
end
|
17
19
|
|
18
|
-
protected
|
19
|
-
|
20
20
|
def authenticate
|
21
|
-
return
|
21
|
+
return if current_user || params[:user_based_model].in?(Tramway::Api.user_based_models)
|
22
|
+
unauthorized
|
22
23
|
end
|
23
24
|
|
25
|
+
protected
|
26
|
+
|
24
27
|
def auth_token
|
25
28
|
if entity.respond_to? :to_token_payload
|
26
29
|
::Knock::AuthToken.new payload: entity.to_token_payload
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Tramway::Api::V1
|
2
4
|
class RecordsController < ::Tramway::Api::V1::ApplicationController
|
3
5
|
before_action :check_available_model_class
|
@@ -8,18 +10,18 @@ module Tramway::Api::V1
|
|
8
10
|
records = model_class.active.order(id: :desc).send params[:scope] || :all
|
9
11
|
records = records.full_text_search params[:search] if params[:search]
|
10
12
|
render json: records,
|
11
|
-
|
12
|
-
|
13
|
-
|
13
|
+
each_serializer: serializer_class,
|
14
|
+
include: '*',
|
15
|
+
status: :ok
|
14
16
|
end
|
15
17
|
|
16
18
|
def create
|
17
19
|
record_form = form_class.new model_class.new
|
18
20
|
if record_form.submit snake_case params[:data][:attributes]
|
19
21
|
render json: record_form.model,
|
20
|
-
|
21
|
-
|
22
|
-
|
22
|
+
serializer: serializer_class,
|
23
|
+
include: '*',
|
24
|
+
status: :created
|
23
25
|
else
|
24
26
|
render_errors_for record_form
|
25
27
|
end
|
@@ -29,9 +31,9 @@ module Tramway::Api::V1
|
|
29
31
|
record_form = form_class.new model_class.active.find params[:id]
|
30
32
|
if record_form.submit snake_case params[:data][:attributes]
|
31
33
|
render json: record_form.model,
|
32
|
-
|
33
|
-
|
34
|
-
|
34
|
+
serializer: serializer_class,
|
35
|
+
include: '*',
|
36
|
+
status: :ok
|
35
37
|
else
|
36
38
|
render_errors_for record_form
|
37
39
|
end
|
@@ -40,36 +42,34 @@ module Tramway::Api::V1
|
|
40
42
|
def show
|
41
43
|
record = model_class.active.find params[:id]
|
42
44
|
render json: record,
|
43
|
-
|
44
|
-
|
45
|
-
|
45
|
+
serializer: serializer_class,
|
46
|
+
include: '*',
|
47
|
+
status: :ok
|
46
48
|
end
|
47
49
|
|
48
50
|
def destroy
|
49
51
|
record = model_class.active.find params[:id]
|
50
52
|
record.remove
|
51
53
|
render json: record,
|
52
|
-
|
53
|
-
|
54
|
-
|
54
|
+
serializer: serializer_class,
|
55
|
+
include: '*',
|
56
|
+
status: :no_content
|
55
57
|
end
|
56
58
|
|
57
59
|
private
|
58
60
|
|
59
61
|
def check_available_model_class
|
60
|
-
head
|
62
|
+
head(:unprocessable_entity) && return unless model_class
|
61
63
|
end
|
62
64
|
|
63
65
|
def check_available_model_action
|
64
66
|
open_actions = Tramway::Api.available_models[model_class.to_s][:open]&.map(&:to_s) || []
|
65
|
-
closed_actions =
|
66
|
-
head
|
67
|
+
closed_actions = Tramway::Api.available_models[model_class.to_s][:closed]&.map(&:to_s) || []
|
68
|
+
head(:unprocessable_entity) && return unless action_name.in? open_actions + closed_actions
|
67
69
|
end
|
68
70
|
|
69
71
|
def authenticate_user_if_needed
|
70
|
-
if action_name.in?
|
71
|
-
current_user
|
72
|
-
end
|
72
|
+
head :unauthorized and return if action_name.in?(Tramway::Api.available_models[model_class.to_s][:closed]&.map(&:to_s) || []) && !current_user
|
73
73
|
end
|
74
74
|
|
75
75
|
def model_class
|
@@ -1,4 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
+
|
2
3
|
class Tramway::Api::V1::UserTokensController < ::Tramway::Api::V1::ApplicationController
|
3
4
|
def create
|
4
5
|
if entity.present? && entity.authenticate(auth_params[:password])
|
@@ -12,7 +13,7 @@ class Tramway::Api::V1::UserTokensController < ::Tramway::Api::V1::ApplicationCo
|
|
12
13
|
}
|
13
14
|
}, status: :created
|
14
15
|
else
|
15
|
-
|
16
|
+
unauthorized
|
16
17
|
end
|
17
18
|
end
|
18
19
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'tramway/helpers/class_name_helpers'
|
2
4
|
|
3
5
|
class Tramway::Api::V1::UsersController < ::Tramway::Api::V1::ApplicationController
|
@@ -7,13 +9,14 @@ class Tramway::Api::V1::UsersController < ::Tramway::Api::V1::ApplicationControl
|
|
7
9
|
def create
|
8
10
|
user_form = sign_up_form_class_name(user_based_model).new user_based_model.new
|
9
11
|
# Implement JSON API spec here
|
10
|
-
if user_form.submit snake_case params[
|
12
|
+
if user_form.submit snake_case params[:data][:attributes]
|
11
13
|
token = ::Knock::AuthToken.new(payload: { sub: user_form.model.uid }).token
|
12
|
-
# FIXME refactor this bullshit
|
14
|
+
# FIXME: refactor this bullshit
|
13
15
|
serialized_user = OpenStruct.new(
|
14
16
|
user_form.model.attributes.merge(
|
15
17
|
authentication_token: token,
|
16
|
-
|
18
|
+
uid: user_form.model.uid,
|
19
|
+
id: user_form.model.id
|
17
20
|
)
|
18
21
|
)
|
19
22
|
render json: serialized_user, status: :created
|
@@ -26,21 +26,19 @@ class Tramway::Api::V1::ErrorSerializer < ActiveModel::Serializer
|
|
26
26
|
end
|
27
27
|
end
|
28
28
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
error_messages
|
38
|
-
|
39
|
-
|
40
|
-
path + [attribute_key] + [array_attribute_key]
|
41
|
-
)
|
29
|
+
object.model&.attributes&.each do |attribute_key, attribute_value|
|
30
|
+
if attribute_value.is_a?(Reform::Form)
|
31
|
+
error_messages.merge!(error_messages(attribute_value, path + [attribute_key]))
|
32
|
+
elsif attribute_value.is_a?(Array)
|
33
|
+
attribute_value.each_with_index do |array_attribute_value, array_attribute_key|
|
34
|
+
next unless array_attribute_value.is_a?(Reform::Form)
|
35
|
+
|
36
|
+
error_messages.merge!(
|
37
|
+
error_messages(
|
38
|
+
array_attribute_value,
|
39
|
+
path + [attribute_key] + [array_attribute_key]
|
42
40
|
)
|
43
|
-
|
41
|
+
)
|
44
42
|
end
|
45
43
|
end
|
46
44
|
end
|
@@ -1,4 +1,6 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
ActionDispatch::Request.parameter_parsers[:json] = lambda { |raw_post|
|
2
4
|
data = ActiveSupport::JSON.decode(raw_post)
|
3
5
|
data = { _json: data } unless data.is_a?(Hash)
|
4
6
|
|
data/config/routes.rb
CHANGED
@@ -1,7 +1,9 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
Tramway::Api::Engine.routes.draw do
|
2
4
|
namespace :v1 do
|
3
|
-
resource :user_token, only: [
|
4
|
-
resource :user, only: [
|
5
|
+
resource :user_token, only: [:create]
|
6
|
+
resource :user, only: %i[create show]
|
5
7
|
resources :records
|
6
8
|
end
|
7
9
|
end
|
data/lib/tramway/api.rb
CHANGED
data/lib/tramway/api/engine.rb
CHANGED
data/lib/tramway/api/version.rb
CHANGED
metadata
CHANGED
@@ -1,57 +1,57 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tramway-api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.6.
|
4
|
+
version: 1.6.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Pavel Kalashnikov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-12-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
14
|
+
name: active_model_serializers
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - '='
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
19
|
+
version: 0.10.5
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- -
|
24
|
+
- - '='
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
26
|
+
version: 0.10.5
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
|
-
name:
|
28
|
+
name: knock
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version:
|
33
|
+
version: '0'
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version:
|
40
|
+
version: '0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
|
-
name:
|
42
|
+
name: tramway-core
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- -
|
45
|
+
- - ">="
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version:
|
47
|
+
version: 1.9.2
|
48
48
|
type: :runtime
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- -
|
52
|
+
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version:
|
54
|
+
version: 1.9.2
|
55
55
|
description: Engine for api
|
56
56
|
email:
|
57
57
|
- kalashnikovisme@gmail.com
|