jsonapi-object-mapper 0.5.0 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +25 -7
- data/lib/jsonapi-object-mapper.rb +2 -1
- data/lib/jsonapi-object-mapper/{deserializer → deserialize}/collection.rb +3 -3
- data/lib/jsonapi-object-mapper/{deserializer → deserialize}/dsl.rb +1 -1
- data/lib/jsonapi-object-mapper/{deserializer → deserialize}/resource.rb +4 -4
- data/lib/jsonapi-object-mapper/exceptions.rb +9 -0
- data/lib/jsonapi-object-mapper/version.rb +1 -1
- data/spec/deserialize/collection_spec.rb +85 -0
- data/spec/{deserializer → deserialize}/resource_spec.rb +1 -1
- metadata +10 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cc15e84ef11d68ce266929e2a420eefb0526b05eabd8a63065747721fef0c3a3
|
4
|
+
data.tar.gz: a3a9a57a7d7492124f505cad9c05e509cd5deb5a33f1afec59c7c01295153a5e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7b18f64a3c70061e4860862fd8d8b766bbc5a78de8bf728f52b34b04eeadfc3a1ed9ac0a6e598d1bc8b083e7f82ffb9d85ab3c1a1bd00a62b536c02c2fe72f19
|
7
|
+
data.tar.gz: 43ba9a81a83f4824ed4aedddc6e947cd7812cd1775efc4d925c6e14a5a41d3ed7a86ce3317516429da17a90c42427ce29742f6168fb557fd08678565473b0c8c
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,8 +1,6 @@
|
|
1
1
|
# JsonapiObjectMapper
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
TODO: Delete this and the text above, and describe your gem
|
3
|
+
Deserialize's raw or pre-hashed JsonAPI objects into plan ruby objects as well as embeds any included relational resources.
|
6
4
|
|
7
5
|
## Installation
|
8
6
|
|
@@ -23,13 +21,13 @@ Or install it yourself as:
|
|
23
21
|
## Usage
|
24
22
|
|
25
23
|
```ruby
|
26
|
-
require "
|
24
|
+
require "jsonapi-object-mapper"
|
27
25
|
|
28
|
-
class Photo < JsonAPIObjectMapper::
|
26
|
+
class Photo < JsonAPIObjectMapper::Deserialize::Resource
|
29
27
|
attribute :image
|
30
28
|
end
|
31
29
|
|
32
|
-
class User < JsonAPIObjectMapper::
|
30
|
+
class User < JsonAPIObjectMapper::Deserialize::Resource
|
33
31
|
# Embedding with another Resource class, will deserialize the `included` resource with the given class
|
34
32
|
has_one :photo, embed_with: Photo
|
35
33
|
|
@@ -51,12 +49,32 @@ class User < JsonAPIObjectMapper::Deserializer::Resource
|
|
51
49
|
end
|
52
50
|
|
53
51
|
|
54
|
-
User.call_collection(json_payload) #=> [<#User:123>, <#User:432>]
|
55
52
|
user = User.call(json_payload) #=> <#User:123>
|
56
53
|
|
57
54
|
user.first_name #=> "FOOER"
|
58
55
|
user.last_name #=> "Bar"
|
59
56
|
|
57
|
+
# If json API Payload is a collection of data points
|
58
|
+
users = User.call(json_payload) #=> <# JsonAPIObjectMapper::Deserialize::Collection #>
|
59
|
+
|
60
|
+
users.each do |user|
|
61
|
+
user.first_name
|
62
|
+
user.last_name
|
63
|
+
end
|
64
|
+
|
65
|
+
```
|
66
|
+
|
67
|
+
### Errors
|
68
|
+
|
69
|
+
```ruby
|
70
|
+
|
71
|
+
user = User.call(json_payload)
|
72
|
+
|
73
|
+
# Aliases: document_valid?
|
74
|
+
# Inverses: errors?, invalid?, document_invalid?
|
75
|
+
user.valid? #=> false
|
76
|
+
user.errors #=> [<# OpenStruct title:..., detail: ..., source: {...}, ...>, ...]
|
77
|
+
|
60
78
|
```
|
61
79
|
|
62
80
|
## Development
|
@@ -1,8 +1,9 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require "oj"
|
4
|
+
require "jsonapi-object-mapper/exceptions"
|
4
5
|
require "jsonapi-object-mapper/parser/document"
|
5
|
-
require "jsonapi-object-mapper/
|
6
|
+
require "jsonapi-object-mapper/deserialize/resource"
|
6
7
|
require "jsonapi-object-mapper/version"
|
7
8
|
|
8
9
|
module JsonAPIObjectMapper
|
@@ -3,7 +3,7 @@
|
|
3
3
|
require "jsonapi-object-mapper/parser/errors"
|
4
4
|
|
5
5
|
module JsonAPIObjectMapper
|
6
|
-
module
|
6
|
+
module Deserialize
|
7
7
|
class Collection
|
8
8
|
include Enumerable
|
9
9
|
include JsonAPIObjectMapper::Parser::Errors
|
@@ -11,8 +11,8 @@ module JsonAPIObjectMapper
|
|
11
11
|
attr_accessor :collection_data
|
12
12
|
|
13
13
|
def initialize(parser, klass:)
|
14
|
-
raise
|
15
|
-
raise
|
14
|
+
raise InvalidResource, "Must provide a valid resource klass" unless klass.is_a?(Class)
|
15
|
+
raise InvalidParser, "Must provide a parsed document" unless parser.is_a?(JsonAPIObjectMapper::Parser::Document)
|
16
16
|
@errors = parser.errors
|
17
17
|
@collection_data =
|
18
18
|
if document_invalid?
|
@@ -1,11 +1,11 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require "jsonapi-object-mapper/
|
4
|
-
require "jsonapi-object-mapper/
|
3
|
+
require "jsonapi-object-mapper/deserialize/dsl"
|
4
|
+
require "jsonapi-object-mapper/deserialize/collection"
|
5
5
|
require "jsonapi-object-mapper/parser/errors"
|
6
6
|
|
7
7
|
module JsonAPIObjectMapper
|
8
|
-
module
|
8
|
+
module Deserialize
|
9
9
|
class Resource
|
10
10
|
include JsonAPIObjectMapper::Parser::Errors
|
11
11
|
extend DSL
|
@@ -42,7 +42,7 @@ module JsonAPIObjectMapper
|
|
42
42
|
|
43
43
|
def initialize(parser, document: nil)
|
44
44
|
super()
|
45
|
-
raise
|
45
|
+
raise InvalidParser, "Must provide a parsed document" unless parser.is_a?(JsonAPIObjectMapper::Parser::Document)
|
46
46
|
@errors = parser.errors
|
47
47
|
|
48
48
|
if document_valid?
|
@@ -0,0 +1,85 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "spec_helper"
|
4
|
+
|
5
|
+
module JsonAPIObjectMapper
|
6
|
+
module Deserialize
|
7
|
+
RSpec.describe Collection do
|
8
|
+
let(:parser) { JsonAPIObjectMapper::Parser::Document.new(payload) }
|
9
|
+
let(:payload) { {} }
|
10
|
+
|
11
|
+
describe ".new" do
|
12
|
+
context "Rising Exceptions" do
|
13
|
+
it "is expected to raise an error if the resource isn't a class object" do
|
14
|
+
expect { described_class.new(nil, klass: "Class") }.to raise_error(InvalidResource)
|
15
|
+
end
|
16
|
+
|
17
|
+
it "is expected to raise an error if the parser isn't a parsed json document" do
|
18
|
+
expect { described_class.new({}, { klass: Class }) }.to raise_error(InvalidParser)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
context "Creating a collection of resources" do
|
23
|
+
let(:payload) do
|
24
|
+
{
|
25
|
+
"data" => [
|
26
|
+
{
|
27
|
+
"id" => "1",
|
28
|
+
"type" => "foobar",
|
29
|
+
"attributes" => {
|
30
|
+
"name" => "Bert",
|
31
|
+
},
|
32
|
+
},
|
33
|
+
{
|
34
|
+
"id" => "2",
|
35
|
+
"type" => "foobar",
|
36
|
+
"attributes" => {
|
37
|
+
"name" => "Johnson",
|
38
|
+
},
|
39
|
+
},
|
40
|
+
],
|
41
|
+
}
|
42
|
+
end
|
43
|
+
it "Should collect all resources into a enumerable resource" do
|
44
|
+
foobar_klass = Class.new(JsonAPIObjectMapper::Deserialize::Resource) do
|
45
|
+
attribute :name
|
46
|
+
end
|
47
|
+
|
48
|
+
result = described_class.new(parser, klass: foobar_klass)
|
49
|
+
expect(result.valid?).to be_truthy
|
50
|
+
expect(result.count).to eq(2)
|
51
|
+
expect(result.map(&:name)).to eq(%w[Bert Johnson])
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
context "Results containing errors" do
|
56
|
+
let(:payload) do
|
57
|
+
{
|
58
|
+
"errors" => [
|
59
|
+
{
|
60
|
+
"title" => "Invalid name",
|
61
|
+
"detail" => "Look like foobar to me",
|
62
|
+
"source" => {
|
63
|
+
"pointer" => "name",
|
64
|
+
},
|
65
|
+
},
|
66
|
+
],
|
67
|
+
}
|
68
|
+
end
|
69
|
+
|
70
|
+
it "Should return as an invalid or error document" do
|
71
|
+
result = described_class.new(parser, klass: Class.new)
|
72
|
+
expect(result).to be_invalid
|
73
|
+
end
|
74
|
+
|
75
|
+
it "Should contain all error responses the in #errors field" do
|
76
|
+
result = described_class.new(parser, klass: Class.new).errors.first
|
77
|
+
expect(result.title).to eq("Invalid name")
|
78
|
+
expect(result.detail).to eq("Look like foobar to me")
|
79
|
+
expect(result.source).to include("pointer" => "name")
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jsonapi-object-mapper
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- George Protacio-Karaszi
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-07-
|
11
|
+
date: 2018-07-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: oj
|
@@ -91,14 +91,16 @@ files:
|
|
91
91
|
- bin/setup
|
92
92
|
- jsonapi-object-mapper.gemspec
|
93
93
|
- lib/jsonapi-object-mapper.rb
|
94
|
-
- lib/jsonapi-object-mapper/
|
95
|
-
- lib/jsonapi-object-mapper/
|
96
|
-
- lib/jsonapi-object-mapper/
|
94
|
+
- lib/jsonapi-object-mapper/deserialize/collection.rb
|
95
|
+
- lib/jsonapi-object-mapper/deserialize/dsl.rb
|
96
|
+
- lib/jsonapi-object-mapper/deserialize/resource.rb
|
97
|
+
- lib/jsonapi-object-mapper/exceptions.rb
|
97
98
|
- lib/jsonapi-object-mapper/parser/document.rb
|
98
99
|
- lib/jsonapi-object-mapper/parser/errors.rb
|
99
100
|
- lib/jsonapi-object-mapper/parser/included_resources.rb
|
100
101
|
- lib/jsonapi-object-mapper/version.rb
|
101
|
-
- spec/
|
102
|
+
- spec/deserialize/collection_spec.rb
|
103
|
+
- spec/deserialize/resource_spec.rb
|
102
104
|
- spec/jsonapi_object_mapper_spec.rb
|
103
105
|
- spec/spec_helper.rb
|
104
106
|
homepage: https://github.com/GeorgeKaraszi/jsonapi-object-mapper
|
@@ -126,6 +128,7 @@ signing_key:
|
|
126
128
|
specification_version: 4
|
127
129
|
summary: Digests JSON-API responses to plain old ruby objects
|
128
130
|
test_files:
|
129
|
-
- spec/
|
131
|
+
- spec/deserialize/collection_spec.rb
|
132
|
+
- spec/deserialize/resource_spec.rb
|
130
133
|
- spec/jsonapi_object_mapper_spec.rb
|
131
134
|
- spec/spec_helper.rb
|