motion-json-api 0.0.3 → 0.0.4

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: 77961f9a9d745834192c875973f89006b5810799
4
- data.tar.gz: 8604cbd34596f03adbe94554514b6c55e78cf7a6
3
+ metadata.gz: 1873feb8c0a1010e7761c7b688c772bed96f0b36
4
+ data.tar.gz: d887f7bcea7afacaab8b813ae0fe2ebe384d9a3f
5
5
  SHA512:
6
- metadata.gz: eacaf5b6ccaf822cc270cf2340e029c2cb6242e3258811a34a3c8cf832e26cb7935c77b16d76770de8918115b45e52c87d464b7502db931279cf92aba58ffbc6
7
- data.tar.gz: 329d9665d2ec580b788a038bd59e382b196a082d6ba1ed464dfaff2b822e27356e94dbb18567249aceac4884aa6bb91c3f8f38e21e90752fc60efefcf886be7e
6
+ metadata.gz: a49636a82da8d282d1dc5f26c9c0a3878a3c83cdc9001bdc88fd4147f9c57e9abbc59a709075e444ec69b30dcf351eaf5ee2e43f89d2e1b5051ececa7891b79e
7
+ data.tar.gz: 7907a3ff8903fd5515f2879c2a9f3865099b9e1187bb58aa21a8245d4a241d67cf201f97a502c15b948bfe4542fa830d1d804d177e8de4db69d55537fc7d0f59
@@ -1,3 +1,6 @@
1
1
  module MotionJsonApi
2
2
  class UndefinedResource < StandardError; end
3
+ class UndefinedAttribute < StandardError; end
4
+ class UndefinedHasOneRelation < StandardError; end
5
+ class UndefinedHasManyRelation < StandardError; end
3
6
  end
@@ -16,9 +16,11 @@ module MotionJsonApi
16
16
 
17
17
  def initialize(object, top_level = [], included = [])
18
18
  @id = object["data"]["id"]
19
- @attributes = object["data"]["attributes"]
20
- @relationships = object["data"]["relationships"]
21
- @meta = object.fetch("meta", {})
19
+ @attributes = object["data"].fetch("attributes", {})
20
+ @relationships = object["data"].fetch("relationships", {})
21
+ @meta = object.fetch("meta") do
22
+ object["data"].fetch("meta", {})
23
+ end
22
24
  @links = object.fetch("links", {})
23
25
  @top_level = top_level
24
26
  @included = included
@@ -36,14 +38,18 @@ module MotionJsonApi
36
38
  def self.attribute(attribute, options = {})
37
39
  key = options.fetch(:key, attribute)
38
40
  define_method(key) do
39
- self.attributes.fetch(attribute.to_s)
41
+ self.attributes.fetch(attribute.to_s) do
42
+ raise UndefinedAttribute, "Couldn't find attribute:`#{attribute}` in #{self.attributes}"
43
+ end
40
44
  end
41
45
  end
42
46
 
43
47
  def self.has_one(relation, options = {})
44
48
  key = options.fetch(:key, relation)
45
49
  define_method(key) do
46
- relationship = self.relationships.fetch(relation.to_s)
50
+ relationship = self.relationships.fetch(relation.to_s) do
51
+ raise UndefinedHasOneRelation, "Couldn't find relation:`#{relation}` in #{self.relationships}"
52
+ end
47
53
 
48
54
  data = relationship.fetch("data")
49
55
  if data
@@ -59,7 +65,10 @@ module MotionJsonApi
59
65
  def self.has_many(relation, options = {})
60
66
  key = options.fetch(:key, relation)
61
67
  define_method(key) do
62
- relationship = self.relationships.fetch(relation.to_s)
68
+ relationship = self.relationships.fetch(relation.to_s) do
69
+ raise UndefinedHasManyRelation, "Couldn't find relation:`#{relation}` in #{self.relationships}"
70
+ end
71
+
63
72
  relationship.fetch("data").map do |data|
64
73
  object = _find_in_included(data["id"], data["type"])
65
74
  Resource._object_handler({"data" => object}, self.top_level, self.included)
@@ -97,7 +106,6 @@ module MotionJsonApi
97
106
  included ||= self.included || object.fetch("included", [])
98
107
  top_level ||= self.top_level || object["data"]
99
108
  top_level = [top_level].flatten
100
-
101
109
  case object["data"]
102
110
  when Array
103
111
  return object["data"].map do |data|
@@ -1,3 +1,3 @@
1
1
  module MotionJsonApi
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: motion-json-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joffrey Jaffeux
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-11-20 00:00:00.000000000 Z
11
+ date: 2016-11-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler