tramway-api 1.4.4 → 1.5

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: be852b8025894c89dbc01109337011ac3178b1fcc14cb8d7655bebfcbf8083ec
4
+ data.tar.gz: 8a13ec95aee745c0b262664658cf14807eb823ada87d845ae34e21a4b3c4720c
5
5
  SHA512:
6
- metadata.gz: f84dd206d408c41f1d3c230079418445ff4b7fffbb4808637297695c3f033dec031e3e7f3f29f699a58d865e950d1afac1d1f8a3f6116c1ea40469fabc06bf0d
7
- data.tar.gz: c746cad5ecbce016799e6337ea9f23a38287bd2cbee543a9460a5750e301238e10459bd0f4da0cb0e9bb3f3fd885665daa998b4e59982f2cb014034a48297ed1
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
@@ -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: '*',
@@ -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.4'
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.4
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-16 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