ja2r 0.1.2 → 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: 3714dfab924b4b0e8ea22ef697411766f28656f24ab7482d7b18db6eb2a052a3
4
- data.tar.gz: 4f0b9817c27f9a2c5d19ad374301c004f95f7dd3e06a5e439b4b500be2d47093
3
+ metadata.gz: 4a63874306c133ab82461114f0162d0acf55758122c631eaf421a3efcb515d70
4
+ data.tar.gz: 8001da58d5d720839e4e84772a02e41e6d7a08c7e940fcf6096bda08e22b76dd
5
5
  SHA512:
6
- metadata.gz: c46f7996751caddfcf980330328c28dba22c5cd178bf9dce86b5e672bb9e21e08ff8d2d839b2c3aab120af26cba8ae75eeccc10e7f33da4da94b27ab0e8a5385
7
- data.tar.gz: a6d463d6258585b5a8d5ca8d92009d9e93650c918f4e59a302d1fbd3f1fe00c792a4b915fbbd1742f2052a29148d634990a05a3f975e22bce399a6228c362cad
6
+ metadata.gz: 8a255f27f9d7511c06ed90f62b13b1ee3bef19296548732bd37852f18059534edce253ac147f4833bdadd59db3686c9104c51ae4ea14fd67ff729bd6293b4a06
7
+ data.tar.gz: 3616d5c1c52ee42179276e735b4e26a59ddcea542669c875d7076417fa020cdc2ead536b3b1122f224a4cc5ece8ea288512771657a271cc62b5a17a43d780e2e
data/README.md CHANGED
@@ -1,5 +1,8 @@
1
1
  # JA2R
2
2
 
