tramway-api 1.4.4 → 1.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +69 -1
- data/app/controllers/tramway/api/application_controller.rb +7 -5
- data/app/controllers/tramway/api/v1/application_controller.rb +8 -0
- data/app/controllers/tramway/api/v1/records_controller.rb +5 -3
- data/app/controllers/tramway/api/v1/user_tokens_controller.rb +2 -1
- data/app/controllers/tramway/api/v1/users_controller.rb +1 -1
- data/config/initializers/json_param_key_transform.rb +6 -0
- data/lib/tramway/api.rb +14 -6
- data/lib/tramway/api/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 01e0eb09865cd422bd78339ed17061bcb3e9783109877faf8c7d284386152696
|
4
|
+
data.tar.gz: 805e37d122674f9a95e71b8dcc98b819b34dfcf3e5ab8f1b6fc34883f65e6c1a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ee33d4fd776288f9ebe9159e8ac7387aa001a8687e32a6e91dff1b32d0786e7cc0ec58a19d597e0600c117785c234801fdad0adf9616cb00dc05f381af0314ce
|
7
|
+
data.tar.gz: f8b7b35880e8f509cdd73d9d9e9a41f3c9f3bd7d669c399a8b97b649494d444323188493e9abcc768b490242792bf2d88c6cd63268cef3ade7f4aca6e2ca8b7f
|
data/README.md
CHANGED
@@ -43,6 +43,7 @@ coming soon...
|
|
43
43
|
gem 'tramway-core'
|
44
44
|
gem 'state_machine', github: 'seuros/state_machine'
|
45
45
|
gem 'knock'
|
46
|
+
gem 'audited'
|
46
47
|
```
|
47
48
|
|
48
49
|
## Usage
|
@@ -82,6 +83,10 @@ t.uuid :uid, default: 'uuid_generate_v4()'
|
|
82
83
|
```ruby
|
83
84
|
class User < Tramway::Core::ApplicationRecord
|
84
85
|
has_secure_password
|
86
|
+
|
87
|
+
def self.from_token_payload(payload)
|
88
|
+
find_by uid: payload['sub']
|
89
|
+
end
|
85
90
|
end
|
86
91
|
```
|
87
92
|
|
@@ -185,7 +190,9 @@ require 'rails_helper'
|
|
185
190
|
|
186
191
|
RSpec.describe 'Post creating user', type: :feature do
|
187
192
|
describe 'POST /api/v1/user with model User' do
|
188
|
-
let(:attributes)
|
193
|
+
let(:attributes) do
|
194
|
+
kebab_case_converter attributes_for :user
|
195
|
+
end
|
189
196
|
|
190
197
|
it 'returns created status' do
|
191
198
|
post '/api/v1/user', params: { user: attributes }
|
@@ -284,6 +291,67 @@ Enabled methods:
|
|
284
291
|
* index
|
285
292
|
* destroy
|
286
293
|
|
294
|
+
### Index
|
295
|
+
|
296
|
+
Every model you've added in initializer will be able by URL `api/v1/records?model=#{model_class}`.
|
297
|
+
|
298
|
+
Just update your initializer:
|
299
|
+
|
300
|
+
```ruby
|
301
|
+
::Tramway::Api.set_available_models user: { open: %i[create], closed: %i[update index] } # we've added index method
|
302
|
+
```
|
303
|
+
|
304
|
+
Create serializer
|
305
|
+
|
306
|
+
*app/serializers/user_serializer.rb*
|
307
|
+
|
308
|
+
```ruby
|
309
|
+
class UserSerializer < Tramway::Core::ApplicationSerializer
|
310
|
+
attributes :username, :email
|
311
|
+
end
|
312
|
+
```
|
313
|
+
|
314
|
+
Then write test:
|
315
|
+
|
316
|
+
```ruby
|
317
|
+
it 'returns status' do
|
318
|
+
get '/api/v1/records', params: { model: 'User' }, headers: headers
|
319
|
+
|
320
|
+
expect(response.status).to eq 200
|
321
|
+
end
|
322
|
+
|
323
|
+
it 'returns needed count' do
|
324
|
+
get '/api/v1/records', params: { model: 'User' }, headers: headers
|
325
|
+
|
326
|
+
expect(json_response[:data].size).to eq User.active.count
|
327
|
+
end
|
328
|
+
```
|
329
|
+
|
330
|
+
You have your records in JSON API spec.
|
331
|
+
|
332
|
+
### Create
|
333
|
+
|
334
|
+
Production ready
|
335
|
+
|
336
|
+
Docs coming soon
|
337
|
+
|
338
|
+
### Update
|
339
|
+
|
340
|
+
Production ready
|
341
|
+
|
342
|
+
Docs coming soon
|
343
|
+
|
344
|
+
### Show
|
345
|
+
|
346
|
+
Production ready
|
347
|
+
|
348
|
+
Docs coming soon
|
349
|
+
|
350
|
+
### Destroy
|
351
|
+
|
352
|
+
Production ready
|
353
|
+
|
354
|
+
Docs coming soon
|
287
355
|
|
288
356
|
## Contributing
|
289
357
|
Contribution directions go here.
|
@@ -18,7 +18,7 @@ module Tramway
|
|
18
18
|
protected
|
19
19
|
|
20
20
|
def authenticate
|
21
|
-
return unauthorized
|
21
|
+
return unauthorized if current_user.nil? || !params[:user_based_model].in?(Tramway::Api.user_based_models)
|
22
22
|
end
|
23
23
|
|
24
24
|
def auth_token
|
@@ -30,17 +30,19 @@ module Tramway
|
|
30
30
|
end
|
31
31
|
|
32
32
|
def entity
|
33
|
+
user_based_model = params[:user_based_model].constantize
|
33
34
|
@entity ||=
|
34
|
-
if
|
35
|
-
|
35
|
+
if user_based_model.respond_to? :from_token_request
|
36
|
+
user_based_model.active.from_token_request request
|
36
37
|
else
|
37
38
|
params[:auth] && find_user_by_auth_attributes
|
38
39
|
end
|
39
40
|
end
|
40
41
|
|
41
42
|
def find_user_by_auth_attributes
|
42
|
-
|
43
|
-
|
43
|
+
user_based_model = params[:user_based_model].constantize
|
44
|
+
Tramway::Api.auth_attributes[user_based_model].each do |attribute|
|
45
|
+
object = user_based_model.active.where.not(attribute => nil).find_by(attribute => auth_params[:login])
|
44
46
|
return object if object
|
45
47
|
end
|
46
48
|
nil
|
@@ -9,6 +9,14 @@ module Tramway
|
|
9
9
|
def render_error_with_text(text)
|
10
10
|
render json: { text: text }, status: :bad_request
|
11
11
|
end
|
12
|
+
|
13
|
+
def snake_case(params)
|
14
|
+
hash = {}
|
15
|
+
params.each do |attribute, value|
|
16
|
+
hash.merge! attribute.to_s.gsub('-', '_') => value
|
17
|
+
end
|
18
|
+
hash
|
19
|
+
end
|
12
20
|
end
|
13
21
|
end
|
14
22
|
end
|
@@ -15,7 +15,7 @@ module Tramway::Api::V1
|
|
15
15
|
|
16
16
|
def create
|
17
17
|
record_form = form_class.new model_class.new
|
18
|
-
if record_form.submit params[:data][:attributes]
|
18
|
+
if record_form.submit snake_case params[:data][:attributes]
|
19
19
|
render json: record_form.model,
|
20
20
|
serializer: serializer_class,
|
21
21
|
include: '*',
|
@@ -27,7 +27,7 @@ module Tramway::Api::V1
|
|
27
27
|
|
28
28
|
def update
|
29
29
|
record_form = form_class.new model_class.active.find params[:id]
|
30
|
-
if record_form.submit params[:data][:attributes]
|
30
|
+
if record_form.submit snake_case params[:data][:attributes]
|
31
31
|
render json: record_form.model,
|
32
32
|
serializer: serializer_class,
|
33
33
|
include: '*',
|
@@ -68,7 +68,9 @@ module Tramway::Api::V1
|
|
68
68
|
|
69
69
|
def authenticate_user_if_needed
|
70
70
|
if action_name.in? Tramway::Api::available_models[model_class.to_s][:closed]&.map(&:to_s) || []
|
71
|
-
|
71
|
+
Tramway::Api.user_based_models.map do |user_based_model|
|
72
|
+
send("current_#{user_based_model.name.underscore}").present?
|
73
|
+
end.include? true
|
72
74
|
end
|
73
75
|
end
|
74
76
|
|
@@ -7,7 +7,7 @@ class Tramway::Api::V1::UsersController < ::Tramway::Api::V1::ApplicationControl
|
|
7
7
|
def create
|
8
8
|
user_form = sign_up_form_class_name(Tramway::Api.user_based_model).new Tramway::Api.user_based_model.new
|
9
9
|
# Implement JSON API spec here
|
10
|
-
if user_form.submit params[Tramway::Api.user_based_model.name.underscore]
|
10
|
+
if user_form.submit snake_case params[Tramway::Api.user_based_model.name.underscore]
|
11
11
|
token = ::Knock::AuthToken.new(payload: { sub: user_form.model.uid }).token
|
12
12
|
# FIXME refactor this bullshit
|
13
13
|
serialized_user = OpenStruct.new(
|
data/lib/tramway/api.rb
CHANGED
@@ -4,19 +4,27 @@ module Tramway
|
|
4
4
|
module Api
|
5
5
|
class << self
|
6
6
|
def auth_config
|
7
|
-
@@auth_config ||= { user_model: ::Tramway::User::User, auth_attributes: :email }
|
7
|
+
@@auth_config ||= [{ user_model: ::Tramway::User::User, auth_attributes: :email }]
|
8
8
|
end
|
9
9
|
|
10
|
-
def auth_config=(
|
11
|
-
|
10
|
+
def auth_config=(params)
|
11
|
+
if params.is_a? Hash
|
12
|
+
@@auth_config = [params]
|
13
|
+
elsif params.is_a? Array
|
14
|
+
@@auth_config = params
|
15
|
+
end
|
12
16
|
end
|
13
17
|
|
14
|
-
def
|
15
|
-
@@auth_config
|
18
|
+
def user_based_models
|
19
|
+
@@auth_config.map do |conf|
|
20
|
+
conf[:user_model]
|
21
|
+
end
|
16
22
|
end
|
17
23
|
|
18
24
|
def auth_attributes
|
19
|
-
@@auth_config
|
25
|
+
@@auth_config.reduce({}) do |hash, conf|
|
26
|
+
hash.merge! conf[:user_model] => conf[:auth_attributes]
|
27
|
+
end
|
20
28
|
end
|
21
29
|
|
22
30
|
def set_available_models(**models)
|
data/lib/tramway/api/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tramway-api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: '1.6'
|
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-10-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: knock
|
@@ -76,6 +76,7 @@ files:
|
|
76
76
|
- app/serializers/tramway/api/v1/error_serializer.rb
|
77
77
|
- app/views/layouts/tramway/api/application.html.erb
|
78
78
|
- config/initializers/active_model_serializers.rb
|
79
|
+
- config/initializers/json_param_key_transform.rb
|
79
80
|
- config/routes.rb
|
80
81
|
- lib/tasks/tramway/api_tasks.rake
|
81
82
|
- lib/tramway/api.rb
|