simple-json-api-serializer 0.3.1 → 0.4.0

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: 636cf40e78aeed265d14f599509d00c8758b1e1f
4
- data.tar.gz: 5a0032a58ffa31c720e0612ea32257168e682a78
3
+ metadata.gz: e5df1c7cd4c2c8c860ec9819d9b746b61ff27d24
4
+ data.tar.gz: 6000a8133259af0e33f0abc16e702536a8324bcb
5
5
  SHA512:
6
- metadata.gz: 2b209862422dbcfe07fb35d895cff8e96e55530d3ddda03fb5d5256feead92d07e2932be77baa5d6e869aae6de5436407c1dc694bdf25a99d4aae7b248766b6c
7
- data.tar.gz: e4907962aa34d9de5b90d04c3f91e41a1686c22bdb5de55cba63b1e3ef796976e905dbfdc7ea117fbc2dfc34c8799f99720cf4c8d7389f5776389a8cae7fc810
6
+ metadata.gz: ca12d9d4e3fa0bff22ea271ceadf865e23a99dfd19ced074d4f798fc07e4a7b8dfb7a8b8105539d11d917a3c563094e46bc91f66349a2f034c7fe9abbb9697a9
7
+ data.tar.gz: c16affcb28eaa535004cb934d0c89fe0f92d88acf039be48f0133ddcf94540cd16f9eda3eb8fdd431f22b4374940acb7a0089f0f1db274bff95e25faaf5aa4d8
data/Gemfile.lock CHANGED
@@ -1,13 +1,13 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- simple-json-api-serializer (0.3.0)
4
+ simple-json-api-serializer (0.3.1)
5
5
  activesupport (~> 4.2)
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
9
9
  specs:
10
- activesupport (4.2.3)
10
+ activesupport (4.2.4)
11
11
  i18n (~> 0.7)
12
12
  json (~> 1.7, >= 1.7.7)
13
13
  minitest (~> 5.1)
@@ -16,7 +16,7 @@ GEM
16
16
  diff-lcs (1.2.5)
17
17
  i18n (0.7.0)
18
18
  json (1.8.3)
19
- minitest (5.8.0)
19
+ minitest (5.8.1)
20
20
  rake (10.4.2)
21
21
  rspec (3.3.0)
22
22
  rspec-core (~> 3.3.0)
data/lib/json_api.rb CHANGED
@@ -2,4 +2,5 @@ module JSONApi
2
2
  require 'json_api/utils'
3
3
  require 'json_api/object_serializer'
4
4
  require 'json_api/object_serializer_definition'
5
+ require 'json_api/params_deserializer'
5
6
  end
@@ -0,0 +1,48 @@
1
+ module JSONApi
2
+ class ParamsDeserializer
3
+ def self.deserialize(data)
4
+ self.new.deserialize(data)
5
+ end
6
+
7
+ def deserialize(data)
8
+ type = sanitize_type_name(data.fetch('type'))
9
+
10
+ attributes = sanitize_hash(data.fetch('attributes', {}))
11
+ relationships = data.fetch('relationships', {})
12
+
13
+ deserialize_relationships(relationships, attributes)
14
+
15
+ { type => attributes }
16
+ end
17
+
18
+ private
19
+
20
+ def deserialize_relationships(relationships, attributes)
21
+ relationships.each do |name, data|
22
+ data = data.fetch('data')
23
+ name = sanitize_attribute_name(name)
24
+
25
+ attributes["#{name}_id"] = data.fetch('id')
26
+ attributes["#{name}_type"] = sanitize_type_name(data.fetch('type'))
27
+ end
28
+ end
29
+
30
+ def sanitize_hash(hash)
31
+ hash.map do |key, value|
32
+ value = sanitize_hash(value) if value.is_a?(Hash)
33
+ [sanitize_attribute_name(key), value]
34
+ end.to_h
35
+ end
36
+
37
+ def sanitize_attribute_name(attribute_name)
38
+ attribute_name
39
+ .downcase
40
+ .underscore
41
+ end
42
+
43
+ def sanitize_type_name(type_name)
44
+ sanitize_attribute_name(type_name)
45
+ .singularize
46
+ end
47
+ end
48
+ end
@@ -1,3 +1,3 @@
1
1
  module JSONApi
2
- VERSION = '0.3.1'
2
+ VERSION = '0.4.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple-json-api-serializer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Marten Schilstra
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-09-21 00:00:00.000000000 Z
11
+ date: 2015-09-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -87,6 +87,7 @@ files:
87
87
  - lib/json_api.rb
88
88
  - lib/json_api/object_serializer.rb
89
89
  - lib/json_api/object_serializer_definition.rb
90
+ - lib/json_api/params_deserializer.rb
90
91
  - lib/json_api/relationship_serializer.rb
91
92
  - lib/json_api/utils.rb
92
93
  - lib/json_api/version.rb
@@ -111,7 +112,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
111
112
  version: '0'
112
113
  requirements: []
113
114
  rubyforge_project:
114
- rubygems_version: 2.4.6
115
+ rubygems_version: 2.4.5.1
115
116
  signing_key:
116
117
  specification_version: 4
117
118
  summary: An extremely simple JSON Api serializer