3
+ [![Gem Version](https://badge.fury.io/rb/ja2r.svg)](https://badge.fury.io/rb/ja2r)
4
+ [![Build Status](https://travis-ci.org/mkon/ja2r.svg?branch=master)](https://travis-ci.org/mkon/ja2r)
5
+
3
6
  JASON-API to Ruby Object
4
7
 
5
8
  Converts a JSON-API payload into a ruby object which supports navigation over relationships.
@@ -0,0 +1,47 @@
1
+ module JA2R
2
+ class Element
3
+ def initialize(payload)
4
+ @id = payload['id']
5
+ @type = payload['type']
6
+ @attributes = (payload['attributes'] || {}).with_indifferent_access
7
+ @meta = (payload['meta'] || {}).with_indifferent_access
8
+ @relationships = payload['relationships'] ? convert_relationship(payload['relationships']) : {}
9
+ end
10
+
11
+ attr_reader :id, :type, :attributes, :relationships
12
+
13
+ def attribute(key)
14
+ @attributes[key]
15
+ end
16
+
17
+ def meta(key)
18
+ @meta[key]
19
+ end
20
+
21
+ private
22
+
23
+ def method_missing(method, *args)
24
+ return attributes[method] if attributes&.key? method
25
+ return relationships[method] if relationships&.key? method
26
+
27
+ super
28
+ end
29
+
30
+ def respond_to_missing?(symbol, include_all = false)
31
+ return true if attributes&.key?(symbol.to_s)
32
+ return true if relationships&.key?(symbol.to_s)
33
+
34
+ super
35
+ end
36
+
37
+ def convert_relationship(hash)
38
+ Hash[hash.map do |key, data|
39
+ if data['data'].is_a? Array
40
+ [key, data['data'].map { |d| KlassRegistry.instantiate(d) }]
41
+ else
42
+ [key, KlassRegistry.instantiate(data['data'])]
43
+ end
44
+ end].with_indifferent_access
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,23 @@
1
+ module JA2R
2
+ module KlassRegistry
3
+ extend self
4
+
5
+ def instantiate(hash)
6
+ lookup(hash['type']).new(hash)
7
+ end
8
+
9
+ def lookup(type)
10
+ registry.fetch(type) { Element }
11
+ end
12
+
13
+ def register(type, klass)
14
+ registry[type] = klass
15
+ end
16
+
17
+ private
18
+
19
+ def registry
20
+ @registry ||= {}
21
+ end
22
+ end
23
+ end
data/lib/ja2r/parser.rb CHANGED
@@ -23,20 +23,20 @@ module JA2R
23
23
  end
24
24
 
25
25
  def parse_single
26
- root = Element.new hash['data']
26
+ root = KlassRegistry.instantiate(hash['data'])
27
27
  object_space << root
28
28
  root
29
29
  end
30
30
 
31
31
  def parse_list
32
- root = hash['data'].map { |data| Element.new data }
32
+ root = hash['data'].map { |data| KlassRegistry.instantiate(data) }
33
33
  object_space.push(*root)
34
34
  root
35
35
  end
36
36
 
37
37
  def parse_included
38
38
  hash['included']&.map do |data|
39
- object_space.push Element.new(data)
39
+ object_space.push KlassRegistry.instantiate(data)
40
40
  end
41
41
  end
42
42
 
@@ -54,6 +54,7 @@ module JA2R
54
54
  end
55
55
  else
56
56
  next unless (obj = object_space.dig(relationship.type, relationship.id))
57
+
57
58
  element.relationships[key] = obj
58
59
  end
59
60
  end
data/lib/ja2r.rb CHANGED
@@ -2,46 +2,17 @@ require 'active_support/core_ext/array'
2
2
  require 'active_support/core_ext/hash'
3
3
 
4
4
  module JA2R
5
- autoload :Parser, 'ja2r/parser'
5
+ autoload :Element, 'ja2r/element'
6
+ autoload :KlassRegistry, 'ja2r/klass_registry'
7
+ autoload :Parser, 'ja2r/parser'
6
8
 
7
9
  module_function
8
10
 
9
- class Element
10
- def initialize(payload)
11
- @id = payload['id']
12
- @type = payload['type']
13
- @attributes = (payload['attributes'] || {}).with_indifferent_access
14
- @relationships = payload['relationships'] ? convert_relationship(payload['relationships']) : {}
15
- end
16
-
17
- attr_reader :id, :type, :attributes, :relationships
18
-
19
- private
20
-
21
- def method_missing(method, *args)
22
- return attributes[method] if attributes&.key? method
23
- return relationships[method] if relationships&.key? method
24
- super
25
- end
26
-
27
- def respond_to_missing?(symbol, include_all = false)
28
- return true if attributes&.key?(symbol.to_s)
29
- return true if relationships&.key?(symbol.to_s)
30
- super
31
- end
32
-
33
- def convert_relationship(hash)
34
- Hash[hash.map do |key, data|
35
- if data['data'].is_a? Array
36
- [key, data['data'].map { |d| Element.new(d) }]
37
- else
38
- [key, Element.new(data['data'])]
39
- end
40
- end].with_indifferent_access
41
- end
42
- end
43
-
44
11
  def parse(hash)
45
12
  Parser.new(hash).call
46
13
  end
14
+
15
+ def klass_for(hash)
16
+ KlassRegistry.call(hash['type'])
17
+ end
47
18
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ja2r
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - mkon
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-08-28 00:00:00.000000000 Z
11
+ date: 2018-11-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -50,14 +50,28 @@ dependencies:
50
50
  requirements:
51
51
  - - '='
52
52
  - !ruby/object:Gem::Version
53
- version: 0.54.0
53
+ version: 0.60.0
54
54
  type: :development
55
55
  prerelease: false
56
56
  version_requirements: !ruby/object:Gem::Requirement
57
57
  requirements:
58
58
  - - '='
59
59
  - !ruby/object:Gem::Version
60
- version: 0.54.0
60
+ version: 0.60.0
61
+ - !ruby/object:Gem::Dependency
62
+ name: rubocop-rspec
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - '='
66
+ - !ruby/object:Gem::Version
67
+ version: 1.30.1
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - '='
73
+ - !ruby/object:Gem::Version
74
+ version: 1.30.1
61
75
  - !ruby/object:Gem::Dependency
62
76
  name: simplecov
63
77
  requirement: !ruby/object:Gem::Requirement
@@ -82,6 +96,8 @@ files:
82
96
  - LICENSE
83
97
  - README.md
84
98
  - lib/ja2r.rb
99
+ - lib/ja2r/element.rb
100
+ - lib/ja2r/klass_registry.rb
85
101
  - lib/ja2r/parser.rb
86
102
  homepage: https://github.com/mkon/ja2r
87
103
  licenses: