jsonapi-rails 0.1.1 → 0.1.2

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: d03b1dcb88e6112b5b8c82e1fb400ce802fe66a5
4
- data.tar.gz: cf69d3e19c4e73a5cdf1b7543e0620ee2f6d4504
3
+ metadata.gz: e4738972836e05a1bc9efcc9c19ceabd4ce9af7a
4
+ data.tar.gz: 800b0f6bb491c2e4863e8e824d081d311753e08c
5
5
  SHA512:
6
- metadata.gz: 0233e1af894d9227ba5b396c669c500c9f244d258bf6ac66e85936566b93be555837d6be3ecb041b000aa989b6a699ab113eab73fac15c99aa1358bc441569fc
7
- data.tar.gz: 42665fbd7fc8d29d251573d20fc3a3aba19f03e1390f7bbb86fea6a337e19dc30d9435a29ef48e6474b01da41c1213cca4d5e04c1886dc0f1d475bb6b252e763
6
+ metadata.gz: 06bd1dc83d80fae68fd62cadb624ddb9ab2028a0892ae6bfe763934e8e05660108121ddee9dbefb55a7f7c316d6bf5ce7c41fc2e862b3124fcf43a0c5a83e71d
7
+ data.tar.gz: 4ba8bebdf11e2cf0b455307e78ed0d70bcc950ca5d0b0e1c72f041c5ff35b41fb0e7bcc2266077a46ed849e757e0570520ff4fc7c76891daaa429934e3778c26
data/README.md CHANGED
@@ -4,9 +4,15 @@ Rails integration for [jsonapi-rb](http://jsonapi-rb.org).
4
4
  ## Status
5
5
 
6
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/jsonapi-rails.svg?branch=master)](http://travis-ci.org/jsonapi-rb/rails?branch=master)
7
+ [![Build Status](https://secure.travis-ci.org/jsonapi-rb/jsonapi-rails.svg?branch=master)](http://travis-ci.org/jsonapi-rb/jsonapi-rails?branch=master)
8
8
  [![Gitter chat](https://badges.gitter.im/gitterHQ/gitter.png)](https://gitter.im/jsonapi-rb/Lobby)
9
9
 
10
+ ## Resources
11
+
12
+ * Chat: [gitter](http://gitter.im/jsonapi-rb)
13
+ * Twitter: [@jsonapirb](http://twitter.com/jsonapirb)
14
+ * Docs: [jsonapi-rb.org](http://jsonapi-rb.org)
15
+
10
16
  ## Installation
11
17
 
12
18
  Add the following to your application's Gemfile:
@@ -4,12 +4,19 @@ require 'jsonapi/parser'
4
4
  module JSONAPI
5
5
  module Rails
6
6
  module ActionController
7
- def self.included(base)
7
+ REVERSE_MAPPING_KEY = 'jsonapi_deserializable.reverse_mapping'.freeze
8
+
9
+ def self.prepended(base)
8
10
  base.class_eval do
9
11
  extend ClassMethods
10
12
  end
11
13
  end
12
14
 
15
+ # def render(options = {})
16
+ # reverse_mapping = request.env[REVERSE_MAPPING_KEY]
17
+ # super(options.merge(_reverse_mapping: reverse_mapping))
18
+ # end
19
+
13
20
  module ClassMethods
14
21
  def deserializable_resource(key, options = {}, &block)
15
22
  _deserializable(key, options,
@@ -41,9 +48,10 @@ module JSONAPI
41
48
  def call(env)
42
49
  request = Rack::Request.new(env)
43
50
  body = JSON.parse(request.body.read)
44
- deserialized_hash = @deserializable_class.call(body)
51
+ deserializable = @deserializable_class.new(body)
52
+ env[REVERSE_MAPPING_KEY] = deserializable.reverse_mapping
45
53
  (env[REQUEST_PARAMETERS_KEY] ||= {}).tap do |request_parameters|
46
- request_parameters[@deserializable_key] = deserialized_hash
54
+ request_parameters[@deserializable_key] = deserializable.to_hash
47
55
  end
48
56
 
49
57
  @app.call(env)
File without changes
@@ -11,14 +11,14 @@ module JSONAPI
11
11
  MEDIA_TYPE = 'application/vnd.api+json'.freeze
12
12
  PARSER = JSONAPI::Rails.parser
13
13
  RENDERERS = {
14
- jsonapi: JSONAPI::Rails.rails_renderer(SuccessRenderer),
15
- jsonapi_error: JSONAPI::Rails.rails_renderer(ErrorRenderer)
14
+ jsonapi: JSONAPI::Rails.rails_renderer(SuccessRenderer),
15
+ jsonapi_errors: JSONAPI::Rails.rails_renderer(ErrorRenderer)
16
16
  }.freeze
17
17
 
18
18
  initializer 'jsonapi-rails.action_controller' do
19
19
  ActiveSupport.on_load(:action_controller) do
20
20
  require 'jsonapi/rails/action_controller'
21
- include ::JSONAPI::Rails::ActionController
21
+ prepend ::JSONAPI::Rails::ActionController
22
22
 
23
23
  Mime::Type.register MEDIA_TYPE, :jsonapi
24
24
 
@@ -1,9 +1,21 @@
1
1
  require 'jsonapi/serializable/renderer'
2
2
 
3
+ module Serializable
4
+ class ActiveModelError < JSONAPI::Serializable::Error
5
+ title do
6
+ @message
7
+ end
8
+
9
+ source do
10
+ pointer @pointer
11
+ end
12
+ end
13
+ end
14
+
3
15
  module JSONAPI
4
16
  module Rails
5
17
  class SuccessRenderer
6
- def self.render(resources, options)
18
+ def self.render(resources, options, _request)
7
19
  opts = options.dup
8
20
  # TODO(beauby): Move this to a global configuration.
9
21
  default_exposures = {
@@ -17,8 +29,16 @@ module JSONAPI
17
29
  end
18
30
 
19
31
  class ErrorRenderer
20
- def self.render(errors, options)
21
- # TODO(beauby): SerializableError inference on AR validation errors.
32
+ def self.render(errors, options, request)
33
+ reverse_mapping =
34
+ request.env['jsonapi_deserializable.reverse_mapping']
35
+ if errors.is_a?(ActiveModel::Errors)
36
+ errors = errors.messages.map do |attr, message|
37
+ pointer = reverse_mapping[attr]
38
+ ::Serializable::ActiveModelError.new(message: message,
39
+ pointer: pointer)
40
+ end
41
+ end
22
42
  JSONAPI::Serializable::ErrorRenderer.render(errors, options)
23
43
  end
24
44
  end
@@ -28,7 +48,7 @@ module JSONAPI
28
48
  # @api private
29
49
  def rails_renderer(renderer)
30
50
  proc do |json, options|
31
- json = renderer.render(json, options) unless json.is_a?(String)
51
+ json = renderer.render(json, options, request) unless json.is_a?(String)
32
52
  self.content_type ||= Mime[:jsonapi]
33
53
  self.response_body = json
34
54
  end
metadata CHANGED
@@ -1,29 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsonapi-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lucas Hosseini
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-12-02 00:00:00.000000000 Z
11
+ date: 2017-02-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jsonapi-rb
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - '='
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 0.1.1
19
+ version: '0.1'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - '='
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: 0.1.1
26
+ version: '0.1'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rails
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -107,9 +107,11 @@ files:
107
107
  - lib/generators/jsonapi/serializable/templates/serializable.rb.erb
108
108
  - lib/jsonapi/rails.rb
109
109
  - lib/jsonapi/rails/action_controller.rb
110
+ - lib/jsonapi/rails/active_record.rb
110
111
  - lib/jsonapi/rails/parser.rb
111
112
  - lib/jsonapi/rails/railtie.rb
112
113
  - lib/jsonapi/rails/renderer.rb
114
+ - lib/jsonapi/rails/serializable_active_record_error.rb
113
115
  homepage: https://github.com/jsonapi-rb/jsonapi-rails
114
116
  licenses:
115
117
  - MIT