jsonapi-renderer 0.1.1.beta3 → 0.1.1

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
  SHA1:
3
- metadata.gz: 0e0ff29d67fa35e41f7a46f6f4bec480555d9318
4
- data.tar.gz: 0d2a619490327182f40f978a917bc80b534f09ca
3
+ metadata.gz: 07afefd2fe41039cb8c922578ba39e3aba41f5ec
4
+ data.tar.gz: e269e8a82ad892be4e7f9081e30487301c20d30d
5
5
  SHA512:
6
- metadata.gz: 9c7b8a58093c1a7a01e1bf352b4232471f483c910386c24d77cc32301bfeb477089a163e78ab57a55d4d3e900a4945ee031cb892fe45e4a0854a3d8fab6d741f
7
- data.tar.gz: e61f0a433b6a84b238e9717128073ec934557a9e7ec9e912a29adad883d9c29e902ab65c56862b456407bc617b50239c16a1638ce45f088c63e68bccf83755be
6
+ metadata.gz: 2b79ebe7aa3d18209658d89042d8dfcc06d5a2a837688b57c9d3495b173aa50f65e6f35a0db0d4bb7c40b1fc0915db5b7733f016a4643e3b684b0878db9aac40
7
+ data.tar.gz: e320d995609cb5a3064533b08b01cf86133a513932ce22bfdc5eeede5c64d2b2c0899fcab2907d10251927910bb4ffd780f641377e722056aff80bb7a616253a
data/README.md CHANGED
@@ -4,7 +4,9 @@ Ruby gem for rendering [JSON API](http://jsonapi.org) documents.
4
4
  ## Status
5
5
 
6
6
  [![Gem Version](https://badge.fury.io/rb/jsonapi-renderer.svg)](https://badge.fury.io/rb/jsonapi-renderer)
7
- [![Build Status](https://secure.travis-ci.org/jsonapi-rb/renderer.svg?branch=master)](http://travis-ci.org/jsonapi-rb/renderer?branch=master)
7
+ [![Build Status](https://secure.travis-ci.org/jsonapi-rb/jsonapi-renderer.svg?branch=master)](http://travis-ci.org/jsonapi-rb/renderer?branch=master)
8
+ [![codecov](https://codecov.io/gh/jsonapi-rb/jsonapi-renderer/branch/master/graph/badge.svg)](https://codecov.io/gh/jsonapi-rb/renderer)
9
+ [![Gitter chat](https://badges.gitter.im/gitterHQ/gitter.png)](https://gitter.im/jsonapi-rb/Lobby)
8
10
 
9
11
  ## Installation
10
12
  ```ruby
@@ -40,18 +42,18 @@ class ResourceInterface
40
42
  # @return [String]
41
43
  def jsonapi_id; end
42
44
 
43
- # Returns a hash containing, for each included relationship, the resource(s)
44
- # to be included from that one.
45
+ # Returns a hash containing, for each included relationship, an array of the
46
+ # resources to be included from that one.
45
47
  # @param [Array<Symbol>] included_relationships The keys of the relationships
46
48
  # to be included.
47
- # @return [Hash{Symbol => #ResourceInterface, Array<#ResourceInterface>}]
49
+ # @return [Hash{Symbol => Array<#ResourceInterface>}]
48
50
  def jsonapi_related(included_relationships); end
49
51
 
50
52
  # Returns a JSON API-compliant representation of the resource as a hash.
51
53
  # @param [Hash] options
52
54
  # @option [Array<Symbol>, Nil] fields The requested fields, or nil.
53
- # @option [Array<Symbol>, Nil] included The requested included
54
- # relationships, or nil.
55
+ # @option [Array<Symbol>] include The requested relationships to
56
+ # include (defaults to []).
55
57
  # @return [Hash]
56
58
  def as_jsonapi(options = {}); end
57
59
  end
@@ -9,7 +9,7 @@ module JSONAPI
9
9
  @errors = params.fetch(:errors, [])
10
10
  @meta = params.fetch(:meta, nil)
11
11
  @links = params.fetch(:links, {})
12
- @fields = params.fetch(:fields, {})
12
+ @fields = _symbolize_fields(params.fetch(:fields, {}))
13
13
  @jsonapi = params.fetch(:jsonapi, nil)
14
14
  @include = JSONAPI::IncludeDirective.new(params.fetch(:include, {}))
15
15
  end
@@ -48,6 +48,12 @@ module JSONAPI
48
48
  hash[:errors] = @errors.map(&:as_jsonapi)
49
49
  end
50
50
  end
51
+
52
+ def _symbolize_fields(fields)
53
+ fields.each_with_object({}) do |(k, v), h|
54
+ h[k.to_sym] = v.map(&:to_sym)
55
+ end
56
+ end
51
57
  end
52
58
  end
53
59
  end
@@ -7,58 +7,78 @@ module JSONAPI
7
7
  @resources = resources
8
8
  @include = include
9
9
  @fields = fields
10
- @primary = []
11
- @included = []
12
- @hashes = {}
13
- @queue = []
14
- @processed = Set.new # NOTE(beauby): Set of [type, id, prefix].
15
10
  end
16
11
 
17
12
  def process
18
- @resources.each do |res|
19
- process_resource(res, '', @include, true)
20
- @processed.add([res.jsonapi_type, res.jsonapi_id, ''])
21
- end
22
- until @queue.empty?
23
- res, prefix, include_dir = @queue.pop
24
- process_resource(res, prefix, include_dir, false)
25
- end
13
+ traverse_resources
14
+ process_resources
26
15
 
27
16
  [@primary, @included]
28
17
  end
29
18
 
30
19
  private
31
20
 
32
- def merge_resources!(a, b)
33
- b[:relationships].each do |name, rel|
34
- a[:relationships][name][:data] ||= rel[:data] if rel.key?(:data)
35
- if rel.key?(:links)
36
- (a[:relationships][name][:links] ||= {}).merge!(rel[:links])
37
- end
21
+ def traverse_resources
22
+ @traversed = Set.new # [type, id, prefix]
23
+ @include_rels = {} # [type, id => Set]
24
+ @queue = []
25
+ @primary = []
26
+ @included = []
27
+
28
+ initialize_queue
29
+ traverse_queue
30
+ end
31
+
32
+ def initialize_queue
33
+ @resources.each do |res|
34
+ @traversed.add([res.jsonapi_type, res.jsonapi_id, ''])
35
+ traverse_resource(res, @include.keys, true)
36
+ enqueue_related_resources(res, '', @include)
38
37
  end
39
38
  end
40
39
 
41
- def process_resource(res, prefix, include_dir, is_primary)
40
+ def traverse_queue
41
+ until @queue.empty?
42
+ res, prefix, include_dir = @queue.shift
43
+ traverse_resource(res, include_dir.keys, false)
44
+ enqueue_related_resources(res, prefix, include_dir)
45
+ end
46
+ end
47
+
48
+ def traverse_resource(res, include_keys, primary)
42
49
  ri = [res.jsonapi_type, res.jsonapi_id]
43
- hash = res.as_jsonapi(fields: @fields[res.jsonapi_type.to_sym],
44
- include: include_dir.keys)
45
- if @hashes.key?(ri)
46
- merge_resources!(@hashes[ri], hash)
50
+ if @include_rels.include?(ri)
51
+ @include_rels[ri].merge!(include_keys)
47
52
  else
48
- (is_primary ? @primary : @included) << (@hashes[ri] = hash)
53
+ @include_rels[ri] = Set.new(include_keys)
54
+ (primary ? @primary : @included) << res
49
55
  end
50
- process_relationships(res, prefix, include_dir)
51
56
  end
52
57
 
53
- def process_relationships(res, prefix, include_dir)
58
+ def enqueue_related_resources(res, prefix, include_dir)
54
59
  res.jsonapi_related(include_dir.keys).each do |key, data|
55
- Array(data).each do |child_res|
60
+ data.each do |child_res|
56
61
  next if child_res.nil?
57
62
  child_prefix = "#{prefix}.#{key}"
58
- next unless @processed.add?([child_res.jsonapi_type,
59
- child_res.jsonapi_id,
60
- child_prefix])
61
- @queue << [child_res, child_prefix, include_dir[key]]
63
+ enqueue_resource(child_res, child_prefix, include_dir[key])
64
+ end
65
+ end
66
+ end
67
+
68
+ def enqueue_resource(res, prefix, include_dir)
69
+ return unless @traversed.add?([res.jsonapi_type,
70
+ res.jsonapi_id,
71
+ prefix])
72
+ @queue << [res, prefix, include_dir]
73
+ end
74
+
75
+ def process_resources
76
+ [@primary, @included].each do |resources|
77
+ resources.map! do |res|
78
+ ri = [res.jsonapi_type, res.jsonapi_id]
79
+ include_dir = @include_rels[ri]
80
+ fields = @fields[res.jsonapi_type.to_sym]
81
+ res.as_jsonapi(include: include_dir, fields: fields)
62
82
  end
63
83
  end
64
84
  end
@@ -7,13 +7,14 @@ module JSONAPI
7
7
  #
8
8
  # @param [Hash] params
9
9
  # @option [(#jsonapi_id, #jsonapi_type, #jsonapi_related, #as_jsonapi),
10
- # Array<(#jsonapi_id, #jsonapi_type, #jsonapi_related, #as_jsonapi)>,
10
+ # Array<(#jsonapi_id, #jsonapi_type, #jsonapi_related,
11
+ # #as_jsonapi)>,
11
12
  # nil] data Primary resource(s) to be rendered.
12
13
  # @option [Array<#jsonapi_id>] errors Errors to be rendered.
13
- # @option [String, Hash{Symbol => Hash}] include Relationships to be
14
- # included.
15
- # @option [Hash{Symbol, Array<Symbol>}] fields List of requested fields
16
- # for some or all of the resource types.
14
+ # @option include Relationships to be included. See
15
+ # JSONAPI::IncludeDirective.
16
+ # @option [Hash{Symbol, Array<Symbol>}, Hash{String, Array<String>}] fields
17
+ # List of requested fields for some or all of the resource types.
17
18
  # @option [Hash] meta Non-standard top-level meta information to be
18
19
  # included.
19
20
  # @option [Hash] links Top-level links to be included.
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsonapi-renderer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1.beta3
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lucas Hosseini
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-11-14 00:00:00.000000000 Z
11
+ date: 2016-12-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '3.5'
41
+ - !ruby/object:Gem::Dependency
42
+ name: codecov
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.1'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.1'
41
55
  description: Efficiently render JSON API documents.
42
56
  email: lucas.hosseini@gmail.com
43
57
  executables: []
@@ -50,7 +64,7 @@ files:
50
64
  - lib/jsonapi/renderer.rb
51
65
  - lib/jsonapi/renderer/document.rb
52
66
  - lib/jsonapi/renderer/resources_processor.rb
53
- homepage: https://github.com/jsonapi-rb/renderer
67
+ homepage: https://github.com/jsonapi-rb/jsonapi-renderer
54
68
  licenses:
55
69
  - MIT
56
70
  metadata: {}
@@ -65,12 +79,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
65
79
  version: '0'
66
80
  required_rubygems_version: !ruby/object:Gem::Requirement
67
81
  requirements:
68
- - - ">"
82
+ - - ">="
69
83
  - !ruby/object:Gem::Version
70
- version: 1.3.1
84
+ version: '0'
71
85
  requirements: []
72
86
  rubyforge_project:
73
- rubygems_version: 2.5.1
87
+ rubygems_version: 2.6.8
74
88
  signing_key:
75
89
  specification_version: 4
76
90
  summary: Render JSONAPI documents.