jsonapi_spec_helpers 0.3.2 → 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:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9c36878621e6f202f4b5791d431b56afcbf2d7ae
|
4
|
+
data.tar.gz: 9b88eb9065c3432556ec28c66bc700efc9554eb7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d33e7becfd54ae759ffc1652d70398214ac44c9387c2a3c090bef58ff60a898fdf89464c2aea04217bcb230ed02d2092d8c5e456bc9bf26151e92fb8291d031f
|
7
|
+
data.tar.gz: 14c9d8f575203fb2ef060b280eebea241e4e696a14801efcf275978e2365fe0404e9a24ff85dc68c0c8438d7141266619cc02ac59f0b55dcac3de6b753a37049
|
data/lib/jsonapi_spec_helpers.rb
CHANGED
@@ -3,6 +3,7 @@ require 'jsonapi_spec_helpers/version'
|
|
3
3
|
require 'jsonapi_spec_helpers/matchers'
|
4
4
|
require 'jsonapi_spec_helpers/helpers'
|
5
5
|
require 'jsonapi_spec_helpers/payload'
|
6
|
+
require 'jsonapi_spec_helpers/payload_sanitizer'
|
6
7
|
|
7
8
|
module JsonapiSpecHelpers
|
8
9
|
def self.included(klass)
|
@@ -54,5 +54,22 @@ module JsonapiSpecHelpers
|
|
54
54
|
ids
|
55
55
|
end
|
56
56
|
|
57
|
+
def jsonapi_headers
|
58
|
+
{
|
59
|
+
'CONTENT_TYPE' => 'application/vnd.api+json'
|
60
|
+
}
|
61
|
+
end
|
62
|
+
|
63
|
+
def jsonapi_post(url, payload)
|
64
|
+
post url, params: payload.to_json, headers: jsonapi_headers
|
65
|
+
end
|
66
|
+
|
67
|
+
def jsonapi_put(url, payload)
|
68
|
+
put url, params: payload.to_json, headers: jsonapi_headers
|
69
|
+
end
|
70
|
+
|
71
|
+
def jsonapi_payload(input)
|
72
|
+
PayloadSanitizer.new(input).sanitize
|
73
|
+
end
|
57
74
|
end
|
58
75
|
end
|
@@ -0,0 +1,93 @@
|
|
1
|
+
module JsonapiSpecHelpers
|
2
|
+
class PayloadSanitizer
|
3
|
+
def initialize(payload)
|
4
|
+
@payload = payload
|
5
|
+
@included = []
|
6
|
+
end
|
7
|
+
|
8
|
+
def resource
|
9
|
+
@resource ||= sane_resource(@payload)
|
10
|
+
end
|
11
|
+
|
12
|
+
def sanitize
|
13
|
+
resource[:relationships].each_pair do |key, relationship_payload|
|
14
|
+
set_default_relationship_payload(key, relationship_payload)
|
15
|
+
|
16
|
+
if relationship_payload.is_a?(Array)
|
17
|
+
relationship_payload.each do |p|
|
18
|
+
process_relationship(key, p)
|
19
|
+
end
|
20
|
+
else
|
21
|
+
process_relationship(key, relationship_payload)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
payload = { data: data }
|
26
|
+
payload[:included] = @included
|
27
|
+
payload
|
28
|
+
end
|
29
|
+
|
30
|
+
def included
|
31
|
+
@included
|
32
|
+
end
|
33
|
+
|
34
|
+
def add_include(incl)
|
35
|
+
@included.push(incl) if @included.index(incl).nil?
|
36
|
+
end
|
37
|
+
|
38
|
+
def resource_identifier
|
39
|
+
{}.tap do |ri|
|
40
|
+
ri[:id] = resource[:id] if resource[:id]
|
41
|
+
ri[:type] = resource[:type]
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def data
|
46
|
+
@data ||= {}.tap do |d|
|
47
|
+
d[:id] = resource[:id] if resource[:id]
|
48
|
+
d[:type] = resource[:type]
|
49
|
+
d[:attributes] = resource[:attributes]
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
private
|
54
|
+
|
55
|
+
def set_default_relationship_payload(key, payload)
|
56
|
+
data[:relationships] ||= {}
|
57
|
+
if payload.is_a?(Array)
|
58
|
+
data[:relationships][key] ||= { data: [] }
|
59
|
+
else
|
60
|
+
data[:relationships][key] ||= { data: nil }
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def process_relationship(name, relationship_payload)
|
65
|
+
sanitizer = self.class.new(relationship_payload)
|
66
|
+
|
67
|
+
if data[:relationships][name][:data].is_a?(Array)
|
68
|
+
data[:relationships][name][:data] << sanitizer.resource_identifier
|
69
|
+
else
|
70
|
+
data[:relationships][name] = { data: sanitizer.resource_identifier }
|
71
|
+
end
|
72
|
+
|
73
|
+
sanitized = sanitizer.sanitize
|
74
|
+
add_include(sanitized[:data])
|
75
|
+
sanitized[:included].each do |incl|
|
76
|
+
add_include(incl)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
def sane_resource(payload)
|
81
|
+
id, type = payload.delete(:id), payload.delete(:type)
|
82
|
+
relationships = payload.delete(:relationships) || {}
|
83
|
+
raise 'jsonapi payloads must specify a "type"' if type.nil?
|
84
|
+
|
85
|
+
{
|
86
|
+
id: id,
|
87
|
+
type: type,
|
88
|
+
attributes: payload,
|
89
|
+
relationships: relationships
|
90
|
+
}
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jsonapi_spec_helpers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Lee Richmond
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-05-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: pry
|
@@ -116,6 +116,7 @@ files:
|
|
116
116
|
- lib/jsonapi_spec_helpers/helpers.rb
|
117
117
|
- lib/jsonapi_spec_helpers/matchers.rb
|
118
118
|
- lib/jsonapi_spec_helpers/payload.rb
|
119
|
+
- lib/jsonapi_spec_helpers/payload_sanitizer.rb
|
119
120
|
- lib/jsonapi_spec_helpers/version.rb
|
120
121
|
homepage:
|
121
122
|
licenses:
|
@@ -137,7 +138,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
137
138
|
version: '0'
|
138
139
|
requirements: []
|
139
140
|
rubyforge_project:
|
140
|
-
rubygems_version: 2.6.
|
141
|
+
rubygems_version: 2.6.11
|
141
142
|
signing_key:
|
142
143
|
specification_version: 4
|
143
144
|
summary: Spec helpers for jsonapi
|