motion-json-api 0.0.2 → 0.0.3

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: 7dd1b8babd9d470b1b7af3fd3106e26496a81734
4
- data.tar.gz: 1cc071905c1f335c2a7c821a1b6f701e6a32f2de
3
+ metadata.gz: 77961f9a9d745834192c875973f89006b5810799
4
+ data.tar.gz: 8604cbd34596f03adbe94554514b6c55e78cf7a6
5
5
  SHA512:
6
- metadata.gz: 761ba2811df1c545547ba321d9788e1d06e4f33bdc49f425577d51bf62d14f8351f8f4cddd26385d1674d60134c78a2fac711f75bb19b60b99e56fdca747d3ad
7
- data.tar.gz: 15c6a28c9c2abbe4b78160d207904277c66ae69641f09c561cd185eeac2c0363745cbbaebe23d2671431b1ea6b8e7c8d1e37cf4d134b5ff948e3c65bdda3707f
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, as: :author
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
- define_method(attribute) do
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(:as, relation).to_s
44
+ key = options.fetch(:key, relation)
44
45
  define_method(key) do
45
- relationship = self.relationships.fetch(key)
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(:as, relation).to_s
60
+ key = options.fetch(:key, relation)
60
61
  define_method(key) do
61
- relationship = self.relationships.fetch(key)
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)
@@ -1,3 +1,3 @@
1
1
  module MotionJsonApi
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: motion-json-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joffrey Jaffeux