json_api_server 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. checksums.yaml +7 -0
  2. data/.dockerignore +7 -0
  3. data/.gitignore +14 -0
  4. data/.rspec +2 -0
  5. data/.rubocop.yml +35 -0
  6. data/.travis.yml +5 -0
  7. data/CODE_OF_CONDUCT.md +74 -0
  8. data/Dockerfile +9 -0
  9. data/Gemfile +10 -0
  10. data/LICENSE.txt +21 -0
  11. data/README.md +432 -0
  12. data/Rakefile +6 -0
  13. data/bin/console +14 -0
  14. data/bin/setup +8 -0
  15. data/config/locales/en.yml +32 -0
  16. data/docker-compose.yml +10 -0
  17. data/json_api_server.gemspec +50 -0
  18. data/lib/json_api_server.rb +76 -0
  19. data/lib/json_api_server/api_version.rb +8 -0
  20. data/lib/json_api_server/attributes_builder.rb +135 -0
  21. data/lib/json_api_server/base_serializer.rb +169 -0
  22. data/lib/json_api_server/builder.rb +201 -0
  23. data/lib/json_api_server/cast.rb +89 -0
  24. data/lib/json_api_server/configuration.rb +99 -0
  25. data/lib/json_api_server/controller/error_handling.rb +164 -0
  26. data/lib/json_api_server/engine.rb +5 -0
  27. data/lib/json_api_server/error.rb +64 -0
  28. data/lib/json_api_server/errors.rb +50 -0
  29. data/lib/json_api_server/exceptions.rb +6 -0
  30. data/lib/json_api_server/fields.rb +76 -0
  31. data/lib/json_api_server/filter.rb +255 -0
  32. data/lib/json_api_server/filter_builders.rb +135 -0
  33. data/lib/json_api_server/filter_config.rb +71 -0
  34. data/lib/json_api_server/filter_parser.rb +88 -0
  35. data/lib/json_api_server/include.rb +158 -0
  36. data/lib/json_api_server/meta_builder.rb +39 -0
  37. data/lib/json_api_server/mime_types.rb +21 -0
  38. data/lib/json_api_server/pagination.rb +189 -0
  39. data/lib/json_api_server/paginator.rb +134 -0
  40. data/lib/json_api_server/relationships_builder.rb +215 -0
  41. data/lib/json_api_server/resource_serializer.rb +245 -0
  42. data/lib/json_api_server/resources_serializer.rb +131 -0
  43. data/lib/json_api_server/serializer.rb +34 -0
  44. data/lib/json_api_server/sort.rb +156 -0
  45. data/lib/json_api_server/sort_configs.rb +63 -0
  46. data/lib/json_api_server/validation_errors.rb +51 -0
  47. data/lib/json_api_server/version.rb +3 -0
  48. metadata +259 -0
