grape_serializer 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e6cde3413de0621c240b2c3b6843a2c5beb4fd0674211a1d124761d9ddd0f42b
4
+ data.tar.gz: 03c916507d7f224b0e0dc2d60be1f7e09fcc55e6e7ebe2a0e6733432a5a9001f
5
+ SHA512:
6
+ metadata.gz: a96423d53ed8246e87477595311cdd1bd837aa53ecb0bdae7d106fe3835e427c6476f3e80c2a37ab0d7148c07a80b7c4998d82dc1cd4cccff2a8d76b8bff3207
7
+ data.tar.gz: 24ce272712ab7b40cf25eb5e8ad7adeab25f89be03f8bd630a357ce5adc381c41507ec1872ca2a3d9ed7e1fe32f3dacf8c1917dce17455ebbc958f5e0932699a
@@ -0,0 +1,36 @@
1
+ module GrapeResponse
2
+
3
+ def serialize_response(serializer_name, resource, **options)
4
+ set_serializer_class(serializer_name)
5
+ serializer = serializer_class.new(current_member: current_member)
6
+ render status: 200, json: {data: serializer.represent(resource, options)}
7
+ end
8
+
9
+ private
10
+
11
+ def current_member
12
+ end
13
+
14
+ def controller_path
15
+ end
16
+
17
+ def serializer_class
18
+ @serializer_class
19
+ end
20
+
21
+ def serializer_class=(class_name)
22
+ @serializer_class = class_name.safe_constantize
23
+ end
24
+
25
+ def set_serializer_class(serializer_name)
26
+ serializer_class_name = "#{serializer_name.to_s.camelize}Serializer"
27
+ if controller_path.nil?
28
+ self.serializer_class = serializer_class_name
29
+ else
30
+ controller_name = controller_path.classify
31
+ self.serializer_class = controller_name.gsub(/Api::(\w+)::\S+$/, "Api::#{'\1'}::#{serializer_class_name}")
32
+ end
33
+ serializer_class
34
+ end
35
+
36
+ end
@@ -0,0 +1,32 @@
1
+ require 'grape_response'
2
+ require 'service_entity'
3
+
4
+ class GrapeSerializer
5
+
6
+ class << self
7
+ attr_reader :entity_class
8
+
9
+ def entity(entity_class)
10
+ @entity_class ||= entity_class
11
+ end
12
+ end
13
+
14
+ def initialize(params = {})
15
+ @params = params
16
+ end
17
+
18
+ def represent(resource, opts = {})
19
+ preload_associations(resource) if defined?(::Rails::Engine) && defined?(::ActiveRecord) && resource.is_a?(ActiveRecord)
20
+ self.class.entity_class.represent(resource, opts.merge(@params)).as_json
21
+ end
22
+
23
+ protected
24
+
25
+ def preload_associations(resource)
26
+ ActiveRecord::Associations::Preloader.new.preload(resource, association_array)
27
+ end
28
+
29
+ def association_array
30
+ end
31
+
32
+ end
@@ -0,0 +1,15 @@
1
+ require 'grape-entity'
2
+
3
+ class ServiceEntity < Grape::Entity
4
+
5
+ # expose service additional info by default
6
+ expose :service_info, merge: true, if: :service
7
+
8
+ protected
9
+
10
+ def service_info
11
+ service = options[:service]
12
+ service_name = service.class.name.underscore
13
+ send(service_name) rescue nil || send(service_name.split('/').last) rescue {}
14
+ end
15
+ end
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: grape_serializer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - JiaRou Lee
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-11-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: grape-entity
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.10.2
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.10.2
27
+ description: A serializer base on grape-entity gem
28
+ email: laura34963@kdanmobile.com
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - lib/grape_response.rb
34
+ - lib/grape_serializer.rb
35
+ - lib/service_entity.rb
36
+ homepage: https://github.com/kdan-mobile-software-ltd/grape_serializer
37
+ licenses:
38
+ - MIT
39
+ metadata: {}
40
+ post_install_message:
41
+ rdoc_options: []
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ requirements: []
55
+ rubygems_version: 3.2.3
56
+ signing_key:
57
+ specification_version: 4
58
+ summary: Serializer for Grape Entity
59
+ test_files: []