tramway-api 1.4.4 → 1.6.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: eed51b344d38bb111854f4e32d985856ea3504b76fbf901fee56735183eae415
4
- data.tar.gz: d430ca30613c117aed36db0493885b4399d4015d53d12f34470fd8bf60fe7d71
3
+ metadata.gz: 0f21707e10ef82a1c378250e9c82339400ee41739d7a6009001ecd1c8a17c4e0
4
+ data.tar.gz: 22732d5ad75036e1bf59831171f4d82af30f95805432900df283566b2b24619c
5
5
  SHA512:
6
- metadata.gz: f84dd206d408c41f1d3c230079418445ff4b7fffbb4808637297695c3f033dec031e3e7f3f29f699a58d865e950d1afac1d1f8a3f6116c1ea40469fabc06bf0d
7
- data.tar.gz: c746cad5ecbce016799e6337ea9f23a38287bd2cbee543a9460a5750e301238e10459bd0f4da0cb0e9bb3f3fd885665daa998b4e59982f2cb014034a48297ed1
6
+ metadata.gz: 313b31746731d79afe528f6100daa910260707e03c4d68d5c0961b6a046591d485006a58e3438b9b3bf4c1dcb7b79009511834b7a58905e293834d679c47bb66
7
+ data.tar.gz: 6b9a2570cd6b8e68d039de8706d9923651081c010f0a4cebce5347a06f442033b2c3833b7c07194bd4eea3b9bb3a2b7b81d629078d18380df4488f35b90ba63f
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) { attributes_for :user }
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 unless current_user
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 Tramway::Api.user_based_model.respond_to? :from_token_request
35
- Tramway::Api.user_based_model.active.from_token_request request
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
- Tramway::Api.auth_attributes.each do |attribute|
43
- object = Tramway::Api.user_based_model.active.where.not(attribute => nil).find_by(attribute => auth_params[:login])
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
@@ -49,6 +51,12 @@ module Tramway
49
51
  def auth_params
50
52
  params[:auth]&.permit(:login, :password)
51
53
  end
54
+
55
+ def current_user
56
+ Tramway::Api.user_based_models.map do |user_based_model|
57
+ send("current_#{user_based_model.name.underscore}")
58
+ end.compact.first
59
+ end
52
60
  end
53
61
  end
54
62
  end
@@ -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,7 @@ 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
- authenticate_user
71
+ current_user
72
72
  end
73
73
  end
74
74
 
@@ -7,7 +7,8 @@ class Tramway::Api::V1::UserTokensController < ::Tramway::Api::V1::ApplicationCo
7
7
  auth_token: token,
8
8
  user: {
9
9
  email: @entity.email,
10
- uid: @entity.uid
10
+ uid: @entity.uid,
11
+ id: @entity.id
11
12
  }
12
13
  }, status: :created
13
14
  else
@@ -5,9 +5,9 @@ class Tramway::Api::V1::UsersController < ::Tramway::Api::V1::ApplicationControl
5
5
  include Tramway::ClassNameHelpers
6
6
 
7
7
  def create
8
- user_form = sign_up_form_class_name(Tramway::Api.user_based_model).new Tramway::Api.user_based_model.new
8
+ user_form = sign_up_form_class_name(user_based_model).new 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[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(
@@ -31,4 +31,8 @@ class Tramway::Api::V1::UsersController < ::Tramway::Api::V1::ApplicationControl
31
31
  def sign_up_form_class_name(model_class)
32
32
  form_class_name "#{model_class}SignUp"
33
33
  end
34
+
35
+ def user_based_model
36
+ params[:user_based_model].constantize if params[:user_based_model].in? Tramway::Api.user_based_models.map(&:to_s)
37
+ end
34
38
  end
@@ -0,0 +1,6 @@
1
+ ActionDispatch::Request.parameter_parsers[:json] = -> (raw_post) {
2
+ data = ActiveSupport::JSON.decode(raw_post)
3
+ data = { _json: data } unless data.is_a?(Hash)
4
+
5
+ data.deep_transform_keys!(&:underscore)
6
+ }
@@ -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=(**params)
11
- @@auth_config = params
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 user_based_model
15
- @@auth_config[:user_model]
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[:auth_attributes]
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)
@@ -1,5 +1,5 @@
1
1
  module Tramway
2
2
  module Api
3
- VERSION = '1.4.4'
3
+ VERSION = '1.6.0.1'
4
4
  end
5
5
  end
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.4
4
+ version: 1.6.0.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: 2019-09-16 00:00:00.000000000 Z
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