jsonapi-hanami 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: 94e7c1facd5e28912e6075ba466857778aa4bea7
4
- data.tar.gz: 9a59bdd9aa7c79c08fb6f0cf7b3da9b4092a268f
3
+ metadata.gz: b0b7a3e38db607b870ef29a1c25959f8eff78b4d
4
+ data.tar.gz: 61936fb8435c552b44a5f7b498efbcd436c7d168
5
5
  SHA512:
6
- metadata.gz: 4af00dfe4cc9636eb6960a93cf5f8752310fa125c2f2d22ea06a76051e20fd81af970a3eae760c5962926fbc521b9737f37add926686798de865055cfa8ef423
7
- data.tar.gz: c78e95aaff61c47b5a28890e2d694910e1a081e66c4f6093b91e64b16d80e7cd019940e822197f632ba699403af0e9478d804c0d9b9ea11ca9534c69ce6b8fce
6
+ metadata.gz: eefac983e6f2d404a2f85ae2d7469a5bb5f7370062dcf5b41e3363479f2449f5d45eb447b640a32cce1bf1bdbc492e47faa789607645c5cb35240561eb908c7f
7
+ data.tar.gz: e0a1656fa2b6f5eb1913efbd377c411fff3339c62619b83e3d786316e022f3017cef1093b9d783a9aec63795cf8723c4da288880313766231ac7719587365e86
data/README.md CHANGED
@@ -40,9 +40,21 @@ module API::Controllers::Posts
40
40
  end
41
41
 
42
42
  def call(params)
43
- User.new(params[:user])
44
- # ...
45
- self.body = jsonapi_render(resources)
43
+ unless params.valid?
44
+ self.errors = params.errors
45
+ return
46
+ end
47
+
48
+ repo = UserRepository.new
49
+ user = repo.create(params[:user])
50
+
51
+ self.data = user
52
+ self.meta = { foo: 'bar' }
53
+ self.links = { self: 'foo://bar' }
54
+ self.jsonapi = { version: '1.0', meta: { foo: 'bar' } }
55
+ # Also available:
56
+ # self.included = { posts: [:author, comments: [:author]] }
57
+ # self.fields = { posts: [:title, :date], users: [:name] }
46
58
  end
47
59
  end
48
60
  end
@@ -1,13 +1,13 @@
1
- require 'jsonapi/hanami/renderer'
2
- require 'jsonapi/hanami/deserializable'
1
+ require 'jsonapi/hanami/deserialization'
2
+ require 'jsonapi/hanami/rendering'
3
3
 
4
4
  module JSONAPI
5
5
  module Hanami
6
6
  module Action
7
7
  def self.included(base)
8
8
  base.class_eval do
9
- include Renderer
10
- include Deserializable
9
+ include Deserialization
10
+ include Rendering
11
11
  end
12
12
  end
13
13
  end
@@ -0,0 +1,12 @@
1
+ require 'hanami/commands/generate/abstract'
2
+ require 'hanami/application_name'
3
+
4
+ module Hanami
5
+ module Commands
6
+ class Generate
7
+ class Deserializable < Abstract
8
+
9
+ end
10
+ end
11
+ end
12
+ end
@@ -3,7 +3,7 @@ require 'jsonapi/parser'
3
3
 
4
4
  module JSONAPI
5
5
  module Hanami
6
- module Deserializable
6
+ module Deserialization
7
7
  def self.included(base)
8
8
  base.extend ClassMethods
9
9
  end
@@ -13,19 +13,21 @@ module JSONAPI
13
13
  if klass.nil?
14
14
  klass = Class.new(JSONAPI::Deserializable::Resource, &block)
15
15
  end
16
- use DeserializableResource, key, klass
16
+ use DeserializeResource, key, klass
17
17
  end
18
18
 
19
19
  def deserializable_relationship(key, klass = nil, &block)
20
20
  if klass.nil?
21
21
  klass = Class.new(JSONAPI::Deserializable::Relationship, &block)
22
22
  end
23
- use DeserializableRelationship, key, klass
23
+ use DeserializeRelationship, key, klass
24
24
  end
25
25
  end
26
26
 
27
- class DeserializableMiddleware
27
+ class DeserializationMiddleware
28
28
  ROUTER_PARAMS = 'router.params'.freeze
29
+ ROUTER_PARSED_BODY = 'router.parsed_body'.freeze
30
+ JSONAPI_KEYS = [:data, :meta, :links, :jsonapi].freeze
29
31
 
