tramway-api 0.3.0.1 → 0.4

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: 6d7e3796ed0eff2effbfe97f978635e7b51b6c2ddc7db5a1a5873cd2bb1d38f3
4
- data.tar.gz: 14aeb32511d860361f82090d931d8b98dd3e23d217328270687e13de46bce079
3
+ metadata.gz: 8d64659e9b37eb052d82029e1ad7f7e3c9d892ffa299b969ae5b213818e8d961
4
+ data.tar.gz: bc1cf91766d49634ff7d9ca760c87f1217295104dfcf769c25106b3da9e9dcdc
5
5
  SHA512:
6
- metadata.gz: f6ecbf27d8279cc6d24cbca336ec5f1bd23731fee9b302d1321d5733a8a4e1ef072b5b55b208de9d544388569bfffa69fb410df0a184dcb60a1930e502c3dcf6
7
- data.tar.gz: ce24a91223c7af98f70e897e4a64a0d29e7f71e3ed6872d7fda8d2d34300b8f34ff97bbabff3b239bbe26de9fd999916f3e4865d8e92a46bf39bf842c4d179f8
6
+ metadata.gz: 31040cfd0eec04aa89691ddc107d5866e4fcf1c4c58110d381c882ddc0825d90806ef5fe2f87ca9b9660966953d0906f002753596cfbbf157eb54db3da7f9bd5
7
+ data.tar.gz: f31d6398c0b62b5d695c02578098029f55b6b62cf996b56e86e48a1c6d1f12d221a9b400d111b9c12f77541f85661da3ff511d4e29fcc12b85647b680dda0c28
@@ -2,6 +2,11 @@ module Tramway
2
2
  module Api
3
3
  module V1
4
4
  class ApplicationController < ::Tramway::Api::ApplicationController
5
+ private
6
+
7
+ def render_errors_for(model)
8
+ render json: model, status: :unprocessable_entity, serializer: ::Tramway::Api::V1::ErrorSerializer
9
+ end
5
10
  end
6
11
  end
7
12
  end
@@ -11,7 +11,7 @@ class Tramway::Api::V1::UsersController < ::Tramway::Api::V1::ApplicationControl
11
11
  serialized_user = OpenStruct.new user_form.model.attributes.merge authentication_token: token
12
12
  render json: serialized_user, status: :created
13
13
  else
14
- render json: user_form.errors.messages, status: :unprocessable_entity
14
+ render_errors_for user_form
15
15
  end
16
16
  end
17
17
 
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Tramway::Api::V1::ErrorSerializer < ActiveModel::Serializer
4
+ # Based on https://github.com/rails-api/active_model_serializers/blob/master/lib/active_model/serializer/error_serializer.rb
5
+ # @return [Hash<field_name,Array<error_message>>]
6
+ def as_json
7
+ error_messages(object)
8
+ end
9
+
10
+ def success?
11
+ false
12
+ end
13
+
14
+ private
15
+
16
+ def error_messages(object, path = []) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
17
+ error_messages = {}
18
+
19
+ object.errors.messages.each do |name, messages|
20
+ error_messages.merge!((path + [name]).join('/') => messages)
21
+ end
22
+
23
+ if object.model
24
+ object.model.errors.messages.each do |name, messages|
25
+ error_messages.merge!((path + [name]).join('/') => messages)
26
+ end
27
+ end
28
+
29
+ if object.model
30
+ object.model.attributes.each do |attribute_key, attribute_value|
31
+ if attribute_value.is_a?(Reform::Form)
32
+ error_messages.merge!(error_messages(attribute_value, path + [attribute_key]))
33
+ elsif attribute_value.is_a?(Array)
34
+ attribute_value.each_with_index do |array_attribute_value, array_attribute_key|
35
+ next unless array_attribute_value.is_a?(Reform::Form)
36
+
37
+ error_messages.merge!(
38
+ error_messages(
39
+ array_attribute_value,
40
+ path + [attribute_key] + [array_attribute_key]
41
+ )
42
+ )
43
+ end
44
+ end
45
+ end
46
+ end
47
+
48
+ error_messages
49
+ end
50
+ end
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ ActiveModelSerializers.config.adapter = :json_api
@@ -1,5 +1,5 @@
1
1
  module Tramway
2
2
  module Api
3
- VERSION = '0.3.0.1'
3
+ VERSION = '0.4'
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: 0.3.0.1
4
+ version: '0.4'
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-02-19 00:00:00.000000000 Z
11
+ date: 2019-02-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: knock
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: 1.9.2
41
+ - !ruby/object:Gem::Dependency
42
+ name: active_model_serializers
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '='
46
+ - !ruby/object:Gem::Version
47
+ version: 0.10.5
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '='
53
+ - !ruby/object:Gem::Version
54
+ version: 0.10.5
41
55
  description: Engine for api
42
56
  email:
43
57
  - kalashnikovisme@gmail.com
@@ -58,7 +72,9 @@ files:
58
72
  - app/jobs/tramway/api/application_job.rb
59
73
  - app/mailers/tramway/api/application_mailer.rb
60
74
  - app/models/tramway/api/application_record.rb
75
+ - app/serializers/tramway/api/v1/error_serializer.rb
61
76
  - app/views/layouts/tramway/api/application.html.erb
77
+ - config/initializers/active_model_serializers.rb
62
78
  - config/routes.rb
63
79
  - lib/tasks/tramway/api_tasks.rake
64
80
  - lib/tramway/api.rb