json_api_ruby 0.0.1.alpha → 0.0.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: f137dccac6caa31e15ca4519ed3b6a9e45682739
4
- data.tar.gz: 5256e906efe7899bfacd0c6916620bdc066d8dc6
3
+ metadata.gz: aecf0984e8890283e322162acfacab44fa72647c
4
+ data.tar.gz: 08fc7edccd62b61df8a9f0883dcfb790a4a9af2e
5
5
  SHA512:
6
- metadata.gz: 76ce05324913fb8100783b4a3f274c98c1e4a8afa6d2ec0fc90db58f8d1aac39379012de2bd6dd0f178343d11f805a867fb9b1a715df3021ea76c5d50f62ba83
7
- data.tar.gz: 1bd8c66e4ebb6c7ddf1f0137f90fe5050b08c5e81d0b4c9503f42cf095fc2124d94b86ec7b44954a4c888634aa37294e207c2d3b55342060ee77e4074edca2d7
6
+ metadata.gz: 97f8d15d1a25fe1cfd6bdf8694e35266d5f968c4c82697d2214369e987a38d1008fd9b5eb54bce68874572f97b2d52527d2e747ce5be0c9b3ecfdce0cc5e762a
7
+ data.tar.gz: c2dbc5d3b6dc759724b1f7d332cb49d9ecfde7583d9e046399671ba838f41c38d8f08164557186af36de0b53edd21d340b7e66fe4f616383acf2cb4f8f1c499e
data/README.md ADDED
@@ -0,0 +1,9 @@
1
+ # jsonapi_ruby
2
+ Ruby serializer that takes a resource object and spits it out as a JSON API document
3
+
4
+ ### Usage
5
+ See the <a href="https://github.com/teubanks/jsonapi_ruby/wiki">Wiki</a> page or the tests for usage
6
+
7
+ ### Status
8
+ Definitely a work in progress.
9
+ This is currently an Alpha release, use at your own peril
@@ -47,8 +47,9 @@ module JsonApi
47
47
  attr_reader :includes
48
48
 
49
49
  def initialize(model, options={})
50
+ options.stringify_keys!
50
51
  @_model = model
51
- @includes = options.fetch(:include, []).map(&:to_s)
52
+ @includes = Array.wrap(options.fetch('include', [])).map(&:to_s)
52
53
  build_object_graph # base module method
53
54
  end
54
55
  end
@@ -51,8 +51,8 @@ module JsonApi
51
51
  @relationships ||= []
52
52
  Array(self.class.relationships).each do |relationship|
53
53
  included = includes.include?(relationship.name)
54
- relationship.build_resources({parent_resource: self, included: included})
55
- @relationships << relationship
54
+ rel = relationship.build_resources({parent_resource: self, included: included})
55
+ @relationships << rel
56
56
  end
57
57
  end
58
58
  end
@@ -32,11 +32,7 @@ module JsonApi
32
32
  private
33
33
  def add_relationship(object_name, cardinality, options)
34
34
  @relationships ||= []
35
- if cardinality == :one
36
- @relationships << ToOneRelationship.new(object_name, options)
37
- else
38
- @relationships << ToManyRelationship.new(object_name, options)
39
- end
35
+ @relationships << RelationshipMeta.new(object_name, options.merge(cardinality: cardinality))
40
36
  create_accessor_methods(object_name)
41
37
  end
42
38
 
@@ -1,7 +1,7 @@
1
1
  module JsonApi
2
2
  module Resources
3
3
 
4
- class Relationships
4
+ class RelationshipMeta
5
5
  # The name of this relationship.
6
6
  # This name comes from the resource object that defines the
7
7
  # relationship. Example:
@@ -10,6 +10,25 @@ module JsonApi
10
10
  # end
11
11
  attr_reader :name
12
12
 