30
32
  def initialize(app, key, klass)
31
33
  @app = app
@@ -34,22 +36,29 @@ module JSONAPI
34
36
  end
35
37
 
36
38
  def call(env)
37
- env[ROUTER_PARAMS].tap do |body|
38
- parser.parse!(body)
39
- deserialized_hash = @deserializable_class.call(body)
40
- body.replace(@deserializable_key => deserialized_hash)
39
+ body = env[ROUTER_PARSED_BODY]
40
+ parser.parse!(body)
41
+ deserialized_hash = @deserializable_class.call(body)
42
+ params = env[ROUTER_PARAMS]
43
+ # TODO(beauby): Actually replace the request body upstream instead
44
+ # of hacking it here.
45
+ params[:_jsonapi] = {}
46
+ JSONAPI_KEYS.each do |key|
47
+ params[:_jsonapi][key] = params.delete(key) if params.key?(key)
41
48
  end
49
+ params[@deserializable_key] = deserialized_hash
50
+
42
51
  @app.call(env)
43
52
  end
44
53
  end
45
54
 
46
- class DeserializableResource < DeserializableMiddleware
55
+ class DeserializeResource < DeserializationMiddleware
47
56
  def parser
48
57
  JSONAPI::Parser::Resource
49
58
  end
50
59
  end
51
60
 
52
- class DeserializableRelationship < DeserializableMiddleware
61
+ class DeserializeRelationship < DeserializationMiddleware
53
62
  def parser
54
63
  JSONAPI::Parser::Relationship
55
64
  end
@@ -0,0 +1,7 @@
1
+ p "zizi"
2
+ Hanami::CliSubCommands::Generate.define_commands do
3
+ desc 'deserializable', 'Generate deserializable class.'
4
+ def deserializable(application_name = :app, resource_name)
5
+
6
+ end
7
+ end
@@ -0,0 +1,67 @@
1
+ require 'jsonapi/serializable'
2
+ require 'jsonapi/hanami/rendering/dsl'
3
+
4
+ module JSONAPI
5
+ module Hanami
6
+ module Rendering
7
+ def self.included(base)
8
+ base.class_eval do
9
+ include JSONAPI::Hanami::Rendering::DSL
10
+
11
+ after do
12
+ _jsonapi_render if @_body.nil?
13
+ end
14
+ end
15
+ end
16
+
17
+ def _jsonapi_render
18
+ if @_jsonapi.key?(:errors)
19
+ _jsonapi_render_error
20
+ else
21
+ _jsonapi_render_success
22
+ end
23
+ end
24
+
25
+ def _jsonapi_render_success
26
+ self.format = :jsonapi if @format.nil?
27
+ return unless @_jsonapi.key?(:data)
28
+ self.body = JSONAPI::Serializable::Renderer.render(@_jsonapi[:data],
29
+ _jsonapi_params)
30
+ end
31
+
32
+ # NOTE(beauby): It might be worth factoring those methods out into a
33
+ # class.
34
+ def _jsonapi_params
35
+ # TODO(beauby): Inject global params (toplevel jsonapi, etc.).
36
+ @_jsonapi.dup.merge!(expose: _jsonapi_exposures)
37
+ end
38
+
39
+ def _jsonapi_exposures
40
+ { routes: routes }.merge!(exposures)
41
+ end
42
+
43
+ def _jsonapi_render_error
44
+ document =
45
+ JSONAPI::Serializable::ErrorRenderer.render(@_jsonapi[:errors],
46
+ _jsonapi_error_params)
47
+ self.status = _jsonapi_error_status unless @_status
48
+ self.format = :jsonapi if @format.nil?
49
+ self.body = document
50
+ end
51
+
52
+ def _jsonapi_error_status
53
+ # TODO(beauby): Set HTTP status code accordingly.
54
+ 400
55
+ end
56
+
57
+ def _jsonapi_error_params
58
+ @_jsonapi
59
+ end
60
+
61
+ def _jsonapi_errors
62
+ # TODO(beauby): Implement inferrence for Hanami::Validations.
63
+ @_jsonapi[:errors]
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,47 @@
1
+ module JSONAPI
2
+ module Hanami
3
+ module Rendering
4
+ module DSL
5
+ def data=(value)
6
+ (@_jsonapi ||= {}).merge!(data: value)
7
+ end
8
+
9
+ def errors=(value)
10
+ (@_jsonapi ||= {}).merge!(errors: value)
11
+ end
12
+
13
+ def include=(value)
14
+ (@_jsonapi ||= {}).merge!(include: value)
15
+ end
16
+
17
+ def fields=(value)
18
+ (@_jsonapi ||= {}).merge!(fields: value)
19
+ end
20
+
21
+ def meta=(value)
22
+ (@_jsonapi ||= {}).merge!(meta: value)
23
+ end
24
+
25
+ def links=(value)
26
+ (@_jsonapi ||= {}).merge!(links: value)
27
+ end
28
+
29
+ def jsonapi=(value)
30
+ (@_jsonapi ||= {}).merge!(jsonapi: value)
31
+ end
32
+
33
+ def serializable_class=(value)
34
+ (@_jsonapi ||= {}).merge!(class: value)
35
+ end
36
+
37
+ def serializable_namespace=(value)
38
+ (@_jsonapi ||= {}).merge!(namespace: value)
39
+ end
40
+
41
+ def serializable_inferer=(value)
42
+ (@_jsonapi ||= {}).merge!(inferer: value)
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsonapi-hanami
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
7
  - Lucas Hosseini
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-10-29 00:00:00.000000000 Z
11
+ date: 2016-11-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jsonapi-renderer
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - '='
18
18
  - !ruby/object:Gem::Version