@@ -0,0 +1,63 @@
1
+ module JsonApiServer # :nodoc:
2
+ # Configs that can be specified in a controller to enable sorting on attributes.
3
+ # Specify permitted attributes for sorting. If an alias is used instead of an
4
+ # attribute name, it can be mapped (see :created in example below).
5
+ #
6
+ # Example sort configuration:
7
+ #
8
+ # before_action do |c|
9
+ # c.sort_options = {
10
+ # permitted: [
11
+ # :id,
12
+ # :body,
13
+ # { created: { col_name: :created_at} }
14
+ # ],
15
+ # default: { id: :desc }
16
+ # }
17
+ # end
18
+ #
19
+ # Config requirements - :permitted, :col_name and :default must be symbols.
20
+ class SortConfigs
21
+ # Config - permitted
22
+ def initialize(configs)
23
+ @configs = configs
24
+ end
25
+
26
+ # Attributes API users can sort against. Arrray of hashes -
27
+ # [{attr: <required - attr as string>, col_name: <optional key - database column name as string> }, ...]
28
+ def permitted
29
+ @permitted ||= begin
30
+ return [] unless @configs[:permitted].is_a?(Array)
31
+ @configs[:permitted].map { |c| configs_from(c) }
32
+ end
33
+ end
34
+
35
+ # Returns the config spec for an attributes. Returns nil if attribute
36
+ # isn't permitted.
37
+ def config_for(attr)
38
+ permitted.find { |elem| elem[:attr] == attr.to_s }
39
+ end
40
+
41
+ def permitted?(attr)
42
+ config_for(attr.to_s) != nil
43
+ end
44
+
45
+ # Default order specified in 'options' accessor. Specify ActiveRecord order params,
46
+ # i.e., { id: :desc }
47
+ # Defaults to empty array if none specified.
48
+ def default_order
49
+ @default = @configs[:default] || []
50
+ end
51
+
52
+ protected
53
+
54
+ def configs_from(config)
55
+ if config.respond_to?(:keys)
56
+ key, value = config.first
57
+ { attr: key.to_s, col_name: (value[:col_name] || key).to_s }
58
+ else
59
+ { attr: config.to_s }
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,51 @@
1
+ module JsonApiServer # :nodoc:
2
+ # Converts ActiveModel validations to JSON API Errors.
3
+ # Spec: http://jsonapi.org/format/#error-objects.
4
+ #
5
+ # Calling <tt>render_422(model_instance)</tt> in a controller will create an instance of
6
+ # this class and render with appropriate headers.
7
+ #
8
+ # i.e,
9
+ #
10
+ # def create
11
+ # topic = Topic.new(topic_params)
12
+ #
13
+ # if topic.save
14
+ # serializer = TopicSerializer.new(topic)
15
+ # render json: serializer.to_json, status: :created
16
+ # else
17
+ # render_422(topic)
18
+ # end
19
+ # end
20
+ class ValidationErrors
21
+ include JsonApiServer::Serializer
22
+ include JsonApiServer::ApiVersion
23
+
24
+ def initialize(model)
25
+ errors = get_errors(model)
26
+ @errors = JsonApiServer::Errors.new(errors)
27
+ end
28
+
29
+ def as_json
30
+ @errors.as_json
31
+ end
32
+
33
+ protected
34
+
35
+ # Grabs the first error per attribute.
36
+ # Spec -> status: the HTTP status code applicable to this problem, expressed as a string value.
37
+ # http://jsonapi.org/format/#error-objects
38
+ def get_errors(model)
39
+ if model.respond_to?(:errors) && model.errors.respond_to?(:full_messages_for)
40
+ model.errors.keys.map do |field|
41
+ {
42
+ 'status' => '422',
43
+ 'source' => { 'pointer' => "/data/attributes/#{field}" },
44
+ 'title' => 'Invalid Attribute',
45
+ 'detail' => model.errors.full_messages_for(field).first
46
+ }
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,3 @@
1
+ module JsonApiServer # :nodoc:
2
+ VERSION = '0.0.1'.freeze
3
+ end
metadata ADDED
@@ -0,0 +1,259 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: json_api_server
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - ed.mare
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-09-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: oj
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: railties
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '5.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '5.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: activesupport
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: will_paginate
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 3.1.0
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 3.1.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: bundler
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '1.13'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '1.13'
83
+ - !ruby/object:Gem::Dependency
84
+ name: sqlite3
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rake
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '10.0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '10.0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rspec
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '3.0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '3.0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: rails
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '4.1'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '4.1'
139
+ - !ruby/object:Gem::Dependency
140
+ name: rspec-rails
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '3.5'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '3.5'
153
+ - !ruby/object:Gem::Dependency
154
+ name: rubocop
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0.47'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0.47'
167
+ description: For building JSON APIs.
168
+ email:
169
+ executables: []
170
+ extensions: []
171
+ extra_rdoc_files: []
172
+ files:
173
+ - ".dockerignore"
174
+ - ".gitignore"
175
+ - ".rspec"
176
+ - ".rubocop.yml"
177
+ - ".travis.yml"
178
+ - CODE_OF_CONDUCT.md
179
+ - Dockerfile
180
+ - Gemfile
181
+ - LICENSE.txt
182
+ - README.md
183
+ - Rakefile
184
+ - bin/console
185
+ - bin/setup
186
+ - config/locales/en.yml
187
+ - docker-compose.yml
188
+ - json_api_server.gemspec
189
+ - lib/json_api_server.rb
190
+ - lib/json_api_server/api_version.rb
191
+ - lib/json_api_server/attributes_builder.rb
192
+ - lib/json_api_server/base_serializer.rb
193
+ - lib/json_api_server/builder.rb
194
+ - lib/json_api_server/cast.rb
195
+ - lib/json_api_server/configuration.rb
196
+ - lib/json_api_server/controller/error_handling.rb
197
+ - lib/json_api_server/engine.rb
198
+ - lib/json_api_server/error.rb
199
+ - lib/json_api_server/errors.rb
200
+ - lib/json_api_server/exceptions.rb
201
+ - lib/json_api_server/fields.rb
202
+ - lib/json_api_server/filter.rb
203
+ - lib/json_api_server/filter_builders.rb
204
+ - lib/json_api_server/filter_config.rb
205
+ - lib/json_api_server/filter_parser.rb
206
+ - lib/json_api_server/include.rb
207
+ - lib/json_api_server/meta_builder.rb
208
+ - lib/json_api_server/mime_types.rb
209
+ - lib/json_api_server/pagination.rb
210
+ - lib/json_api_server/paginator.rb
211
+ - lib/json_api_server/relationships_builder.rb
212
+ - lib/json_api_server/resource_serializer.rb
213
+ - lib/json_api_server/resources_serializer.rb
214
+ - lib/json_api_server/serializer.rb
215
+ - lib/json_api_server/sort.rb
216
+ - lib/json_api_server/sort_configs.rb
217
+ - lib/json_api_server/validation_errors.rb
218
+ - lib/json_api_server/version.rb
219
+ homepage: https://github.com/ed-mare/json_api_server
220
+ licenses:
221
+ - MIT
222
+ metadata:
223
+ allowed_push_host: https://rubygems.org
224
+ post_install_message:
225
+ rdoc_options:
226
+ - "--main"
227
+ - README.md
228
+ - "--exclude"
229
+ - spec
230
+ - "--exclude"
231
+ - bin
232
+ - "--exclude"
233
+ - Dockerfile
234
+ - "--exclude"
235
+ - Gemfile
236
+ - "--exclude"
237
+ - Gemfile.lock
238
+ - "--exclude"
239
+ - Rakefile
240
+ require_paths:
241
+ - lib
242
+ required_ruby_version: !ruby/object:Gem::Requirement
243
+ requirements:
244
+ - - ">="
245
+ - !ruby/object:Gem::Version
246
+ version: '2.1'
247
+ required_rubygems_version: !ruby/object:Gem::Requirement
248
+ requirements:
249
+ - - ">="
250
+ - !ruby/object:Gem::Version
251
+ version: '0'
252
+ requirements: []
253
+ rubyforge_project:
254
+ rubygems_version: 2.5.1
255
+ signing_key:
256
+ specification_version: 4
257
+ summary: Implements JSON API 1.0 - data, errors, meta, pagination, sort, filters,
258
+ sparse fieldsets and inclusion of related resources.
259
+ test_files: []