skalka 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c70c936569d44cb79fe41c6ad86d2c4a9c43bfb787b79e285c83585e425ea7f1
4
- data.tar.gz: a4449fd3ecccce7d9e20fc7e23cb91e4f85771ae9ab390bed9fcf9af36d117e2
3
+ metadata.gz: 127fb261be8fd450102d76ac6e76d0a5080d7273b89fdae633f05d8c318e4873
4
+ data.tar.gz: 6902e5e67bdf3a3528c77ed26fbcbea0db1d0d2e6689d496f3ac89b261eadde0
5
5
  SHA512:
6
- metadata.gz: 115ca2f626cc8b10c918118a5dd05fe966f529ef87336593d5cf7be3877131c5d130847a97c2294f339315c77ba96d78e06e73811f2d8c6f3e5759e3f8d1209b
7
- data.tar.gz: 5492c631a829f42c170dd2c99bf3d345e93d0164ded21c881b197894149e8e13f8f2007ad9eba4874a52586978372d83eba0133235e6da9ebe5970be4c852224
6
+ metadata.gz: 949b512b5c35a3e81b7212c9db87d22711f3fe8a2e29e41193a916730d5dc3b627742e880215f1737bd140a427a630e536d3a3df6fd0419d8fdfb75b38e2a6ef
7
+ data.tar.gz: 36e38e01276d3d502d73540d498edb9a2a44e135b850f707017c700e6463a90194dc71d89c6ccf65ec6b98e0c0f734bfb7049201537d5d40d2da88a989cd2f26
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ before_install: gem install bundler
3
+ script:
4
+ - bundle exec rake spec
5
+ rvm:
6
+ - 2.5.1
data/Gemfile CHANGED
@@ -5,4 +5,5 @@ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
5
5
  # Specify your gem's dependencies in skalka.gemspec
6
6
  gemspec
7
7
 
8
- gem "pry"
8
+ gem "pry", require: false
9
+ gem "simplecov", require: false
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", title: "Wise Thought",
75
- body: "You can't blame gravity for falling in love.",
76
- created_at: "2015-05-22T14:56:29.000Z",
77
- updated_at: "2015-05-22T14:56:28.000Z",
78
- author: [{ id: "42", name: "Albert Einstein", age: 76, gender: "male" }],
79
- comments: [{ id: "55", content: "Meh" }]
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
- meta: { page: 1, count: 1 }
99
+ meta: { page: "1", count: "1" }
82
100
  }
83
101
  ```
84
102
 
@@ -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
- **Functions[:deattribute][item],
12
- **attributes(item)
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, fetch_and_process_data]
19
+ Functions[:map_values, fetch_and_reject_type]
20
20
  )[item]
21
21
  end
22
22
 
23
- private def fetch_and_process_data
24
- Functions[:fetch_data] >> Functions[:map_or_pass][reject_type_and_convert_id]
25
- end
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
@@ -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
- def deattribute_with_nested(item)
15
- {
16
- **self[:deattribute][item],
17
- **NestedResource[:build][item]
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 fetch_link_func(included)
26
- Functions[:fetch_data] >> Functions[:map_or_pass][
27
- self[:find_resource].with(included) >> NestedResource[:build]
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, fetch_link_func(included)]
37
+ Functions[:map_values, fetch_link(included)]
41
38
  )[item]
42
39
  end
43
40
  end
@@ -1,3 +1,3 @@
1
1
  module Skalka
2
- VERSION = "0.3.0".freeze
2
+ VERSION = "0.4.0".freeze
3
3
  end
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.3.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-04-25 00:00:00.000000000 Z
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