13
+ attr_reader :cardinality
14
+
15
+ def initialize(name, options)
16
+ @name = name.to_s
17
+ @cardinality = options.fetch(:cardinality)
18
+ end
19
+
20
+ def build_resources(options)
21
+ if cardinality == :one
22
+ relationship = ToOneRelationship.new(name, options)
23
+ else
24
+ relationship = ToManyRelationship.new(name, options)
25
+ end
26
+ relationship.build_resources(options)
27
+ relationship
28
+ end
29
+ end
30
+
31
+ class Relationship
13
32
  # The resource object that "owns" this relationship
14
33
  #
15
34
  # Example:
@@ -19,28 +38,19 @@ module JsonApi
19
38
  #
20
39
  # `ArticleResource` is the parent of the author object
21
40
  attr_reader :parent
41
+ attr_reader :parent_model
22
42
 
23
43
  # Determines whether the `data` attribute should be filled out and
24
44
  # included
25
45
  attr_reader :included
46
+ attr_reader :name
26
47
 
27
48
  # The resource object that represents this relationship
28
49
  attr_reader :resources
29
50
 
30
- attr_reader :parent_model
31
-
32
51
  def initialize(name, options)
33
- @name = name.to_s
52
+ @name = name
34
53
  @resources = []
35
- end
36
-
37
- def to_hash
38
- return_hash = links
39
- return_hash.merge!(data) if included?
40
- return_hash
41
- end
42
-
43
- def build_resources(options)
44
54
  @parent = options.fetch(:parent_resource)
45
55
  @parent_model = parent._model
46
56
  @included = options.fetch(:included, false)
@@ -50,6 +60,12 @@ module JsonApi
50
60
  included == true
51
61
  end
52
62
 
63
+ def to_hash
64
+ return_hash = links
65
+ return_hash.merge!(data) if included?
66
+ return_hash
67
+ end
68
+
53
69
  def links
