Skip to content

Commit 804ea42

Browse files
committed
Small refactor + update dependencies.
1 parent 10ac9c7 commit 804ea42

File tree

2 files changed

+14
-27
lines changed

2 files changed

+14
-27
lines changed

jsonapi-hanami.gemspec

+2-3
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@ Gem::Specification.new do |spec|
1414
spec.files = Dir['README.md', 'lib/**/*']
1515
spec.require_path = 'lib'
1616

17-
spec.add_dependency 'jsonapi-deserializable', '0.1.1.beta3'
18-
spec.add_dependency 'jsonapi-parser', '0.1.1.beta3'
19-
spec.add_dependency 'jsonapi-serializable', '0.1.1.beta4'
17+
spec.add_dependency 'jsonapi-deserializable', '0.1.1'
18+
spec.add_dependency 'jsonapi-serializable', '0.1.1'
2019

2120
spec.add_development_dependency 'rake', '~> 11.3'
2221
spec.add_development_dependency 'rspec', '~> 3.5'

lib/jsonapi/hanami/deserialization.rb

+12-24
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
require 'jsonapi/deserializable'
2-
require 'jsonapi/parser'
32

43
module JSONAPI
54
module Hanami
@@ -9,18 +8,20 @@ def self.included(base)
98
end
109

1110
module ClassMethods
12-
def deserializable_resource(key, klass = nil, &block)
13-
if klass.nil?
14-
klass = Class.new(JSONAPI::Deserializable::Resource, &block)
15-
end
16-
use DeserializeResource, key, klass
11+
def deserializable_resource(key, options = {}, &block)
12+
_deserializable(key, options,
13+
JSONAPI::Deserializable::Resource, &block)
1714
end
1815

19-
def deserializable_relationship(key, klass = nil, &block)
20-
if klass.nil?
21-
klass = Class.new(JSONAPI::Deserializable::Relationship, &block)
22-
end
23-
use DeserializeRelationship, key, klass
16+
def deserializable_relationship(key, options = {}, &block)
17+
_deserializable(key, options,
18+
JSONAPI::Deserializable::Relationship, &block)
19+
end
20+
21+
# @api private
22+
def _deserializable(key, options, fallback, &block)
23+
klass = options[:class] || Class.new(fallback, &block)
24+
use DeserializationMiddleware, key, klass
2425
end
2526
end
2627

@@ -37,7 +38,6 @@ def initialize(app, key, klass)
3738

3839
def call(env)
3940
body = env[ROUTER_PARSED_BODY]
40-
parser.parse!(body)
4141
deserialized_hash = @deserializable_class.call(body)
4242
params = env[ROUTER_PARAMS]
4343
# TODO(beauby): Actually replace the request body upstream instead
@@ -51,18 +51,6 @@ def call(env)
5151
@app.call(env)
5252
end
5353
end
54-
55-
class DeserializeResource < DeserializationMiddleware
56-
def parser
57-
JSONAPI::Parser::Resource
58-
end
59-
end
60-
61-
class DeserializeRelationship < DeserializationMiddleware
62-
def parser
63-
JSONAPI::Parser::Relationship
64-
end
65-
end
6654
end
6755
end
6856
end

0 commit comments

Comments
 (0)