skalka 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: c0e0999bc447eda62f39296a95be5f86a7fa7d99950c9f5849f87cb3431ea101
4
+ data.tar.gz: 1a0d4e2a0d0fa1d06d754a5d969b35a07c3d2b069a5dc88fa48799a7ae8a79d4
5
+ SHA512:
6
+ metadata.gz: 7271be51fee6c0e7d4d1e63bc30dab27b6f5f215ff66b67dbb822792d9feb9563c03466ad2079552c0e9881f8d9791cd53c2002662426cca6c363b4847e6543d
7
+ data.tar.gz: 3f0449c26566a12e463032538c77aed9d516b058f6eddbed6c7a2539a440627691c738418cb4bd3f7ffb16f595c3a27cb4e88cc138c6c3c43f97724c8c3d34c0
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,64 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.5
3
+ DisplayCopNames: true
4
+
5
+ RSpec/MultipleExpectations:
6
+ Enabled: false
7
+
8
+ RSpec/ExampleLength:
9
+ Enabled: false
10
+
11
+ RSpec/RepeatedDescription:
12
+ Enabled: false
13
+
14
+ RSpec/MessageSpies:
15
+ EnforcedStyle: receive
16
+
17
+ Style/AndOr:
18
+ Enabled: false
19
+
20
+ Style/Documentation:
21
+ Enabled: false
22
+
23
+ Style/MethodCalledOnDoEndBlock:
24
+ Enabled: true
25
+
26
+ Style/CollectionMethods:
27
+ Enabled: true
28
+
29
+ Style/SymbolArray:
30
+ Enabled: true
31
+
32
+ Style/StringLiterals:
33
+ EnforcedStyle: double_quotes
34
+ ConsistentQuotesInMultiline: true
35
+
36
+ Style/EmptyMethod:
37
+ EnforcedStyle: expanded
38
+ SupportedStyles:
39
+ - compact
40
+ - expanded
41
+
42
+ Style/FrozenStringLiteralComment:
43
+ Enabled: false
44
+
45
+ Style/StringMethods:
46
+ Enabled: true
47
+
48
+ Metrics/LineLength:
49
+ Max: 120
50
+
51
+ Metrics/BlockLength:
52
+ Enabled: false
53
+
54
+ Layout/AlignParameters:
55
+ EnforcedStyle: with_fixed_indentation
56
+ SupportedStyles:
57
+ - with_first_parameter
58
+ - with_fixed_indentation
59
+
60
+ Lint/EndAlignment:
61
+ EnforcedStyleAlignWith: variable
62
+ SupportedStylesAlignWith:
63
+ - keyword
64
+ - variable
data/.tool-versions ADDED
@@ -0,0 +1 @@
1
+ ruby 2.5.0
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in skalka.gemspec
6
+ gemspec
7
+
8
+ gem "pry"
data/Gemfile.lock ADDED
@@ -0,0 +1,43 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ skalka (0.1.0)
5
+ transproc
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ coderay (1.1.2)
11
+ diff-lcs (1.3)
12
+ method_source (0.9.0)
13
+ pry (0.11.3)
14
+ coderay (~> 1.1.0)
15
+ method_source (~> 0.9.0)
16
+ rake (10.5.0)
17
+ rspec (3.7.0)
18
+ rspec-core (~> 3.7.0)
19
+ rspec-expectations (~> 3.7.0)
20
+ rspec-mocks (~> 3.7.0)
21
+ rspec-core (3.7.1)
22
+ rspec-support (~> 3.7.0)
23
+ rspec-expectations (3.7.0)
24
+ diff-lcs (>= 1.2.0, < 2.0)
25
+ rspec-support (~> 3.7.0)
26
+ rspec-mocks (3.7.0)
27
+ diff-lcs (>= 1.2.0, < 2.0)
28
+ rspec-support (~> 3.7.0)
29
+ rspec-support (3.7.1)
30
+ transproc (1.0.2)
31
+
32
+ PLATFORMS
33
+ ruby
34
+
35
+ DEPENDENCIES
36
+ bundler (~> 1.16)
37
+ pry
38
+ rake (~> 10.0)
39
+ rspec (~> 3.0)
40
+ skalka!
41
+
42
+ BUNDLED WITH
43
+ 1.16.1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Emil Shakirov
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,92 @@
1
+ # Skalka
2
+
3
+ ![](https://i.imgur.com/rsw7QPg.png)
4
+
5
+ ### Skalka — the rolling pin for your json api responses.
6
+
7
+ ## Installation
8
+
9
+ Add to Gemfile
10
+
11
+ ```ruby
12
+ gem "skalka"
13
+ ```
14
+ Run bundle
15
+
16
+ ```bash
17
+ bundle install
18
+ ```
19
+
20
+ ## Usage
21
+
22
+ ```ruby
23
+ require "skalka"
24
+
25
+ json = %{
26
+ {
27
+ "data": [{
28
+ "type": "articles",
29
+ "id": "1",
30
+ "attributes": {
31
+ "title": "Wise Thought",
32
+ "body": "You can't blame gravity for falling in love.",
33
+ "created_at": "2015-05-22T14:56:29.000Z",
34
+ "updated_at": "2015-05-22T14:56:28.000Z"
35
+ },
36
+ "relationships": {
37
+ "author": {
38
+ "data": { "id": "42", "type": "people" }
39
+ },
40
+ "comments": {
41
+ "data": { "id": "55", "type": "comments" }
42
+ }
43
+ }
44
+ }],
45
+ "included": [
46
+ {
47
+ "type": "people",
48
+ "id": "42",
49
+ "attributes": {
50
+ "name": "Albert Einstein",
51
+ "age": 76,
52
+ "gender": "male"
53
+ }
54
+ },
55
+ {
56
+ "type": "comments",
57
+ "id": "55",
58
+ "attributes": {
59
+ "content": "Meh"
60
+ }
61
+ }
62
+ ],
63
+ "meta": {
64
+ "page": 1,
65
+ "count": 1
66
+ }
67
+ }
68
+ }
69
+
70
+ Skalka.call(json)
71
+
72
+ {
73
+ 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" }]
80
+ },
81
+ meta: { page: 1, count: 1 }
82
+ }
83
+ ```
84
+
85
+ ## Ruby support
86
+
87
+ - 2.5.0
88
+
89
+
90
+ ## Credits
91
+
92
+ Skalka is built with [transproc](https://github.com/solnic/transproc) under the hood and shares its FP spirit.
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
@@ -0,0 +1,44 @@
1
+ require "json"
2
+ require "transproc"
3
+ require "transproc/conditional"
4
+
5
+ module Skalka
6
+ module Functions
7
+ EXTRA_FIELDS = %i[errors links meta].freeze
8
+
9
+ extend Transproc::Registry
10
+
11
+ import Transproc::ArrayTransformations
12
+ import Transproc::Conditional
13
+ import Transproc::HashTransformations
14
+
15
+ import :parse, from: JSON, as: :parse_json
16
+
17
+ module_function
18
+
19
+ def fetch_data(item)
20
+ item.fetch(:data, {})
21
+ end
22
+
23
+ def flat_wrap(data)
24
+ [data].flatten
25
+ end
26
+
27
+ def extra_fields(parsed_json)
28
+ parsed_json.slice(*EXTRA_FIELDS)
29
+ end
30
+
31
+ def extract(parsed_json)
32
+ (
33
+ self[:fetch_data] >>
34
+ self[:flat_wrap] >>
35
+ self[:map_array, Resource[:build_resource].with(parsed_json)] >>
36
+ self[:unwrap]
37
+ ).call(parsed_json)
38
+ end
39
+
40
+ def unwrap(list)
41
+ Functions[:guard, ->(l) { l.one? }, ->(l) { l.first }][list]
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,61 @@
1
+ module Skalka
2
+ module Resource
3
+ extend Transproc::Registry
4
+
5
+ module_function
6
+
7
+ def build_resource(item, parsed_json)
8
+ raw_relationships = item.fetch(:relationships, {})
9
+ included = parsed_json.fetch(:included, [])
10
+
11
+ {
12
+ **self[:deattribute][item],
13
+ **relationships(included, raw_relationships)
14
+ }
15
+ end
16
+
17
+ def deattribute(item)
18
+ {
19
+ id: item[:id].to_i,
20
+ **item[:attributes]
21
+ }
22
+ end
23
+
24
+ def deattribute_with_nested(item)
25
+ {
26
+ **deattribute(item),
27
+ **nested_relationships(item.fetch(:relationships, {}))
28
+ }
29
+ end
30
+
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]
36
+ end
37
+
38
+ def find_resource(relationship, included)
39
+ included.find do |included_resource|
40
+ included_resource.values_at(:id, :type) == relationship.values_at(:id, :type)
41
+ end
42
+ end
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]
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,3 @@
1
+ module Skalka
2
+ VERSION = "0.1.0".freeze
3
+ end
data/lib/skalka.rb ADDED
@@ -0,0 +1,23 @@
1
+ require "skalka/functions"
2
+ require "skalka/resource"
3
+ require "skalka/version"
4
+
5
+ module Skalka
6
+ autoload :Functions, "skalka/functions"
7
+ autoload :Resource, "skalka/resource"
8
+
9
+ module_function
10
+
11
+ def call(json)
12
+ parsed_json = parse_json(json)
13
+
14
+ {
15
+ data: Functions[:extract][parsed_json],
16
+ **Functions[:extra_fields][parsed_json]
17
+ }
18
+ end
19
+
20
+ private def parse_json(json)
21
+ (Functions[:parse_json] >> Functions[:deep_symbolize_keys])[json]
22
+ end
23
+ end
data/skalka.gemspec ADDED
@@ -0,0 +1,28 @@
1
+ lib = File.expand_path("../lib", __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "skalka/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "skalka"
7
+ spec.version = Skalka::VERSION
8
+ spec.authors = ["Emil Shakirov"]
9
+ spec.email = ["me@emil.sh"]
10
+
11
+ spec.summary = "The rolling pin for your json api responses."
12
+ spec.description = "Simple JSON API parser/flattener"
13
+ spec.homepage = "https://github.com/vaihtovirta/skalka.git"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.16"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "rspec", "~> 3.0"
26
+
27
+ spec.add_dependency "transproc"
28
+ end
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: skalka
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Emil Shakirov
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-03-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: transproc
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Simple JSON API parser/flattener
70
+ email:
71
+ - me@emil.sh
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".rubocop.yml"
79
+ - ".tool-versions"
80
+ - Gemfile
81
+ - Gemfile.lock
82
+ - LICENSE.txt
83
+ - README.md
84
+ - Rakefile
85
+ - lib/skalka.rb
86
+ - lib/skalka/functions.rb
87
+ - lib/skalka/resource.rb
88
+ - lib/skalka/version.rb
89
+ - skalka.gemspec
90
+ homepage: https://github.com/vaihtovirta/skalka.git
91
+ licenses:
92
+ - MIT
93
+ metadata: {}
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ requirements: []
109
+ rubyforge_project:
110
+ rubygems_version: 2.7.6
111
+ signing_key:
112
+ specification_version: 4
113
+ summary: The rolling pin for your json api responses.
114
+ test_files: []