skalka 0.1.0 → 0.2.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
  SHA256:
3
- metadata.gz: c0e0999bc447eda62f39296a95be5f86a7fa7d99950c9f5849f87cb3431ea101
4
- data.tar.gz: 1a0d4e2a0d0fa1d06d754a5d969b35a07c3d2b069a5dc88fa48799a7ae8a79d4
3
+ metadata.gz: 5719ceadec96d873ec1440d99a5d5c5780aaef255e2b035830e74540d25d9bcc
4
+ data.tar.gz: b85ccbb1645e24dcfd4e3a36c9fa02a56e717f8863d46471550bb4731be5263a
5
5
  SHA512:
6
- metadata.gz: 7271be51fee6c0e7d4d1e63bc30dab27b6f5f215ff66b67dbb822792d9feb9563c03466ad2079552c0e9881f8d9791cd53c2002662426cca6c363b4847e6543d
7
- data.tar.gz: 3f0449c26566a12e463032538c77aed9d516b058f6eddbed6c7a2539a440627691c738418cb4bd3f7ffb16f595c3a27cb4e88cc138c6c3c43f97724c8c3d34c0
6
+ metadata.gz: b7561d80483359b08e8054096900bc90bef5e68e582bcb19fb94d80739dcc7c49296482e7de16b164b656bf72de4b7712de32badf772a964fc8b1d225926feca
7
+ data.tar.gz: 00a13e0657fb7ae1cdc6333b65f8d5051110b63d483dc558d550444419df42615e27c05e7fcd6a3195e33b6fcdc75b4ac7bb9b3398f20caf6554e020e3a63ed8
@@ -20,25 +20,25 @@ module Skalka
20
20
  item.fetch(:data, {})
21
21
  end
22
22
 
23
- def flat_wrap(data)
24
- [data].flatten
25
- end
26
-
27
23
  def extra_fields(parsed_json)
28
24
  parsed_json.slice(*EXTRA_FIELDS)
29
25
  end
30
26
 
31
- def extract(parsed_json)
27
+ def extract_resource(parsed_json)
28
+ included = parsed_json.fetch(:included, [])
29
+
32
30
  (
33
31
  self[:fetch_data] >>
34
- self[:flat_wrap] >>
35
- self[:map_array, Resource[:build_resource].with(parsed_json)] >>
36
- self[:unwrap]
32
+ self[:map_or_pass][ Resource[:build].with(included) ]
37
33
  ).call(parsed_json)
38
34
  end
39
35
 
40
- def unwrap(list)
41
- Functions[:guard, ->(l) { l.one? }, ->(l) { l.first }][list]
36
+ def map_or_pass(func)
37
+ self[:is, Array, self[:map_array, func]] >> self[:is, Hash, func]
38
+ end
39
+
40
+ def parse_and_symbolize_keys(json)
41
+ (self[:parse_json] >> self[:deep_symbolize_keys]).call(json)
42
42
  end
43
43
  end
44
44
  end
@@ -0,0 +1,22 @@
1
+ module Skalka
2
+ module NestedResource
3
+ extend Transproc::Registry
4
+
5
+ module_function
6
+
7
+ def build(item)
8
+ (
9
+ Resource[:fetch_relationships] >>
10
+ Functions[:map_values, fetch_and_process_data]
11
+ )[item]
12
+ end
13
+
14
+ private def fetch_and_process_data
15
+ Functions[:fetch_data] >> Functions[:map_or_pass][reject_type_and_convert_id]
16
+ end
17
+
18
+ private def reject_type_and_convert_id
19
+ Functions[:reject_keys, [:type]] >> Functions[:map_value, :id, ->(id) { id.to_i }]
20
+ end
21
+ end
22
+ end
@@ -4,13 +4,10 @@ module Skalka
4
4
 
5
5
  module_function
6
6
 
7
- def build_resource(item, parsed_json)
8
- raw_relationships = item.fetch(:relationships, {})
9
- included = parsed_json.fetch(:included, [])
10
-
7
+ def build(item, included)
11
8
  {
12
9
  **self[:deattribute][item],
13
- **relationships(included, raw_relationships)
10
+ **self[:relationships].with(included)[item]
14
11
  }