19
- version: 0.1.1.beta2
19
+ version: 0.1.1.beta3
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.beta2
26
+ version: 0.1.1.beta3
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: jsonapi-parser
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -44,14 +44,14 @@ dependencies:
44
44
  requirements:
45
45
  - - '='
46
46
  - !ruby/object:Gem::Version
47
- version: 0.1.1.beta2
47
+ version: 0.1.1.beta3
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - '='
53
53
  - !ruby/object:Gem::Version
54
- version: 0.1.1.beta2
54
+ version: 0.1.1.beta3
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: jsonapi-deserializable
57
57
  requirement: !ruby/object:Gem::Requirement
@@ -70,31 +70,31 @@ dependencies:
70
70
  name: rake
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - ">="
73
+ - - "~>"
74
74
  - !ruby/object:Gem::Version
75
- version: '0.9'
75
+ version: '11.3'
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - ">="
80
+ - - "~>"
81
81
  - !ruby/object:Gem::Version
82
- version: '0.9'
82
+ version: '11.3'
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: rspec
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
87
  - - "~>"
88
88
  - !ruby/object:Gem::Version
89
- version: '3.4'
89
+ version: '3.5'
90
90
  type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
94
  - - "~>"
95
95
  - !ruby/object:Gem::Version
96
- version: '3.4'
97
- description: Efficiently build and consume JSON API documents.
96
+ version: '3.5'
97
+ description: Efficient, convenient, non-intrusive JSONAPI framework for Hanami.
98
98
  email: lucas.hosseini@gmail.com
99
99
  executables: []
100
100
  extensions: []
@@ -104,8 +104,11 @@ files:
104
104
  - lib/generators/hanami/serializable.rb
105
105
  - lib/jsonapi/hanami.rb
106
106
  - lib/jsonapi/hanami/action.rb
107
- - lib/jsonapi/hanami/deserializable.rb
108
- - lib/jsonapi/hanami/renderer.rb
107
+ - lib/jsonapi/hanami/commands/generate/deserializable.rb
108
+ - lib/jsonapi/hanami/deserialization.rb
109
+ - lib/jsonapi/hanami/generators/deserializable.rb
110
+ - lib/jsonapi/hanami/rendering.rb
111
+ - lib/jsonapi/hanami/rendering/dsl.rb
109
112
  homepage: https://github.com/jsonapi-rb/hanami
110
113
  licenses:
111
114
  - MIT
@@ -129,5 +132,5 @@ rubyforge_project:
129
132
  rubygems_version: 2.5.1
130
133
  signing_key:
131
134
  specification_version: 4
132
- summary: Hanami plugin for (de)serialization of JSON API resources.
135
+ summary: jsonapi-rb integrations for Hanami.
133
136
  test_files: []
@@ -1,13 +0,0 @@
1
- require 'jsonapi/renderer'
2
-
3
- module JSONAPI
4
- module Hanami
5
- module Renderer
6
- def jsonapi_render(resources, params = {})
7
- # TODO(beauby): Serializable inference with helpers injection.
8
- # TODO(beauby): Inject global params (toplevel jsonapi, etc.).
9
- JSONAPI::Renderer.new(resources, params).as_json
10
- end
11
- end
12
- end
13
- end