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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 04f5055e00ef3608b436ae004240b26578fb7c9862eee1935e6fbfb6c299e1c6
4
- data.tar.gz: 8269beafa9c7c8ca209d6eb4f35ce5ba7f7539106acd8e2ec1d984c386cff225
3
+ metadata.gz: be852b8025894c89dbc01109337011ac3178b1fcc14cb8d7655bebfcbf8083ec
4
+ data.tar.gz: 8a13ec95aee745c0b262664658cf14807eb823ada87d845ae34e21a4b3c4720c
5
5
  SHA512:
6
- metadata.gz: 86986d7a77393d34502750ffeb93240c71acdba6666df665f33916a815ce25b25038296a0bd5b603acfdda793354c5497f5a3b3375feeb3b4d11bcdba4aa6388
7
- data.tar.gz: a6aed10bc3451c21abf1528893e06f778beb2b669ac89a17b0ca04dc500bf098628e35d8c34f674e5a34ef00dcba3bd51b5dcaeeadc8a7686cfc476411ff0c8b
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) { attributes_for :user }
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
- params[:model].constantize
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.id }).token
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(
@@ -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
+ }
@@ -1,5 +1,5 @@
1
1
  module Tramway
2
2
  module Api
3
- VERSION = '1.4.1.1'
3
+ VERSION = '1.5'
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.1.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-10 00:00:00.000000000 Z
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