15
12
  end
16
13
 
@@ -23,16 +20,19 @@ module Skalka
23
20
 
24
21
  def deattribute_with_nested(item)
25
22
  {
26
- **deattribute(item),
27
- **nested_relationships(item.fetch(:relationships, {}))
23
+ **self[:deattribute][item],
24
+ **NestedResource[:build][item]
28
25
  }
29
26
  end
30
27
 
31
- def fetch_link(included)
32
- Functions[:fetch_data] >>
33
- Functions[:flat_wrap] >>
34
- Functions[:map_array, self[:find_resource].with(included) >> self[:deattribute_with_nested]] >>
35
- Functions[:unwrap]
28
+ def fetch_relationships(item)
29
+ item.fetch(:relationships, {})
30
+ end
31
+
32
+ def fetch_link_func(included)
33
+ Functions[:fetch_data] >> Functions[:map_or_pass][
34
+ self[:find_resource].with(included) >> self[:deattribute_with_nested]
35
+ ]
36
36
  end
37
37
 
38
38
  def find_resource(relationship, included)
@@ -41,21 +41,11 @@ module Skalka
41
41
  end
42
42
  end
43
43
 
44
- def nested_relationships(relationships)
45
- Functions[
46
- :map_values,
47
- Functions[:fetch_data] >>
48
- Functions[:flat_wrap] >>
49
- Functions[
50
- :map_array,
51
- Functions[:accept_keys, [:id]] >>
52
- Functions[:map_value, :id, ->(id) { id.to_i }]
53
- ]
54
- ][relationships]
55
- end
56
-
57
- def relationships(included, raw_relationships)
58
- Functions[:map_values, fetch_link(included)][raw_relationships]
44
+ def relationships(item, included)
45
+ (
46
+ self[:fetch_relationships] >>
47
+ Functions[:map_values, fetch_link_func(included)]
48
+ )[item]
59
49
  end
60
50
  end
61
51
  end
@@ -1,3 +1,3 @@
1
1
  module Skalka
2
- VERSION = "0.1.0".freeze
2
+ VERSION = "0.2.0".freeze
3
3
  end
data/lib/skalka.rb CHANGED
@@ -1,23 +1,21 @@
1
1
  require "skalka/functions"
2
2
  require "skalka/resource"
3
+ require "skalka/nested_resource"
3
4
  require "skalka/version"
4
5
 
5
6
  module Skalka
6
7
  autoload :Functions, "skalka/functions"
7
8
  autoload :Resource, "skalka/resource"
9
+ autoload :NestedResource, "skalka/nested_resource"
8
10
 
9
11
  module_function
10
12
 
11
13
  def call(json)
12
- parsed_json = parse_json(json)
14
+ parsed_json = Functions[:parse_and_symbolize_keys][json]
13
15
 
14
16
  {
15
- data: Functions[:extract][parsed_json],
17
+ data: Functions[:extract_resource][parsed_json],
16
18
  **Functions[:extra_fields][parsed_json]
17
19
  }
18
20
  end
19
-
20
- private def parse_json(json)
21
- (Functions[:parse_json] >> Functions[:deep_symbolize_keys])[json]
22
- end
23
21
  end
data/skalka.gemspec CHANGED
@@ -1,4 +1,4 @@
1
- lib = File.expand_path("../lib", __FILE__)
1
+ lib = File.expand_path("lib", __dir__)
2
2
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
3
  require "skalka/version"
4
4
 
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.1.0
4
+ version: 0.2.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-03-30 00:00:00.000000000 Z
11
+ date: 2018-04-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -84,6 +84,7 @@ files:
84
84
  - Rakefile
85
85
  - lib/skalka.rb
86
86
  - lib/skalka/functions.rb
87
+ - lib/skalka/nested_resource.rb
87
88
  - lib/skalka/resource.rb
88
89
  - lib/skalka/version.rb
89
90
  - skalka.gemspec