jsonapi-rails 0.1.1.beta1 → 0.1.1.beta2

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
  SHA1:
3
- metadata.gz: 149fb06216ef18af6baa06e71acca5d6fa7cbeda
4
- data.tar.gz: b83bbdc7d9d5739a7361eb294b28b1b1ff53a8d2
3
+ metadata.gz: a2187b8d1ba0e20c3d0e23745e04e2072b56e43a
4
+ data.tar.gz: 6bb485f12b4d58a8132388faf76695cd503ddf94
5
5
  SHA512:
6
- metadata.gz: 785524db1f37f2b08fb6f2c96d1811f8ba5364dfc7ae92a3d1b47c211a44f88fc4ab85575e70bbc2fd0c56a640f8a1fa199adea2debff6a07b591370df1078df
7
- data.tar.gz: 4672480284c03e809ecbb527aa8cad84593ef358e936182a1c3ae17aaea19ddad0ad27ed5f6ba817b6d52791cb11099d4523ff8cb37bca3621cd2657fd4b80d4
6
+ metadata.gz: 1ae73e72c786c2751afe9168eed6f5251ca9479bf45faca4594f08243cedca1e8aac8c59468258689601f32b228a71057d3510efb1851b664e8c14bf961da154
7
+ data.tar.gz: 6490dcde60b48fab7e83bc14036575a5b7644ddba4b9aac4f1b80fd77a09443aa25318bd542ceeeec2b5c6ae34c81f575fd0ba41d4b1740afa7539fede4a7fce
data/README.md CHANGED
@@ -1,5 +1,10 @@
1
1
  # jsonapi-rails
