motion-json-api 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +10 -1
- data/lib/motion-json-api/resource.rb +7 -6
- data/lib/motion-json-api/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 77961f9a9d745834192c875973f89006b5810799
|
4
|
+
data.tar.gz: 8604cbd34596f03adbe94554514b6c55e78cf7a6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: eacaf5b6ccaf822cc270cf2340e029c2cb6242e3258811a34a3c8cf832e26cb7935c77b16d76770de8918115b45e52c87d464b7502db931279cf92aba58ffbc6
|
7
|
+
data.tar.gz: 329d9665d2ec580b788a038bd59e382b196a082d6ba1ed464dfaff2b822e27356e94dbb18567249aceac4884aa6bb91c3f8f38e21e90752fc60efefcf886be7e
|
data/README.md
CHANGED
@@ -5,6 +5,13 @@ A small library that helps you transform JSONApi server responses into objects.
|
|
5
5
|
Note: this gem is not intended to create valid JSON Api payload to send to a server.
|
6
6
|
If you have this need, feel free to contribute.
|
7
7
|
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your Gemfile:
|
11
|
+
|
12
|
+
```
|
13
|
+
gem 'motion-json-api'
|
14
|
+
```
|
8
15
|
|
9
16
|
## Usage
|
10
17
|
|
@@ -15,6 +22,7 @@ class User < MotionJsonApi::Resource
|
|
15
22
|
resource_type :users
|
16
23
|
|
17
24
|
attribute :name
|
25
|
+
attribute :profile_name, key: :username
|
18
26
|
|
19
27
|
has_one :blog
|
20
28
|
end
|
@@ -32,13 +40,14 @@ class Article < MotionJsonApi::Resource
|
|
32
40
|
attribute :title
|
33
41
|
attribute :body
|
34
42
|
|
35
|
-
has_one :user
|
43
|
+
has_one :user
|
36
44
|
end
|
37
45
|
|
38
46
|
resource = MotionJsonApi.parse(server_response)
|
39
47
|
|
40
48
|
resource.user
|
41
49
|
resource.user.name
|
50
|
+
resource.user.username
|
42
51
|
resource.user.blog
|
43
52
|
resource.user.blog.title
|
44
53
|
resource.user.blog.articles
|
@@ -33,16 +33,17 @@ module MotionJsonApi
|
|
33
33
|
@_resource_type
|
34
34
|
end
|
35
35
|
|
36
|
-
def self.attribute(attribute)
|
37
|
-
|
36
|
+
def self.attribute(attribute, options = {})
|
37
|
+
key = options.fetch(:key, attribute)
|
38
|
+
define_method(key) do
|
38
39
|
self.attributes.fetch(attribute.to_s)
|
39
40
|
end
|
40
41
|
end
|
41
42
|
|
42
43
|
def self.has_one(relation, options = {})
|
43
|
-
key = options.fetch(:
|
44
|
+
key = options.fetch(:key, relation)
|
44
45
|
define_method(key) do
|
45
|
-
relationship = self.relationships.fetch(
|
46
|
+
relationship = self.relationships.fetch(relation.to_s)
|
46
47
|
|
47
48
|
data = relationship.fetch("data")
|
48
49
|
if data
|
@@ -56,9 +57,9 @@ module MotionJsonApi
|
|
56
57
|
end
|
57
58
|
|
58
59
|
def self.has_many(relation, options = {})
|
59
|
-
key = options.fetch(:
|
60
|
+
key = options.fetch(:key, relation)
|
60
61
|
define_method(key) do
|
61
|
-
relationship = self.relationships.fetch(
|
62
|
+
relationship = self.relationships.fetch(relation.to_s)
|
62
63
|
relationship.fetch("data").map do |data|
|
63
64
|
object = _find_in_included(data["id"], data["type"])
|
64
65
|
Resource._object_handler({"data" => object}, self.top_level, self.included)
|