54
70
  {
55
71
  'links' => {
@@ -61,17 +77,17 @@ module JsonApi
61
77
  end
62
78
 
63
79
  # convenience classes
64
- class ToOneRelationship < Relationships
80
+ class ToOneRelationship < Relationship
65
81
  def data(options={})
66
82
  identifier_hash = resource_object.identifier_hash if resource_object
67
- {'data' => Hash(identifier_hash)}
83
+ {'data' => identifier_hash}
68
84
  end
69
85
 
70
86
  def build_resources(options)
71
- super
72
87
  return unless included?
73
88
  resource_model = parent_model.send(name)
74
89
  return if resource_model.blank?
90
+
75
91
  resource_class = Discovery.resource_for_name(resource_model, options.merge(parent_resource: parent))
76
92
  @resources << resource_class.new(resource_model)
77
93
  end
@@ -81,7 +97,7 @@ module JsonApi
81
97
  end
82
98
  end
83
99
 
84
- class ToManyRelationship < Relationships
100
+ class ToManyRelationship < Relationship
85
101
  def data(options={})
86
102
  data = resource_objects.map do |object|
87
103
  object.identifier_hash
@@ -90,8 +106,8 @@ module JsonApi
90
106
  end
91
107
 
92
108
  def build_resources(options)
93
- super
94
109
  return unless included?
110
+
95
111
  parent_model.send(name).each do |resource_model|
96
112
  resource_class = Discovery.resource_for_name(resource_model, options.merge(parent_resource: parent))
97
113
  @resources << resource_class.new(resource_model)
@@ -1,3 +1,3 @@
1
1
  module JsonApi
2
- VERSION = '0.0.1.alpha'
2
+ VERSION = '0.0.1'
3
3
  end
@@ -1,6 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
- RSpec.describe JsonApi::Resources::Relationships do
3
+ RSpec.describe JsonApi::Resources::RelationshipMeta do
4
4
  let(:article) do
5
5
  Article.new('How to raise Triops', 'Triops are hardy little creatures whose eggs can be frozen for as long as 40 years')
6
6
  end
@@ -23,8 +23,8 @@ RSpec.describe JsonApi::Resources::Relationships do
23
23
 
24
24
  describe 'link serialization' do
25
25
  subject(:links_object) do
26
- author_relation.build_resources(parent_resource: article_resource)
27
- author_relation.to_hash
26
+ rel = author_relation.build_resources(parent_resource: article_resource)
27
+ rel.to_hash
28
28
  end
29
29
 
30
30
  it 'includes a links object' do
@@ -54,8 +54,8 @@ RSpec.describe JsonApi::Resources::Relationships do
54
54
 
55
55
  describe 'data serialization' do
56
56
  subject(:serialized_object) do
57
- author_relation.build_resources(parent_resource: article_resource, included: true)
58
- author_relation.to_hash
57
+ rel = author_relation.build_resources(parent_resource: article_resource, included: true)
58
+ rel.to_hash
59
59
  end
60
60
 
61
61
  it 'has a data top level object' do
@@ -65,8 +65,8 @@ RSpec.describe JsonApi::Resources::Relationships do
65
65
  describe 'data object' do
66
66
  context 'when an array' do
67
67
  subject(:serialized_object) do
68
- comments_relation.build_resources(parent_resource: article_resource, included: true)
69
- comments_relation.to_hash
68
+ rel = comments_relation.build_resources(parent_resource: article_resource, included: true)
69
+ rel.to_hash
70
70
  end
71
71
 
72
72
  it 'has an identity hash for each object' do
@@ -84,8 +84,8 @@ RSpec.describe JsonApi::Resources::Relationships do
84
84
 
85
85
  describe 'identity hash' do
86
86
  it 'returns an identity hash given a model and parent resource' do
87
- author_relation.build_resources(parent_resource: article_resource, included: true)
88
- serialized_object = author_relation.to_hash
87
+ rel = author_relation.build_resources(parent_resource: article_resource, included: true)
88
+ serialized_object = rel.to_hash
89
89
  expect(serialized_object['data'].keys).to eq ['id', 'type']
90
90
  end
91
91
  end
@@ -93,16 +93,16 @@ RSpec.describe JsonApi::Resources::Relationships do
93
93
  describe 'relationship serialization' do
94
94
  context 'when the option "include" is true' do
95
95
  it 'includes the data object' do
96
- author_relation.build_resources(parent_resource: article_resource, included: true)
97
- serialized_data = author_relation.to_hash
96
+ rel = author_relation.build_resources(parent_resource: article_resource, included: true)
97
+ serialized_data = rel.to_hash
98
98
  expect(serialized_data).to include('data')
99
99
  end
100
100
  end
101
101
 
102
102
  context 'when the option "include" is falsey' do
103
103
  it 'does not include data object' do
104
- author_relation.build_resources(parent_resource: article_resource)
105
- serialized_data = author_relation.to_hash
104
+ rel = author_relation.build_resources(parent_resource: article_resource)
105
+ serialized_data = rel.to_hash
106
106
  expect(serialized_data).to_not include('data')
107
107
  end
108
108
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json_api_ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1.alpha
4
+ version: 0.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tracey Eubanks
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-01-10 00:00:00.000000000 Z
11
+ date: 2016-01-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -106,6 +106,7 @@ files:
106
106
  - Gemfile
107
107
  - Gemfile.lock
108
108
  - Guardfile
109
+ - README.md
109
110
  - Rakefile
110
111
  - json_api_ruby.gemspec
111
112
  - lib/json_api_ruby.rb
@@ -141,9 +142,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
141
142
  version: '0'
142
143
  required_rubygems_version: !ruby/object:Gem::Requirement
143
144
  requirements:
144
- - - ">"
145
+ - - ">="
145
146
  - !ruby/object:Gem::Version
146
- version: 1.3.1
147
+ version: '0'
147
148
  requirements: []
148
149
  rubyforge_project:
149
150
  rubygems_version: 2.4.5.1