tramway-api 1.4.1.1 → 1.5
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/README.md +64 -1
- data/app/controllers/tramway/api/v1/application_controller.rb +8 -0
- data/app/controllers/tramway/api/v1/records_controller.rb +13 -3
- 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/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: be852b8025894c89dbc01109337011ac3178b1fcc14cb8d7655bebfcbf8083ec
|
4
|
+
data.tar.gz: 8a13ec95aee745c0b262664658cf14807eb823ada87d845ae34e21a4b3c4720c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 47512dbe567f7b258f2f1b411545503bf0c7a964b822030061e328dee94865c9d93a4c549183ef00386af44177b818fcd2339b3658f78bd81225246256188489
|
7
|
+
data.tar.gz: d45547b8ca370a7357a6123330f51a90451d48663408f682fabe81ff1081955b219cef6608c0eda484284c0f6863371f456c419905513b5632b394b40ea09390
|
data/README.md
CHANGED
@@ -185,7 +185,9 @@ require 'rails_helper'
|
|
185
185
|
|
186
186
|
RSpec.describe 'Post creating user', type: :feature do
|
187
187
|
describe 'POST /api/v1/user with model User' do
|
188
|
-
let(:attributes)
|
188
|
+
let(:attributes) do
|
189
|
+
kebab_case_converter attributes_for :user
|
190
|
+
end
|
189
191
|
|
190
192
|
it 'returns created status' do
|
191
193
|
post '/api/v1/user', params: { user: attributes }
|
@@ -284,6 +286,67 @@ Enabled methods:
|
|
284
286
|
* index
|
285
287
|
* destroy
|
286
288
|
|
289
|
+
### Index
|
290
|
+
|
291
|
+
Every model you've added in initializer will be able by URL `api/v1/records?model=#{model_class}`.
|
292
|
+
|
293
|
+
Just update your initializer:
|
294
|
+
|
295
|
+
```ruby
|
296
|
+
::Tramway::Api.set_available_models user: { open: %i[create], closed: %i[update index] } # we've added index method
|
297
|
+
```
|
298
|
+
|
299
|
+
Create serializer
|
300
|
+
|
301
|
+
*app/serializers/user_serializer.rb*
|
302
|
+
|
303
|
+
```ruby
|
304
|
+
class UserSerializer < Tramway::Core::ApplicationSerializer
|
305
|
+
attributes :username, :email
|
306
|
+
end
|
307
|
+
```
|
308
|
+
|
309
|
+
Then write test:
|
310
|
+
|
311
|
+
```ruby
|
312
|
+
it 'returns status' do
|
313
|
+
get '/api/v1/records', params: { model: 'User' }, headers: headers
|
314
|
+
|
315
|
+
expect(response.status).to eq 200
|
316
|
+
end
|
317
|
+
|
318
|
+
it 'returns needed count' do
|
319
|
+
get '/api/v1/records', params: { model: 'User' }, headers: headers
|
320
|
+
|
321
|
+
expect(json_response[:data].size).to eq User.active.count
|
322
|
+
end
|
323
|
+
```
|
324
|
+
|
325
|
+
You have your records in JSON API spec.
|
326
|
+
|
327
|
+
### Create
|
328
|
+
|
329
|
+
Production ready
|
330
|
+
|
331
|
+
Docs coming soon
|
332
|
+
|
333
|
+
### Update
|
334
|
+
|
335
|
+
Production ready
|
336
|
+
|
337
|
+
Docs coming soon
|
338
|
+
|
339
|
+
### Show
|
340
|
+
|
341
|
+
Production ready
|
342
|
+
|
343
|
+
Docs coming soon
|
344
|
+
|
345
|
+
### Destroy
|
346
|
+
|
347
|
+
Production ready
|
348
|
+
|
349
|
+
Docs coming soon
|
287
350
|
|
288
351
|
## Contributing
|
289
352
|
Contribution directions go here.
|
@@ -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
|
@@ -6,16 +6,19 @@ module Tramway::Api::V1
|
|
6
6
|
|
7
7
|
def index
|
8
8
|
records = model_class.active.order(id: :desc).send params[:scope] || :all
|
9
|
+
records = records.full_text_search params[:search] if params[:search]
|
9
10
|
render json: records,
|
10
11
|
each_serializer: serializer_class,
|
12
|
+
include: '*',
|
11
13
|
status: :ok
|
12
14
|
end
|
13
15
|
|
14
16
|
def create
|
15
17
|
record_form = form_class.new model_class.new
|
16
|
-
if record_form.submit params[:data][:attributes]
|
18
|
+
if record_form.submit snake_case params[:data][:attributes]
|
17
19
|
render json: record_form.model,
|
18
20
|
serializer: serializer_class,
|
21
|
+
include: '*',
|
19
22
|
status: :created
|
20
23
|
else
|
21
24
|
render_errors_for record_form
|
@@ -24,9 +27,10 @@ module Tramway::Api::V1
|
|
24
27
|
|
25
28
|
def update
|
26
29
|
record_form = form_class.new model_class.active.find params[:id]
|
27
|
-
if record_form.submit params[:data][:attributes]
|
30
|
+
if record_form.submit snake_case params[:data][:attributes]
|
28
31
|
render json: record_form.model,
|
29
32
|
serializer: serializer_class,
|
33
|
+
include: '*',
|
30
34
|
status: :ok
|
31
35
|
else
|
32
36
|
render_errors_for record_form
|
@@ -37,6 +41,7 @@ module Tramway::Api::V1
|
|
37
41
|
record = model_class.active.find params[:id]
|
38
42
|
render json: record,
|
39
43
|
serializer: serializer_class,
|
44
|
+
include: '*',
|
40
45
|
status: :ok
|
41
46
|
end
|
42
47
|
|
@@ -45,6 +50,7 @@ module Tramway::Api::V1
|
|
45
50
|
record.remove
|
46
51
|
render json: record,
|
47
52
|
serializer: serializer_class,
|
53
|
+
include: '*',
|
48
54
|
status: :no_content
|
49
55
|
end
|
50
56
|
|
@@ -68,7 +74,11 @@ module Tramway::Api::V1
|
|
68
74
|
|
69
75
|
def model_class
|
70
76
|
if params[:model].to_s.in? ::Tramway::Api.available_models.keys.map(&:to_s)
|
71
|
-
|
77
|
+
begin
|
78
|
+
params[:model].constantize
|
79
|
+
rescue ActiveSupport::Concern::MultipleIncludedBlocks => e
|
80
|
+
raise "#{e}. Maybe #{params[:model]} model doesn't exists or there is naming conflicts with it"
|
81
|
+
end
|
72
82
|
end
|
73
83
|
end
|
74
84
|
|
@@ -8,7 +8,7 @@ class Tramway::Api::V1::UsersController < ::Tramway::Api::V1::ApplicationControl
|
|
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
10
|
if user_form.submit params[Tramway::Api.user_based_model.name.underscore]
|
11
|
-
token = ::Knock::AuthToken.new(payload: { sub: user_form.model.
|
11
|
+
token = ::Knock::AuthToken.new(payload: { sub: user_form.model.uid }).token
|
12
12
|
# FIXME refactor this bullshit
|
13
13
|
serialized_user = OpenStruct.new(
|
14
14
|
user_form.model.attributes.merge(
|
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.5'
|
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-09-
|
11
|
+
date: 2019-09-22 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
|