tramway-api 1.8.2 → 1.8.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +162 -14
- data/app/controllers/tramway/api/application_controller.rb +3 -1
- data/app/controllers/tramway/api/v1/application_controller.rb +9 -8
- data/app/serializers/tramway/api/v1/application_serializer.rb +2 -1
- data/app/serializers/tramway/api/v1/error_serializer.rb +1 -5
- data/lib/tramway/api.rb +11 -0
- data/lib/tramway/api/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6ea920d1d95b5d0879e4761b50d32a53ae9cd09425fd7ce6f3c35cc6173821cf
|
4
|
+
data.tar.gz: 248fc9efe23a6b44db5ce5065fc4e15500c3c387f5c39380b83e677cb750434c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 949028808784d424710d0a72e602eefe85144ef46ffd1cba3a1671ad787fb91c527ae2b52002a5c60ada7c4225c9b853c3ee0f944ccda9ac7d224d086d029402
|
7
|
+
data.tar.gz: 5ca4783d217ee96ab1aef6be3368dd1a8c0a71233a5b129b30147b34d77f74b868933986bddcb0a12229fc4f9d6454332c7a7a1351f4c883b2be8269223c0023
|
data/README.md
CHANGED
@@ -44,6 +44,7 @@ coming soon...
|
|
44
44
|
gem 'state_machine', github: 'seuros/state_machine'
|
45
45
|
gem 'knock'
|
46
46
|
gem 'audited'
|
47
|
+
gem 'ransack'
|
47
48
|
```
|
48
49
|
|
49
50
|
## Usage
|
@@ -64,23 +65,51 @@ gem 'knock'
|
|
64
65
|
|
65
66
|
Run `bundle install`
|
66
67
|
|
68
|
+
### Initialize @application object
|
69
|
+
|
70
|
+
[How-to](https://github.com/Purple-Magic/tramway-core/blob/develop/README.md#every-tramway-application-need-initialized-application-object-or-if-you-create-tramway-plugin-it-should-be-application_engine-object)
|
71
|
+
|
72
|
+
*config/routes.rb*
|
73
|
+
|
74
|
+
```ruby
|
75
|
+
Rails.application.routes.draw do
|
76
|
+
# ...
|
77
|
+
mount Tramway::Api::Engine, at: '/api'
|
78
|
+
# ...
|
79
|
+
end
|
80
|
+
```
|
81
|
+
|
67
82
|
Then generate User (you use another name, it's just an example) model
|
68
83
|
|
69
84
|
```
|
70
|
-
rails g model user email:text password_digest:text username:text state:text
|
85
|
+
rails g model user email:text password_digest:text username:text state:text uuid:uuid
|
71
86
|
```
|
72
87
|
|
73
88
|
Enable extension in your database:
|
74
89
|
|
75
90
|
*db/migrate/enable_extension.rb*
|
76
|
-
```
|
77
|
-
|
78
|
-
|
91
|
+
```ruby
|
92
|
+
class EnableExtensionUUIDOSSP < ActiveRecord::Migration
|
93
|
+
def change
|
94
|
+
enable_extension 'uuid-ossp'
|
95
|
+
end
|
79
96
|
end
|
80
97
|
```
|
81
98
|
|
99
|
+
### You can choose a method, which will be using as public ID method
|
100
|
+
|
101
|
+
By default, it's a `uuid` method
|
102
|
+
|
103
|
+
To choose your own public ID method, just add this line to:
|
82
104
|
|
83
|
-
|
105
|
+
*config/initializers/tramway.rb*
|
106
|
+
|
107
|
+
```ruby
|
108
|
+
Tramway::Api.id_methods_of(User => :id)
|
109
|
+
```
|
110
|
+
If you want to use `uuid` by default, please, add it to your models
|
111
|
+
|
112
|
+
#### Add generating uuid by default to every model, that is accessible by API
|
84
113
|
|
85
114
|
*db/migrate/add_uuid_to_some_model.rb*
|
86
115
|
|
@@ -100,11 +129,26 @@ class User < Tramway::Core::ApplicationRecord
|
|
100
129
|
end
|
101
130
|
```
|
102
131
|
|
103
|
-
Create file `config/initializers/tramway.rb`
|
132
|
+
#### Create file `config/initializers/tramway.rb`
|
133
|
+
#### If you need JWT authentication add this line to the `config/initializers/tramway.rb`
|
104
134
|
|
105
135
|
```ruby
|
106
136
|
::Tramway::Api.auth_config = { user_model: User, auth_attributes: %i[email username] }
|
107
|
-
|
137
|
+
```
|
138
|
+
|
139
|
+
#### Configurate available models. Tramway will create end points according to this config
|
140
|
+
|
141
|
+
```
|
142
|
+
::Tramway::Api.set_available_models({
|
143
|
+
User => [
|
144
|
+
{
|
145
|
+
show: lambda do |record, current_user|
|
146
|
+
record.id == current_user.id # shows only current_user profile
|
147
|
+
end
|
148
|
+
}
|
149
|
+
],
|
150
|
+
project: :your_project_name
|
151
|
+
})
|
108
152
|
```
|
109
153
|
|
110
154
|
Run `rails g tramway:core:install`
|
@@ -316,7 +360,7 @@ Create serializer
|
|
316
360
|
*app/serializers/user_serializer.rb*
|
317
361
|
|
318
362
|
```ruby
|
319
|
-
class UserSerializer < Tramway::
|
363
|
+
class UserSerializer < Tramway::Api::V1::ApplicationSerializer
|
320
364
|
attributes :username, :email
|
321
365
|
end
|
322
366
|
```
|
@@ -341,21 +385,125 @@ You have your records in JSON API spec.
|
|
341
385
|
|
342
386
|
### Create
|
343
387
|
|
344
|
-
|
388
|
+
*config/initializers/tramway.rb*
|
345
389
|
|
346
|
-
|
390
|
+
```ruby
|
391
|
+
::Tramway::Api.set_available_models({ YourModel => [ :create ] }, project: :your_project_name })
|
392
|
+
```
|
393
|
+
|
394
|
+
*app/forms/your_model_form.rb*
|
395
|
+
|
396
|
+
```ruby
|
397
|
+
class YourModelForm < Tramway::Core::ApplicationForm
|
398
|
+
properties :attribute1, :attribute2, :name
|
399
|
+
|
400
|
+
association :another_association_model
|
401
|
+
|
402
|
+
def name=(value)
|
403
|
+
model.first_name = value.split(' ')[0]
|
404
|
+
model.first_name = value.split(' ')[1]
|
405
|
+
end
|
406
|
+
end
|
407
|
+
```
|
408
|
+
|
409
|
+
Now you can your request. It has such structure
|
410
|
+
|
411
|
+
**POST** `/api/v1/records?model=YourModel`
|
412
|
+
|
413
|
+
```
|
414
|
+
Params Structure
|
415
|
+
{
|
416
|
+
data: {
|
417
|
+
attributes: {
|
418
|
+
attribute1: 'some value',
|
419
|
+
attribute2: 'some value',
|
420
|
+
name: 'some full name',
|
421
|
+
another_association_model: {
|
422
|
+
# here a list of attributes, which you described in AnotherAssociationModelForm
|
423
|
+
}
|
424
|
+
}
|
425
|
+
}
|
426
|
+
}
|
427
|
+
```
|
347
428
|
|
348
429
|
### Update
|
349
430
|
|
350
|
-
|
431
|
+
*config/initializers/tramway.rb*
|
351
432
|
|
352
|
-
|
433
|
+
```ruby
|
434
|
+
::Tramway::Api.set_available_models({ YourModel => [ :update ] }, project: :your_project_name })
|
435
|
+
```
|
436
|
+
|
437
|
+
*app/forms/your_model_form.rb*
|
438
|
+
|
439
|
+
```ruby
|
440
|
+
class YourModelForm < Tramway::Core::ApplicationForm
|
441
|
+
properties :attribute1, :attribute2, :name
|
442
|
+
|
443
|
+
association :another_association_model
|
444
|
+
|
445
|
+
def name=(value)
|
446
|
+
model.first_name = value.split(' ')[0]
|
447
|
+
model.first_name = value.split(' ')[1]
|
448
|
+
end
|
449
|
+
end
|
450
|
+
```
|
451
|
+
|
452
|
+
Now you can your request. It has such structure
|
453
|
+
|
454
|
+
**PATCH** `/api/v1/records/#{object_id}?model=YourModel`
|
455
|
+
|
456
|
+
```
|
457
|
+
Params Structure
|
458
|
+
{
|
459
|
+
data: {
|
460
|
+
attributes: {
|
461
|
+
attribute1: 'some value',
|
462
|
+
attribute2: 'some value',
|
463
|
+
name: 'some full name',
|
464
|
+
another_association_model: {
|
465
|
+
# here a list of attributes, which you described in AnotherAssociationModelForm
|
466
|
+
}
|
467
|
+
}
|
468
|
+
}
|
469
|
+
}
|
470
|
+
```
|
353
471
|
|
354
472
|
### Show
|
355
473
|
|
356
|
-
|
474
|
+
#### Description
|
357
475
|
|
358
|
-
|
476
|
+
It returns just one record, if it is not deleted.
|
477
|
+
|
478
|
+
#### Using
|
479
|
+
|
480
|
+
##### Allow method show in tramway initializer for `YourModel`
|
481
|
+
|
482
|
+
*config/initializers/tramway.rb*
|
483
|
+
|
484
|
+
```ruby
|
485
|
+
::Tramway::Api.set_available_models({ YourModel => [ :show ] }, project: :your_project_name })
|
486
|
+
```
|
487
|
+
|
488
|
+
##### Create serializer
|
489
|
+
|
490
|
+
*app/serializers/user_serializer.rb*
|
491
|
+
|
492
|
+
```ruby
|
493
|
+
class UserSerializer < Tramway::Core::ApplicationSerializer
|
494
|
+
attributes :username, :email
|
495
|
+
end
|
496
|
+
```
|
497
|
+
|
498
|
+
##### Run your server on the localhost `rails s`
|
499
|
+
##### Made this query to test new API method (for example: you can create file `bin/test_tramway.rb` with this lines):
|
500
|
+
|
501
|
+
```ruby
|
502
|
+
require 'net/http'
|
503
|
+
|
504
|
+
YourModel.create! attribute_1: 'some value', attribute_2: 'some_value'
|
505
|
+
Net::HTTP.get('localhost:3000', "/api/v1/records/#{YourModel.last.id}?model=YourModel")
|
506
|
+
```
|
359
507
|
|
360
508
|
### Destroy
|
361
509
|
|
@@ -58,7 +58,9 @@ module Tramway
|
|
58
58
|
|
59
59
|
def current_user
|
60
60
|
Tramway::Api.user_based_models.map do |user_based_model|
|
61
|
-
|
61
|
+
unless user_based_model == User
|
62
|
+
send("current_#{user_based_model.name.underscore}")
|
63
|
+
end
|
62
64
|
end.compact.first
|
63
65
|
end
|
64
66
|
end
|
@@ -15,7 +15,8 @@ module Tramway
|
|
15
15
|
def snake_case(params)
|
16
16
|
hash = {}
|
17
17
|
params.each do |attribute, value|
|
18
|
-
|
18
|
+
key = ::UUID.validate(attribute) ? attribute : attribute.to_s.gsub('-', '_')
|
19
|
+
hash.merge! key => value
|
19
20
|
end
|
20
21
|
hash
|
21
22
|
end
|
@@ -23,7 +24,8 @@ module Tramway
|
|
23
24
|
private
|
24
25
|
|
25
26
|
def record
|
26
|
-
|
27
|
+
id_method = Tramway::Api.id_method_of(model: model_class) || :uuid
|
28
|
+
@record = model_class.find_by! id_method => params[:id] if params[:id].present?
|
27
29
|
end
|
28
30
|
|
29
31
|
def records
|
@@ -95,15 +97,14 @@ module Tramway
|
|
95
97
|
def checking_roles
|
96
98
|
[:open, current_user&.role].compact
|
97
99
|
end
|
100
|
+
|
98
101
|
protected
|
99
102
|
|
100
103
|
def model_class
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
raise "#{e}. Maybe #{params[:model]} model doesn't exists or there is naming conflicts with it"
|
106
|
-
end
|
104
|
+
begin
|
105
|
+
params[:model].constantize
|
106
|
+
rescue ActiveSupport::Concern::MultipleIncludedBlocks => e
|
107
|
+
raise "#{e}. Maybe #{params[:model]} model doesn't exists or there is naming conflicts with it"
|
107
108
|
end
|
108
109
|
end
|
109
110
|
|
@@ -27,12 +27,8 @@ class Tramway::Api::V1::ErrorSerializer < ActiveModel::Serializer
|
|
27
27
|
end
|
28
28
|
|
29
29
|
object.model&.attributes&.each do |attribute_key, attribute_value|
|
30
|
-
if attribute_value.is_a?(
|
31
|
-
error_messages.merge!(error_messages(attribute_value, path + [attribute_key]))
|
32
|
-
elsif attribute_value.is_a?(Array)
|
30
|
+
if attribute_value.is_a?(Array)
|
33
31
|
attribute_value.each_with_index do |array_attribute_value, array_attribute_key|
|
34
|
-
next unless array_attribute_value.is_a?(Reform::Form)
|
35
|
-
|
36
32
|
error_messages.merge!(
|
37
33
|
error_messages(
|
38
34
|
array_attribute_value,
|
data/lib/tramway/api.rb
CHANGED
@@ -83,6 +83,17 @@ module Tramway
|
|
83
83
|
new_hash.merge! pair[0].to_s => pair[1]
|
84
84
|
end
|
85
85
|
end
|
86
|
+
|
87
|
+
def id_methods_of(options = {})
|
88
|
+
@@id_methods ||= {}
|
89
|
+
@@id_methods.merge!(options.reduce({}) do |hash, pair|
|
90
|
+
hash.merge! pair[0].to_s => pair[1]
|
91
|
+
end)
|
92
|
+
end
|
93
|
+
|
94
|
+
def id_method_of(model:)
|
95
|
+
@@id_methods[model.to_s]
|
96
|
+
end
|
86
97
|
end
|
87
98
|
end
|
88
99
|
end
|
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.8.
|
4
|
+
version: 1.8.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Pavel Kalashnikov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-07-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: active_model_serializers
|