json_api_normalizer 0.1.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: a9cac45030e198ecff9add95ca5a3e4b93ab01cbd54c1fbed7e8c319b724d0f2
4
+ data.tar.gz: 2ea65ec741a8c6d9864963f6136da9bba88ce5f6b28ea31e035540c5178da55a
5
+ SHA512:
6
+ metadata.gz: 8093465de04179c4e81d2bd355b6f78102dc506b5689b4f2322190c7d50044beaff667d66ff7661bdfe8687b0effca29a430532d3de64f3d9b483b33c65077ac
7
+ data.tar.gz: 49efd5b11f969bf2eabab4c3ca808aa36b5206344e5bff19333e5d69ed335cd6858a1660143d40a3419bc9cdc6ae2b1d5c9da8fe621a66a2a30bfb7a72d6aa68
@@ -0,0 +1,20 @@
1
+ Copyright 2019 Ivan Rudskikh
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,77 @@
1
+ # JsonApiNormalizer
2
+ A simple way to convert datasets based on JSON API specification
3
+
4
+ ## Installation
5
+ Add this line to your application's Gemfile:
6
+
7
+ ```ruby
8
+ gem 'json_api_normalizer'
9
+ ```
10
+
11
+ And then execute:
12
+ ```bash
13
+ $ bundle
14
+ ```
15
+
16
+ Or install it yourself as:
17
+ ```bash
18
+ $ gem install json_api_normalizer
19
+ ```
20
+
21
+ ## Usage
22
+ ```ruby
23
+ require 'json'
24
+ require 'json_api_normalizer'
25
+
26
+ json = JSON.parse(File.read('articles.json'))
27
+ data = JsonApiNormalizer.parse(json)
28
+ puts JSON.pretty_generate(data) # =>
29
+ ```
30
+
31
+ ```json
32
+ {
33
+ "id": "1",
34
+ "title": "JSON API paints my bikeshed!",
35
+ "author": {
36
+ "id": "9",
37
+ "first-name": "Dan",
38
+ "last-name": "Gebhardt",
39
+ "twitter": "dgeb"
40
+ },
41
+ "comments": [
42
+ {
43
+ "id": "5",
44
+ "body": "First!",
45
+ "author": {
46
+ "id": "2",
47
+ "first-name": "John",
48
+ "last-name": "Travolta",
49
+ "twitter": "johnt"
50
+ }
51
+ },
52
+ {
53
+ "id": "12",
54
+ "body": "I like XML better",
55
+ "author": {
56
+ "id": "9",
57
+ "first-name": "Dan",
58
+ "last-name": "Gebhardt",
59
+ "twitter": "dgeb"
60
+ }
61
+ }
62
+ ]
63
+ }
64
+ ```
65
+
66
+
67
+ ## Contributing
68
+
69
+ * Fork the project.
70
+ * Add a breaking test for your change.
71
+ * Make the tests pass.
72
+ * Run `rubocop -a`
73
+ * Push your fork.
74
+ * Submit a pull request.
75
+
76
+ ## License
77
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,27 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'JsonApiNormalizer'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ require 'bundler/gem_tasks'
18
+
19
+ require 'rake/testtask'
20
+
21
+ Rake::TestTask.new(:test) do |t|
22
+ t.libs << 'test'
23
+ t.pattern = 'test/**/*_test.rb'
24
+ t.verbose = false
25
+ end
26
+
27
+ task default: :test
@@ -0,0 +1,7 @@
1
+ require 'json_api_normalizer/parser'
2
+
3
+ module JsonApiNormalizer
4
+ def self.parse(payload)
5
+ Parser.parse(payload)
6
+ end
7
+ end
@@ -0,0 +1,4 @@
1
+ module JsonApiNormalizer
2
+ BaseError = Class.new(StandardError)
3
+ InvalidPayloadError = Class.new(BaseError)
4
+ end
@@ -0,0 +1,69 @@
1
+ require 'json_api_normalizer/errors'
2
+
3
+ module JsonApiNormalizer
4
+ class Parser
5
+ ROOT_KEYS = %w[data errors].freeze
6
+
7
+ attr_reader :payload
8
+ def initialize(payload)
9
+ @payload = payload
10
+ @models = {}
11
+ end
12
+
13
+ def parse
14
+ validate_payload
15
+
16
+ return payload if payload.key?('errors')
17
+
18
+ return normalize(payload['data']) if payload['data'].is_a?(Hash)
19
+
20
+ payload['data'].map { |json_model| normalize(json_model) }
21
+ end
22
+
23
+ def self.parse(payload)
24
+ new(payload).parse
25
+ end
26
+
27
+ private
28
+
29
+ def validate_payload
30
+ return unless (ROOT_KEYS & payload.keys).empty?
31
+
32
+ raise InvalidPayloadError, "JSON:API document must contain at least one of these objects: #{ROOT_KEYS.join(', ')}"
33
+ end
34
+
35
+ def fetch(model, id)
36
+ return @models[model][id] if @models[model] && @models[model][id]
37
+
38
+ @models[model] = {} unless @models.key?(model)
39
+ @models[model][id] = normalize(includes[model][id])
40
+ end
41
+
42
+ def includes
43
+ @includes ||= payload
44
+ .fetch('included', [])
45
+ .group_by { |model| model['type'] }
46
+ .transform_values do |models|
47
+ models.each_with_object({}) { |model, st| st[model['id']] = model }
48
+ end
49
+ end
50
+
51
+ # rubocop: disable Metrics/MethodLength
52
+ def normalize(api_model)
53
+ model = { 'id' => api_model['id'] }.merge!(api_model.fetch('attributes', {}))
54
+ return model unless api_model.key?('relationships')
55
+
56
+ api_model['relationships'].each do |relation_name, relation|
57
+ data = relation['data']
58
+ model[relation_name] =
59
+ if data.is_a?(Hash)
60
+ fetch(*data.values_at('type', 'id'))
61
+ else
62
+ data.map { |r| fetch(*r.values_at('type', 'id')) }
63
+ end
64
+ end
65
+ model
66
+ end
67
+ # rubocop: enable Metrics/MethodLength
68
+ end
69
+ end
@@ -0,0 +1,3 @@
1
+ module JsonApiNormalizer
2
+ VERSION = '0.1.0'.freeze
3
+ end
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: json_api_normalizer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ivan Rudskikh
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-07-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ description: Simple json api normalizer
28
+ email:
29
+ - shredder.rull@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - MIT-LICENSE
35
+ - README.md
36
+ - Rakefile
37
+ - lib/json_api_normalizer.rb
38
+ - lib/json_api_normalizer/errors.rb
39
+ - lib/json_api_normalizer/parser.rb
40
+ - lib/json_api_normalizer/version.rb
41
+ homepage: https://github.com/digital-design-nyc/json_api_normalizer
42
+ licenses:
43
+ - MIT
44
+ metadata: {}
45
+ post_install_message:
46
+ rdoc_options: []
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ requirements: []
60
+ rubyforge_project:
61
+ rubygems_version: 2.7.9
62
+ signing_key:
63
+ specification_version: 4
64
+ summary: Simple json api normalizer
65
+ test_files: []