skalka 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +6 -0
- data/Gemfile +2 -1
- data/README.md +27 -9
- data/lib/skalka/functions.rb +5 -9
- data/lib/skalka/nested_resource.rb +9 -11
- data/lib/skalka/resource.rb +14 -17
- data/lib/skalka/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 127fb261be8fd450102d76ac6e76d0a5080d7273b89fdae633f05d8c318e4873
|
4
|
+
data.tar.gz: 6902e5e67bdf3a3528c77ed26fbcbea0db1d0d2e6689d496f3ac89b261eadde0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 949b512b5c35a3e81b7212c9db87d22711f3fe8a2e29e41193a916730d5dc3b627742e880215f1737bd140a427a630e536d3a3df6fd0419d8fdfb75b38e2a6ef
|
7
|
+
data.tar.gz: 36e38e01276d3d502d73540d498edb9a2a44e135b850f707017c700e6463a90194dc71d89c6ccf65ec6b98e0c0f734bfb7049201537d5d40d2da88a989cd2f26
|
data/.travis.yml
ADDED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
# Skalka
|
2
2
|
|
3
|
+
[![Build Status](https://travis-ci.com/vaihtovirta/skalka.svg?branch=master)](https://travis-ci.com/vaihtovirta/skalka)
|
4
|
+
|
3
5
|
![](https://i.imgur.com/rsw7QPg.png)
|
4
6
|
|
5
7
|
### Skalka — the rolling pin for your json api responses.
|
@@ -61,8 +63,8 @@ json = %{
|
|
61
63
|
}
|
62
64
|
],
|
63
65
|
"meta": {
|
64
|
-
"page": 1,
|
65
|
-
"count": 1
|
66
|
+
"page": "1",
|
67
|
+
"count": "1"
|
66
68
|
}
|
67
69
|
}
|
68
70
|
}
|
@@ -71,14 +73,30 @@ Skalka.call(json)
|
|
71
73
|
|
72
74
|
{
|
73
75
|
data: {
|
74
|
-
id: "1",
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
76
|
+
id: "1",
|
77
|
+
type: "articles",
|
78
|
+
attributes: {
|
79
|
+
title: "Wise Thought",
|
80
|
+
body: "You can't blame gravity for falling in love.",
|
81
|
+
created_at: "2015-05-22T14:56:29.000Z",
|
82
|
+
updated_at: "2015-05-22T14:56:28.000Z",
|
83
|
+
author: {
|
84
|
+
id: "42",
|
85
|
+
type: "authors",
|
86
|
+
attributes: {
|
87
|
+
id: "42", name: "Albert Einstein", age: 76, gender: "male"
|
88
|
+
}
|
89
|
+
},
|
90
|
+
comments: [
|
91
|
+
{
|
92
|
+
id: "55",
|
93
|
+
type: "comments",
|
94
|
+
attributes: { id: "55", content: "Meh" }
|
95
|
+
}
|
96
|
+
]
|
97
|
+
}
|
80
98
|
},
|
81
|
-
|
99
|
+
meta: { page: "1", count: "1" }
|
82
100
|
}
|
83
101
|
```
|
84
102
|
|
data/lib/skalka/functions.rb
CHANGED
@@ -4,6 +4,7 @@ require "transproc/conditional"
|
|
4
4
|
|
5
5
|
module Skalka
|
6
6
|
module Functions
|
7
|
+
MAIN_FIELDS = %i[id type attributes].freeze
|
7
8
|
EXTRA_FIELDS = %i[errors links meta].freeze
|
8
9
|
|
9
10
|
extend Transproc::Registry
|
@@ -16,15 +17,6 @@ module Skalka
|
|
16
17
|
|
17
18
|
module_function
|
18
19
|
|
19
|
-
def deattribute(item)
|
20
|
-
return {} if item.empty?
|
21
|
-
|
22
|
-
{
|
23
|
-
id: item[:id].to_i,
|
24
|
-
**item[:attributes]
|
25
|
-
}
|
26
|
-
end
|
27
|
-
|
28
20
|
def fetch_data(item)
|
29
21
|
item.fetch(:data, {})
|
30
22
|
end
|
@@ -49,5 +41,9 @@ module Skalka
|
|
49
41
|
def parse_and_symbolize_keys(json)
|
50
42
|
(self[:parse_json] >> self[:deep_symbolize_keys]).call(json)
|
51
43
|
end
|
44
|
+
|
45
|
+
def pick_main_attributes(item)
|
46
|
+
item.slice(*MAIN_FIELDS)
|
47
|
+
end
|
52
48
|
end
|
53
49
|
end
|
@@ -7,25 +7,23 @@ module Skalka
|
|
7
7
|
def build(item)
|
8
8
|
return {} if item.empty?
|
9
9
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
10
|
+
Functions[:deep_merge][
|
11
|
+
Functions[:pick_main_attributes][item],
|
12
|
+
attributes: attributes(item)
|
13
|
+
]
|
14
14
|
end
|
15
15
|
|
16
16
|
private def attributes(item)
|
17
17
|
(
|
18
18
|
Resource[:fetch_relationships] >>
|
19
|
-
Functions[:map_values,
|
19
|
+
Functions[:map_values, fetch_and_reject_type]
|
20
20
|
)[item]
|
21
21
|
end
|
22
22
|
|
23
|
-
private def
|
24
|
-
Functions[:fetch_data] >> Functions[:map_or_pass][
|
25
|
-
|
26
|
-
|
27
|
-
private def reject_type_and_convert_id
|
28
|
-
Functions[:reject_keys, [:type]] >> Functions[:map_value, :id, ->(id) { id.to_i }]
|
23
|
+
private def fetch_and_reject_type
|
24
|
+
Functions[:fetch_data] >> Functions[:map_or_pass][
|
25
|
+
Functions[:reject_keys, [:type]]
|
26
|
+
]
|
29
27
|
end
|
30
28
|
end
|
31
29
|
end
|
data/lib/skalka/resource.rb
CHANGED
@@ -5,39 +5,36 @@ module Skalka
|
|
5
5
|
module_function
|
6
6
|
|
7
7
|
def build(item, included)
|
8
|
-
{
|
9
|
-
**Functions[:deattribute][item],
|
10
|
-
**self[:relationships].with(included)[item]
|
11
|
-
}
|
12
|
-
end
|
8
|
+
return {} if item.empty?
|
13
9
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
}
|
10
|
+
Functions[:deep_merge][
|
11
|
+
Functions[:pick_main_attributes][item],
|
12
|
+
attributes: self[:relationships].with(included)[item]
|
13
|
+
]
|
19
14
|
end
|
20
15
|
|
21
16
|
def fetch_relationships(item)
|
22
17
|
item.fetch(:relationships, {})
|
23
18
|
end
|
24
19
|
|
25
|
-
def
|
26
|
-
|
27
|
-
|
28
|
-
|
20
|
+
private def build_resource(included)
|
21
|
+
self[:find_resource].with(included) >> NestedResource[:build]
|
22
|
+
end
|
23
|
+
|
24
|
+
private def fetch_link(included)
|
25
|
+
Functions[:fetch_data] >> Functions[:map_or_pass][build_resource(included)]
|
29
26
|
end
|
30
27
|
|
31
|
-
def find_resource(relationship, included)
|
28
|
+
private def find_resource(relationship, included)
|
32
29
|
included.find do |included_resource|
|
33
30
|
included_resource.values_at(:id, :type) == relationship.values_at(:id, :type)
|
34
31
|
end
|
35
32
|
end
|
36
33
|
|
37
|
-
def relationships(item, included)
|
34
|
+
private def relationships(item, included)
|
38
35
|
(
|
39
36
|
self[:fetch_relationships] >>
|
40
|
-
Functions[:map_values,
|
37
|
+
Functions[:map_values, fetch_link(included)]
|
41
38
|
)[item]
|
42
39
|
end
|
43
40
|
end
|
data/lib/skalka/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: skalka
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Emil Shakirov
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-09-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -76,6 +76,7 @@ files:
|
|
76
76
|
- ".gitignore"
|
77
77
|
- ".rspec"
|
78
78
|
- ".rubocop.yml"
|
79
|
+
- ".travis.yml"
|
79
80
|
- Gemfile
|
80
81
|
- LICENSE.txt
|
81
82
|
- README.md
|