json_api_test_helpers 1.0.1 → 1.1
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
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 453f2d1cabd8d90513c4249388b3d2280d05bd8648d669d3f330685c7825bead
|
4
|
+
data.tar.gz: 6bb89648163de76a82e4e0ea496c4ef71df7a595aec82239aca744172fb0f763
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b46bad2d8208984f79ebe977563097ccf68121b646d9a4783427e86933566f8714dd840a50e4f7864705b684f2c48699c3ea0109d5642062494713222872c9d1
|
7
|
+
data.tar.gz: 142399cd11e7dae1ab9ed9364a5a6609f7d8e68658455f037b5e032fd17ef7a5bffebd283a7bb163a85928e8ab9a5f5fdc7cfa32fc4121da7cdb4489f1ead778
|
@@ -0,0 +1,100 @@
|
|
1
|
+
module JsonApiTestHelpers
|
2
|
+
module Response
|
3
|
+
def json_response
|
4
|
+
if response.body.present?
|
5
|
+
parsed_json = JSON.parse response.body
|
6
|
+
parsed_json.with_indifferent_access unless parsed_json.is_a? Array
|
7
|
+
else
|
8
|
+
raise 'Response body is empty'
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def json_api_record(record, attributes, relationships: nil, additional: nil)
|
13
|
+
json = {
|
14
|
+
'data' => {
|
15
|
+
'id' => record.id.to_s,
|
16
|
+
'attributes' => json_api_record_attributes(record, attributes)
|
17
|
+
}
|
18
|
+
}
|
19
|
+
json['data']['relationships'] = json_api_relationships(record, relationships) if relationships
|
20
|
+
json
|
21
|
+
end
|
22
|
+
|
23
|
+
def json_api_collection(collection, attributes = nil, relationships: nil)
|
24
|
+
{
|
25
|
+
'data' => collection.map do |record|
|
26
|
+
hash = {
|
27
|
+
'id' => record.id.to_s
|
28
|
+
}
|
29
|
+
hash['attributes'] = json_api_record_attributes(record, attributes) if attributes
|
30
|
+
hash['relationships'] = json_api_relationships(record, relationships) if relationships
|
31
|
+
hash
|
32
|
+
end
|
33
|
+
}
|
34
|
+
end
|
35
|
+
|
36
|
+
def fix_value_for_json(value)
|
37
|
+
return value.iso8601 if value.class.in? [DateTime, ActiveSupport::TimeWithZone]
|
38
|
+
return value if value.is_a? Integer
|
39
|
+
return value.serializable_hash if value.is_a? CarrierWave::Uploader::Serialization
|
40
|
+
return value.reduce({}) do |hash, k_v|
|
41
|
+
if k_v[1].is_a? Hash
|
42
|
+
k_v[1] = k_v[1].reduce({}) do |value_hash, value_k_v|
|
43
|
+
value_hash.merge! value_k_v[0].gsub('_', '-') => value_k_v[1]
|
44
|
+
end
|
45
|
+
end
|
46
|
+
hash.merge! k_v[0].gsub('_', '-') => k_v[1]
|
47
|
+
end if value.is_a? Hash
|
48
|
+
value.to_s
|
49
|
+
end
|
50
|
+
|
51
|
+
def fix_comparing_types(value)
|
52
|
+
if value.class.in? [DateTime, ActiveSupport::TimeWithZone]
|
53
|
+
value.to_datetime.utc.to_s
|
54
|
+
elsif value.class == ActiveRecord::Point
|
55
|
+
"#{value.x}, #{value.y}"
|
56
|
+
else
|
57
|
+
value
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def attributes_for_nested(attributes, **associations)
|
62
|
+
associations.reduce(attributes) do |attr_hash, collection|
|
63
|
+
attr_hash.merge! "#{collection[0]}_attributes" => (collection[1].each_with_index.reduce({}) do |hash, item|
|
64
|
+
hash.merge! item.last.to_s => item.first
|
65
|
+
end)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
private
|
70
|
+
|
71
|
+
def json_api_record_attributes(record, attributes)
|
72
|
+
attributes.reduce({}) do |hash, attr|
|
73
|
+
hash.merge! attr[0].to_s.tr('_', '-') => fix_value_for_json(record.send(attr[0]))
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
def json_api_relationships(record, relationships)
|
78
|
+
relationships.reduce({}) do |hash, relationship|
|
79
|
+
object = record.send relationship
|
80
|
+
data = if object.is_a?(ActiveRecord::Associations::CollectionProxy)
|
81
|
+
object.reduce([]) { |result, item| result << data_relationship_object(item, relationship) }
|
82
|
+
else
|
83
|
+
object.present? ? data_relationship_object(object, relationship) : {}
|
84
|
+
end
|
85
|
+
hash.merge! json_api_model_name(relationship) => { 'data' => data }
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
def json_api_model_name(model_name)
|
90
|
+
model_name.to_s.tr '_', '-'
|
91
|
+
end
|
92
|
+
|
93
|
+
def data_relationship_object(object, relationship)
|
94
|
+
{
|
95
|
+
'id' => object.id.to_s,
|
96
|
+
'type' => json_api_model_name(relationship).pluralize
|
97
|
+
}
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
@@ -1,100 +1,8 @@
|
|
1
1
|
require 'json_api_test_helpers/version'
|
2
|
+
require 'json_api_test_helpers/params'
|
3
|
+
require 'json_api_test_helpers/response'
|
2
4
|
|
3
5
|
module JsonApiTestHelpers
|
4
|
-
|
5
|
-
|
6
|
-
parsed_json = JSON.parse response.body
|
7
|
-
parsed_json.with_indifferent_access unless parsed_json.is_a? Array
|
8
|
-
else
|
9
|
-
raise 'Response body is empty'
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
def json_api_record(record, attributes, relationships: nil, additional: nil)
|
14
|
-
json = {
|
15
|
-
'data' => {
|
16
|
-
'id' => record.id.to_s,
|
17
|
-
'attributes' => json_api_record_attributes(record, attributes)
|
18
|
-
}
|
19
|
-
}
|
20
|
-
json['data']['relationships'] = json_api_relationships(record, relationships) if relationships
|
21
|
-
json
|
22
|
-
end
|
23
|
-
|
24
|
-
def json_api_collection(collection, attributes = nil, relationships: nil)
|
25
|
-
{
|
26
|
-
'data' => collection.map do |record|
|
27
|
-
hash = {
|
28
|
-
'id' => record.id.to_s
|
29
|
-
}
|
30
|
-
hash['attributes'] = json_api_record_attributes(record, attributes) if attributes
|
31
|
-
hash['relationships'] = json_api_relationships(record, relationships) if relationships
|
32
|
-
hash
|
33
|
-
end
|
34
|
-
}
|
35
|
-
end
|
36
|
-
|
37
|
-
def fix_value_for_json(value)
|
38
|
-
return value.iso8601 if value.class.in? [DateTime, ActiveSupport::TimeWithZone]
|
39
|
-
return value if value.is_a? Integer
|
40
|
-
return value.serializable_hash if value.is_a? CarrierWave::Uploader::Serialization
|
41
|
-
return value.reduce({}) do |hash, k_v|
|
42
|
-
if k_v[1].is_a? Hash
|
43
|
-
k_v[1] = k_v[1].reduce({}) do |value_hash, value_k_v|
|
44
|
-
value_hash.merge! value_k_v[0].gsub('_', '-') => value_k_v[1]
|
45
|
-
end
|
46
|
-
end
|
47
|
-
hash.merge! k_v[0].gsub('_', '-') => k_v[1]
|
48
|
-
end if value.is_a? Hash
|
49
|
-
value.to_s
|
50
|
-
end
|
51
|
-
|
52
|
-
def fix_comparing_types(value)
|
53
|
-
if value.class.in? [DateTime, ActiveSupport::TimeWithZone]
|
54
|
-
value.to_datetime.utc.to_s
|
55
|
-
elsif value.class == ActiveRecord::Point
|
56
|
-
"#{value.x}, #{value.y}"
|
57
|
-
else
|
58
|
-
value
|
59
|
-
end
|
60
|
-
end
|
61
|
-
|
62
|
-
def attributes_for_nested(attributes, **associations)
|
63
|
-
associations.reduce(attributes) do |attr_hash, collection|
|
64
|
-
attr_hash.merge! "#{collection[0]}_attributes" => (collection[1].each_with_index.reduce({}) do |hash, item|
|
65
|
-
hash.merge! item.last.to_s => item.first
|
66
|
-
end)
|
67
|
-
end
|
68
|
-
end
|
69
|
-
|
70
|
-
private
|
71
|
-
|
72
|
-
def json_api_record_attributes(record, attributes)
|
73
|
-
attributes.reduce({}) do |hash, attr|
|
74
|
-
hash.merge! attr[0].to_s.tr('_', '-') => fix_value_for_json(record.send(attr[0]))
|
75
|
-
end
|
76
|
-
end
|
77
|
-
|
78
|
-
def json_api_relationships(record, relationships)
|
79
|
-
relationships.reduce({}) do |hash, relationship|
|
80
|
-
object = record.send relationship
|
81
|
-
data = if object.is_a?(ActiveRecord::Associations::CollectionProxy)
|
82
|
-
object.reduce([]) { |result, item| result << data_relationship_object(item, relationship) }
|
83
|
-
else
|
84
|
-
object.present? ? data_relationship_object(object, relationship) : {}
|
85
|
-
end
|
86
|
-
hash.merge! json_api_model_name(relationship) => { 'data' => data }
|
87
|
-
end
|
88
|
-
end
|
89
|
-
|
90
|
-
def json_api_model_name(model_name)
|
91
|
-
model_name.to_s.tr '_', '-'
|
92
|
-
end
|
93
|
-
|
94
|
-
def data_relationship_object(object, relationship)
|
95
|
-
{
|
96
|
-
'id' => object.id.to_s,
|
97
|
-
'type' => json_api_model_name(relationship).pluralize
|
98
|
-
}
|
99
|
-
end
|
6
|
+
include Params
|
7
|
+
include Response
|
100
8
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: json_api_test_helpers
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: '1.1'
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Pavel Kalashnikov
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-07-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -71,6 +71,8 @@ files:
|
|
71
71
|
- bin/setup
|
72
72
|
- json_api_test_helpers.gemspec
|
73
73
|
- lib/json_api_test_helpers.rb
|
74
|
+
- lib/json_api_test_helpers/params.rb
|
75
|
+
- lib/json_api_test_helpers/response.rb
|
74
76
|
- lib/json_api_test_helpers/version.rb
|
75
77
|
homepage: https://github.com/kalashnikovisme/json_api_test_helpers
|
76
78
|
licenses:
|
@@ -92,7 +94,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
92
94
|
version: '0'
|
93
95
|
requirements: []
|
94
96
|
rubyforge_project:
|
95
|
-
rubygems_version: 2.7.
|
97
|
+
rubygems_version: 2.7.6
|
96
98
|
signing_key:
|
97
99
|
specification_version: 4
|
98
100
|
summary: JSON API helpers for testing
|