2
- Rails plugin for building/consuming [JSON API](http://jsonapi.org) documents.
2
+ Rails integration for [jsonapi-rb](https://github.com/jsonapi-rb/jsonapi-rb).
3
+
4
+ ## Status
5
+
6
+ [![Gem Version](https://badge.fury.io/rb/jsonapi-rails.svg)](https://badge.fury.io/rb/jsonapi-rails)
7
+ [![Build Status](https://secure.travis-ci.org/jsonapi-rb/rails.svg?branch=master)](http://travis-ci.org/jsonapi-rb/rails?branch=master)
3
8
 
4
9
  ## Installation
5
10
 
@@ -20,18 +25,6 @@ $ gem install jsonapi-rails
20
25
 
21
26
  TODO
22
27
 
23
- ## Contributing
24
-
25
- 1. Fork the [official repository](https://github.com/beauby/jsonapi-rails).
26
- 2. Make your changes in a topic branch.
27
- 3. Send a pull request.
28
-
29
- Notes:
30
-
31
- * Contributions without tests won't be accepted.
32
- * Please don't update the Gem version.
33
-
34
28
  ## License
35
29
 
36
- It is free software, and may be redistributed under the terms specified in the
37
- [LICENSE](LICENSE) file.
30
+ jsonapi-rails is released under the [MIT License](http://www.opensource.org/licenses/MIT).
@@ -4,7 +4,6 @@ require 'jsonapi/renderer'
4
4
  require 'jsonapi/deserializable'
5
5
  require 'jsonapi/serializable'
6
6
 
7
- require 'jsonapi/rails/serializable/model_extensions'
8
7
  require 'jsonapi/rails/deserializable/resource_extensions'
9
8
 
10
9
  require 'jsonapi/rails/railtie'
@@ -0,0 +1,71 @@
1
+ require 'jsonapi/deserializable'
2
+ require 'jsonapi/parser'
3
+
4
+ module JSONAPI
5
+ module Rails
6
+ module ActionController
7
+ def self.included(base)
8
+ base.class_eval do
9
+ extend ClassMethods
10
+ end
11
+ end
12
+
13
+ module ClassMethods
14
+ def deserializable_resource(key, *args, &block)
15
+ klass = args.shift unless args.first.is_a?(Hash)
16
+ options = args.first || {}
17
+ if klass.nil?
18
+ klass = Class.new(JSONAPI::Deserializable::Resource, &block)
19
+ end
20
+ use DeserializeResource, key, klass, options
21
+ end
22
+
23
+ def deserializable_relationship(key, *args, &block)
24
+ klass = args.shift unless args.first.is_a?(Hash)
25
+ options = args.first || {}
26
+ if klass.nil?
27
+ klass = Class.new(JSONAPI::Deserializable::Relationship, &block)
28
+ end
29
+ use DeserializeResource, key, klass, options
30
+ end
31
+ end
32
+
33
+ class DeserializationMiddleware
34
+ JSONAPI_KEYS = %w(data meta links jsonapi).freeze
35
+ def initialize(app, key, klass)
36
+ @app = app
37
+ @deserializable_key = key
38
+ @deserializable_class = klass
39
+ end
40
+
41
+ def call(env)
42
+ request = Rack::Request.new(env)
43
+ body = request.params.slice(*JSONAPI_KEYS)
44
+ parser.parse!(body)
45
+ deserialized_hash = @deserializable_class.call(body)
46
+ jsonapi = {}
47
+ JSONAPI_KEYS.each do |key|
48
+ next unless request.params.key?(key)
49
+ jsonapi[key.to_sym] = request.delete_param(key)
50
+ end
51
+ request.update_param(:_jsonapi, jsonapi)
52
+ request.update_param(@deserializable_key, deserialized_hash)
53
+
54
+ @app.call(env)
55
+ end
56
+ end
57
+
58
+ class DeserializeResource < DeserializationMiddleware
59
+ def parser
60
+ JSONAPI::Parser::Resource
61
+ end
62
+ end
63
+
64
+ class DeserializeRelationship < DeserializationMiddleware
65
+ def parser
66
+ JSONAPI::Parser::Relationship
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
@@ -1,5 +1,3 @@
1
- require 'jsonapi/parser'
2
-
3
1
  module JSONAPI
4
2
  module Rails
5
3
  module_function
@@ -7,7 +5,6 @@ module JSONAPI
7
5
  def parser
8
6
  lambda do |body|
9
7
  data = JSON.parse(body)
10
- JSONAPI.parse_resource!(body)
11
8
  data = { _json: data } unless data.is_a?(Hash)
12
9
  data.with_indifferent_access
13
10
  end
@@ -1,18 +1,15 @@
1
1
  require 'rails/railtie'
2
2
  require 'action_controller'
3
3
  require 'action_controller/railtie'
4
+ require 'active_support'
4
5
 
5
6
  require 'jsonapi/rails/parser'
6
- require 'jsonapi/rails/render'
7
+ require 'jsonapi/rails/renderer'
7
8
 
8
9
  module JSONAPI
9
10
  module Rails
10
11
  class Railtie < ::Rails::Railtie
11
12
  MEDIA_TYPE = 'application/vnd.api+json'.freeze
12
- HEADERS = {
13
- response: { 'CONTENT_TYPE'.freeze => MEDIA_TYPE },
14
- request: { 'ACCEPT'.freeze => MEDIA_TYPE }
15
- }.freeze
16
13
  PARSER = JSONAPI::Rails.parser
17
14
 
18
15
  initializer 'JSONAPI::Rails.initialize' do
@@ -22,17 +19,28 @@ module JSONAPI
22
19
  else
23
20
  ActionDispatch::ParamsParser::DEFAULT_PARSERS[Mime[:jsonapi]] = PARSER
24
21
  end
22
+
25
23
  ActionController::Renderers.add :jsonapi do |json, options|
26
24
  unless json.is_a?(String)
27
- json = JSONAPI::Rails.render(json, options)
28
- .to_json(options)
25
+ json = JSONAPI::Rails::Renderer.render(json, options)
29
26
  end
30
27
  self.content_type ||= Mime[:jsonapi]
31
28
  self.response_body = json
32
29
  end
33
30
 
34
- # TODO(beauby): Add renderer for `jsonapi_errors`.
31
+ ActionController::Renderers.add :jsonapi_errors do |json, options|
32
+ unless json.is_a?(String)
33
+ json = JSONAPI::Rails::ErrorRender.render_errors(json, options)
34
+ end
35
+ self.content_type ||= Mime[:jsonapi]
36
+ self.response_body = json
37
+ end
35
38
  end
36
39
  end
37
40
  end
38
41
  end
42
+
43
+ ActiveSupport.on_load(:action_controller) do
44
+ require 'jsonapi/rails/action_controller'
45
+ include JSONAPI::Rails::ActionController
46
+ end
@@ -0,0 +1,27 @@
1
+ require 'jsonapi/serializable/renderer'
2
+ require 'jsonapi/serializable/resource_builder'
3
+
4
+ module JSONAPI
5
+ module Rails
6
+ class Renderer
7
+ def self.render(resources, options)
8
+ # TODO(beauby): handle status option.
9
+ opts = options.dup
10
+ # TODO(beauby): Move this to a global configuration.
11
+ default_exposures = {
12
+ url_helpers: ::Rails.application.routes.url_helpers
13
+ }
14
+ opts[:expose] = default_exposures.merge!(opts[:expose] || {})
15
+
16
+ JSONAPI::Serializable::Renderer.render(resources, opts)
17
+ end
18
+ end
19
+
20
+ class ErrorRenderer
21
+ def self.render(errors, options)
22
+ # TODO(beauby): SerializableError inference on AR validation errors.
23
+ JSONAPI::Serializable::ErrorRenderer.render(errors, options)
24
+ end
25
+ end
26
+ end
27
+ end
metadata CHANGED
@@ -1,15 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsonapi-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1.beta1
4
+ version: 0.1.1.beta2
5
5
  platform: ruby
6
6
  authors:
7
- - L. Preston Sego III
8
7
  - Lucas Hosseini
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2016-10-25 00:00:00.000000000 Z
11
+ date: 2016-11-16 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: jsonapi-renderer
@@ -17,14 +16,14 @@ dependencies:
17
16
  requirements:
18
17
  - - '='
19
18
  - !ruby/object:Gem::Version
20
- version: 0.1.1.beta2
19
+ version: 0.1.1.beta3
21
20
  type: :runtime
22
21
  prerelease: false
23
22
  version_requirements: !ruby/object:Gem::Requirement
24
23
  requirements:
25
24
  - - '='
26
25
  - !ruby/object:Gem::Version
27
- version: 0.1.1.beta2
26
+ version: 0.1.1.beta3
28
27
  - !ruby/object:Gem::Dependency
29
28
  name: jsonapi-parser
30
29
  requirement: !ruby/object:Gem::Requirement
@@ -45,14 +44,14 @@ dependencies:
45
44
  requirements:
46
45
  - - '='
47
46
  - !ruby/object:Gem::Version
48
- version: 0.1.1.beta2
47
+ version: 0.1.1.beta3
49
48
  type: :runtime
50
49
  prerelease: false
51
50
  version_requirements: !ruby/object:Gem::Requirement
52
51
  requirements:
53
52
  - - '='
54
53
  - !ruby/object:Gem::Version
55
- version: 0.1.1.beta2
54
+ version: 0.1.1.beta3
56
55
  - !ruby/object:Gem::Dependency
57
56
  name: jsonapi-deserializable
58
57
  requirement: !ruby/object:Gem::Requirement
@@ -115,17 +114,16 @@ dependencies:
115
114
  requirements:
116
115
  - - "~>"
117
116
  - !ruby/object:Gem::Version
118
- version: '3.4'
117
+ version: '3.5'
119
118
  type: :development
120
119
  prerelease: false
121
120
  version_requirements: !ruby/object:Gem::Requirement
122
121
  requirements:
123
122
  - - "~>"
124
123
  - !ruby/object:Gem::Version
125
- version: '3.4'
126
- description: Efficiently build and consume JSONAPI documents.
124
+ version: '3.5'
125
+ description: Efficient, convenient, non-intrusive JSONAPI framework for Rails.
127
126
  email:
128
- - LPSego3+dev@gmail.com
129
127
  - lucas.hosseini@gmail.com
130
128
  executables: []
131
129
  extensions: []
@@ -134,25 +132,17 @@ files:
134
132
  - README.md
135
133
  - lib/generators/jsonapi/deserializable/USAGE
136
134
  - lib/generators/jsonapi/deserializable/deserializable_generator.rb
137
- - lib/generators/jsonapi/deserializable/serializable_generator.rb
138
135
  - lib/generators/jsonapi/deserializable/templates/deserializable.rb.erb
139
- - lib/generators/jsonapi/deserializable/templates/serializable.rb
140
- - lib/generators/jsonapi/deserializable/templates/serializable.rb.erb
141
136
  - lib/generators/jsonapi/serializable/USAGE
142
137
  - lib/generators/jsonapi/serializable/serializable_generator.rb
143
- - lib/generators/jsonapi/serializable/templates/serializable.rb
144
138
  - lib/generators/jsonapi/serializable/templates/serializable.rb.erb
145
- - lib/generators/serializable_generator.rb
146
139
  - lib/jsonapi/rails.rb
147
- - lib/jsonapi/rails/deserializable/model_extensions.rb
140
+ - lib/jsonapi/rails/action_controller.rb
148
141
  - lib/jsonapi/rails/deserializable/resource_extensions.rb
149
142
  - lib/jsonapi/rails/parser.rb
150
143
  - lib/jsonapi/rails/railtie.rb
151
- - lib/jsonapi/rails/render.rb
152
- - lib/jsonapi/rails/serializable/model_extensions.rb
153
- - lib/jsonapi/rails/version.rb
154
- - lib/tasks/jsonapi/rails_tasks.rake
155
- homepage: https://github.com/beauby/jsonapi-rails
144
+ - lib/jsonapi/rails/renderer.rb
145
+ homepage: https://github.com/jsonapi-rb/rails
156
146
  licenses:
157
147
  - MIT
158
148
  metadata: {}
@@ -175,5 +165,5 @@ rubyforge_project:
175
165
  rubygems_version: 2.5.1
176
166
  signing_key:
177
167
  specification_version: 4
178
- summary: Rails plugin for (de)serialization of JSONAPI resources
168
+ summary: jsonapi-rb integrations for Rails.
179
169
  test_files: []
@@ -1,34 +0,0 @@
1
- <% module_namespacing do -%>
2
- class Deserializable<%= class_name %> < JSONAPI::Deserializable::Model
3
- id
4
-
5
- <% attr_names.each do |attr| -%>
6
- attribute :<%= attr %>
7
- <% end -%>
8
-
9
- <% has_one_rel_names.each do |name, reflection| -%>
10
- has_one :<%= name %> do
11
- field :<%= has_one_id_field_name(name) %> do |rel|
12
- rel['data'] && rel['data']['id']
13
- end
14
- <% if reflection.polymorphic? -%>
15
- field :<%= has_one_type_field_name(name) %> do
16
- rel['data'] && rel['data']['type']
17
- end
18
- <% end -%>
19
- end
20
- <% end -%>
21
- <% has_many_rel_names.each do |rel| -%>
22
- has_many :<%= rel %> do
23
- field :<%= has_many_id_field_name(name) %> do |rel|
24
- rel['data'].map { |ri| ri['id'] }
25
- end
26
- <% if reflection.polymorphic? -%>
27
- field :<%= has_one_type_field_name(name) %> do
28
- rel['data'].map { |ri| ri['type'] }
29
- end
30
- <% end -%>
31
- end
32
- <% end -%>
33
- end
34
- <% end -%>
@@ -1,3 +0,0 @@
1
- <% module_namespacing do -%>
2
- <%
3
- # Blah blah
@@ -1,5 +0,0 @@
1
- class SerializableGenerator < Rails::Generator::Base
2
- def create_serializable_file
3
- create_file
4
- end
5
- end
@@ -1,20 +0,0 @@
1
- require 'jsonapi/deserializable/model'
2
-
3
- module JSONAPI
4
- module Rails
5
- module Deserializable
6
- module ModelExtensions
7
- def initialize(params = {})
8
- params[:url_helper] ||= ::Rails.application.routes.url_helpers
9
- super(params)
10
- end
11
- end
12
- end
13
- end
14
-
15
- module Serializable
16
- class Model
17
- prepend JSONAPI::Rails::Serializable::ModelExtensions
18
- end
19
- end
20
- end
@@ -1,33 +0,0 @@
1
- require 'jsonapi/renderer'
2
- require 'jsonapi/serializable/model'
3
-
4
- module JSONAPI
5
- module Rails
6
- module_function
7
-
8
- def render(resources, options)
9
- unless resources.respond_to?(:jsonapi_type)
10
- resources = serializable_resources_for(resources)
11
- end
12
- JSONAPI::Renderer.new(resources, options).as_json
13
- end
14
-
15
- def serializable_resources_for(resources)
16
- if resources.respond_to?(:each)
17
- resources.map { |r| serializable_model_for(r) }
18
- else
19
- serializable_model_for(resources)
20
- end
21
- end
22
-
23
- def serializable_model_for(model)
24
- serializable_klass_for(model).new(model: model)
25
- end
26
-
27
- def serializable_klass_for(model)
28
- # TODO(beauby): Move resource inference on class level in
29
- # jsonapi-serializable.
30
- JSONAPI::Serializable::Model.new.resource_klass_for(model.class)
31
- end
32
- end
33
- end
@@ -1,20 +0,0 @@
1
- require 'jsonapi/serializable/model'
2
-
3
- module JSONAPI
4
- module Rails
5
- module Serializable
6
- module ModelExtensions
7
- def initialize(params = {})
8
- params[:url_helpers] ||= ::Rails.application.routes.url_helpers
9
- super(params)
10
- end
11
- end
12
- end
13
- end
14
-
15
- module Serializable
16
- class Model
17
- prepend JSONAPI::Rails::Serializable::ModelExtensions
18
- end
19
- end
20
- end
@@ -1,5 +0,0 @@
1
- module JSONAPI
2
- module Rails
3
- VERSION = '0.1.0'
4
- end
5
- end
@@ -1,4 +0,0 @@
1
- # desc "Explaining what the task does"
2
- # task :jsonapi_rails do
3
- # # Task goes here
4
- # end