jsonapi_for_rails 0.2.0.pre → 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
  SHA1:
3
- metadata.gz: da54359b12e07d61046ce9a96b2b0c8edadb2af2
4
- data.tar.gz: bef39399515bd6b7b6a10d5466d26d45c60807ec
3
+ metadata.gz: 69b79b4230ffd98fd6046ddcc70ef3469b39fafc
4
+ data.tar.gz: 66864910b74b564e07f9c18e25c346c665656d5a
5
5
  SHA512:
6
- metadata.gz: 5e422031d14cea9d91d30591ceafb5616f868b6cd4471c34a705a7ae7837c9aabc4c74acb6627efaa78f7e311b7a6ec640d9b35fe2533ccd419fc1d1d1acf34a
7
- data.tar.gz: 5750ba36ebe04753855d98914a1fa700f23db4da0541a97d7551b9821c89713a256264f2c9d34fc0400d8a4c67c7f49e47a32d7b187db4e0f933e41692bbe0ee
6
+ metadata.gz: 0b7a960d5e6778940a75a9be27c5af70424ea71264b0032c8fa5deded47db55670642b7c88fbf951abd451c003ec12cac29c65889fcf0fdf84b4753db14a946c
7
+ data.tar.gz: d9e63036c68538bdf502a677f7ca6452a3293abb5b060f29e165a5976d38d26e849f07dbf143a543e067a9a90953f53b76f7952e4a01ebffe8849a0cd7269887
checksums.yaml.gz.sig CHANGED
Binary file
data/README.md CHANGED
@@ -34,7 +34,7 @@ $
34
34
  $ # Check the used version
35
35
  $ bin/rails console
36
36
  irb(main):001:0> JsonapiForRails::VERSION
37
- => "0.2.0.pre"
37
+ => "0.2.0"
38
38
  irb(main):002:0> exit
39
39
  $
40
40
  ```
@@ -64,7 +64,10 @@ Then enable JSONAPI in a parent class of your API controllers.
64
64
  class ApplicationController < ActionController::Base # or ActionController::API
65
65
 
66
66
  # Enable JSONAPI
67
- acts_as_jsonapi_resources
67
+ acts_as_jsonapi_resources(
68
+ # links: false,
69
+ # content_negotiation: false
70
+ )
68
71
 
69
72
  # ...
70
73
  end
@@ -72,9 +75,11 @@ end
72
75
 
73
76
  `acts_as_jsonapi_resources` accepts the following keyword arguments:
74
77
 
75
- * `content_negotiation`: Set this to `false` to disable
76
- [content negotiation](http://jsonapi.org/format/1.0/#content-negotiation).
77
- Default value is `true`. Usage example: `acts_as_jsonapi_resources content_negotiation: false`
78
+ * `links`: Setting this to `false` disables
79
+ [link generation](http://jsonapi.org/format/1.0/#document-links),
80
+ and speeds up your API. The default value is `true`.
81
+ * `content_negotiation`: Setting this to `false` disables
82
+ [content negotiation](http://jsonapi.org/format/1.0/#content-negotiation). Again, this helps speed up your API, but at the expense of making your API non-JSONAPI-compliant, if only just). The default value is `true`.
78
83
 
79
84
  If only some of your controllers are JSONAPI controllers, then create a parent controller for only those controllers, and enable JSONAPI inside that controller rather than `ApplicationController`.
80
85
 
@@ -134,7 +139,7 @@ After populating your database and launching the built-in Rails server with the
134
139
 
135
140
  ```bash
136
141
  $ # Get the list of articles
137
- $ # (the returned HTTP response body is short and terse, but it is prettified here for legibility)
142
+ $ # (the returned HTTP response body is short and terse, but is prettified here for legibility)
138
143
  $ curl 'http://localhost:3000/api/v1/articles'
139
144
  {
140
145
  "data": [
@@ -146,7 +151,10 @@ $ curl 'http://localhost:3000/api/v1/articles'
146
151
  "type": "articles",
147
152
  "id": "994552601"
148
153
  }
149
- ]
154
+ ],
155
+ "links": {
156
+ "self": "/api/v1/articles"
157
+ }
150
158
  }
151
159
  $ # Get an article
152
160
  $ curl 'http://localhost:3000/api/v1/articles/618037523'
@@ -167,6 +175,9 @@ $ curl 'http://localhost:3000/api/v1/articles/618037523'
167
175
  "id": "1023487079"
168
176
  }
169
177
  }
178
+ },
179
+ "links": {
180
+ "self": "/api/v1/articles/618037523"
170
181
  }
171
182
  }
172
183
  }
@@ -186,6 +197,9 @@ $ curl 'http://localhost:3000/api/v1/articles/618037523?filter%5Barticles%5D=tit
186
197
  "id": "1023487079"
187
198
  }
188
199
  }
200
+ },
201
+ "links": {
202
+ "self": "/api/v1/articles/618037523"
189
203
  }
190
204
  },
191
205
  "include": [
@@ -197,6 +211,9 @@ $ curl 'http://localhost:3000/api/v1/articles/618037523?filter%5Barticles%5D=tit
197
211
  "name": "Jill T..."
198
212
  },
199
213
  "relationships": {
214
+ },
215
+ "links": {
216
+ "self": "/api/v1/authors/1023487079"
200
217
  }
201
218
  }
202
219
  }
@@ -268,8 +285,12 @@ class ArticlesController < JsonapiResourcesController
268
285
 
269
286
  def index
270
287
  # These model-related utility methods are available inside all action methods.
271
- jsonapi_model_class # => Article
272
- jsonapi_model_type # => :articles
288
+ jsonapi_model_class # => Article
289
+ jsonapi_model_type # => :articles
290
+
291
+ # @jsonapi_links indicates whether links should be included in response documents.
292
+ # It is available inside all action methods.
293
+ @jsonapi_links # => true
273
294
 
274
295
  # ...
275
296
  end
@@ -310,7 +331,8 @@ The internal architecture is sound. Test coverage is currently being bulked up u
310
331
 
311
332
  Feature support roundup:
312
333
 
313
- * [Content negotiation](http://jsonapi.org/format/1.0/#content-negotiation) is not implemented.
334
+ * [Content negotiation](http://jsonapi.org/format/1.0/#content-negotiation) is implemented and enabled by default, but can be disabled.
335
+ * [Link generation](http://jsonapi.org/format/1.0/#document-links) is implemented and enabled by default, but can be disabled.
314
336
  * [Inclusion of related resources](http://jsonapi.org/format/1.0/#fetching-includes) is currently only implemented for requests that return a single resource, and relationship paths are not supported.
315
337
  * [Sparse fieldsets](http://jsonapi.org/format/1.0/#fetching-sparse-fieldsets) is currently only implemented for requests that return a single resource.
316
338
  * [Sorting](http://jsonapi.org/format/1.0/#fetching-sorting) is currently not implemented.
@@ -17,6 +17,16 @@ module JsonapiForRails::Controller
17
17
  id: record.id.to_s
18
18
  }
19
19
  end
20
+
21
+ # Links
22
+ if @jsonapi_links
23
+ @json[:links] = {
24
+ self: self.send(
25
+ "#{jsonapi_model_type}_path" # TODO: factor out
26
+ )
27
+ }
28
+ end
29
+
20
30
  render_json @json
21
31
  end
22
32
 
@@ -72,9 +82,30 @@ module JsonapiForRails::Controller
72
82
  end
73
83
 
74
84
  def show
85
+ # Attributes and relationships
75
86
  @json = @jsonapi_record.to_jsonapi_hash(
76
- @jsonapi_sparse_fieldsets[jsonapi_model_type]
87
+ sparse_fieldset: @jsonapi_sparse_fieldsets[jsonapi_model_type]
77
88
  )
89
+
90
+ # Links
91
+ if @jsonapi_links
92
+
93
+ # Current resource
94
+ @json[:data][:links] = {
95
+ self: self.send(
96
+ "#{jsonapi_model_type.to_s.singularize}_path", # TODO: factor out
97
+ @jsonapi_record.id
98
+ )
99
+ }
100
+
101
+ # Related resources
102
+ @json[:data][:relationships].each do |rel_name, rel|
103
+ rel[:links] = {
104
+ self: "#{@json[:data][:links][:self]}/relationships/#{rel_name}"
105
+ }
106
+ end
107
+ end
108
+
78
109
  #$stderr.puts "#{@json}"
79
110
 
80
111
  # Include resources
@@ -98,9 +129,22 @@ module JsonapiForRails::Controller
98
129
  r = klass.find_by_id r[:id]
99
130
  next unless r
100
131
 
101
- @json[:include] << r.to_jsonapi_hash(
102
- @jsonapi_sparse_fieldsets[type]
132
+ # Attributes and relationships
133
+ r = r.to_jsonapi_hash(
134
+ sparse_fieldset: @jsonapi_sparse_fieldsets[type]
103
135
  )
136
+
137
+ # Links
138
+ if @jsonapi_links
139
+ r[:links] = {
140
+ self: self.send(
141
+ "#{r[:data][:type].to_s.singularize}_path", # TODO: factor out
142
+ r[:data][:id]
143
+ )
144
+ }
145
+ end
146
+
147
+ @json[:include] << r
104
148
  end
105
149
  end
106
150
  end
@@ -29,6 +29,18 @@ module JsonapiForRails::Controller
29
29
  end
30
30
  @json = {data: @json}
31
31
 
32
+ # Links
33
+ if @jsonapi_links
34
+ record_path = self.send(
35
+ "#{jsonapi_model_type.to_s.singularize}_path",
36
+ @jsonapi_record.id
37
+ )
38
+
39
+ @json[:links] = {
40
+ self: "#{record_path}/relationships/#{@jsonapi_relationship[:definition][:name]}"
41
+ }
42
+ end
43
+
32
44
  render_json @json
33
45
  end
34
46
 
@@ -52,6 +64,8 @@ module JsonapiForRails::Controller
52
64
 
53
65
  @jsonapi_record.send :"#{@jsonapi_relationship[:definition][:name]}=", related
54
66
  @jsonapi_record.save
67
+
68
+ #self.send :relationship_show
55
69
  end
56
70
 
57
71
  # POST for to-many relations only
@@ -75,6 +89,8 @@ module JsonapiForRails::Controller
75
89
  records.each do |record|
76
90
  @jsonapi_record.send(@jsonapi_relationship[:definition][:name]) << record
77
91
  end
92
+
93
+ #self.send :relationship_show
78
94
  end
79
95
 
80
96
  # DELETE for to-many relations only
@@ -99,6 +115,8 @@ module JsonapiForRails::Controller
99
115
  @jsonapi_record.send(@jsonapi_relationship[:definition][:name]).delete record
100
116
  end
101
117
  end
118
+
119
+ #self.send :relationship_show
102
120
  end
103
121
 
104
122
  def self.run_macros receiver
@@ -0,0 +1,29 @@
1
+ module JsonapiForRails::Controller
2
+
3
+ module BeforeActions
4
+ module Links
5
+
6
+ def self.included receiver
7
+ receiver.send :include, InstanceMethods
8
+ run_macros receiver
9
+ end
10
+
11
+ def self.run_macros receiver
12
+ receiver.instance_exec do
13
+ before_action :jsonapi_links
14
+ private :jsonapi_links
15
+ end
16
+ end
17
+
18
+ module InstanceMethods
19
+
20
+ # Enable links in returned documents
21
+ def jsonapi_links
22
+ @jsonapi_links = true
23
+ end
24
+
25
+ end
26
+ end
27
+ end
28
+
29
+ end
@@ -1,5 +1,6 @@
1
1
  require "jsonapi_for_rails/controller/utils/model"
2
2
  require "jsonapi_for_rails/controller/utils/render"
3
+ require "jsonapi_for_rails/controller/before_actions/links"
3
4
  require "jsonapi_for_rails/controller/before_actions/content_negotiation"
4
5
  require "jsonapi_for_rails/controller/before_actions/sparse_fieldsets"
5
6
  require "jsonapi_for_rails/controller/before_actions/include"
@@ -17,11 +18,12 @@ module JsonapiForRails::Controller
17
18
  end
18
19
 
19
20
  class_methods do
20
- def acts_as_jsonapi_resources content_negotiation: true #, model: nil
21
+ def acts_as_jsonapi_resources links: true, content_negotiation: true #, model: nil
21
22
  #$stderr.puts "JsonapiForRails::Controller macro called from #{self}:\n acts_as_jsonapi_resources(model: #{model or 'nil'})"
22
23
 
23
24
  include JsonapiForRails::Controller::Utils::Model
24
25
  include JsonapiForRails::Controller::Utils::Render
26
+ include JsonapiForRails::Controller::BeforeActions::Links if links
25
27
  include JsonapiForRails::Controller::BeforeActions::ContentNegotiation if content_negotiation
26
28
  include JsonapiForRails::Controller::BeforeActions::SparseFieldsets
27
29
  include JsonapiForRails::Controller::BeforeActions::Include
@@ -6,7 +6,7 @@ module JsonapiForRails::Model
6
6
 
7
7
  # Define instance methods
8
8
  class_exec do
9
- def to_jsonapi_hash sparse_fieldset=nil
9
+ def to_jsonapi_hash sparse_fieldset: nil
10
10
  #$stderr.puts "JsonapiForRails::Controller::Actions::Object#show called"
11
11
 
12
12
  # attributes
@@ -1,4 +1,3 @@
1
1
  module JsonapiForRails
2
- VERSION = '0.2.0.pre'
3
- # implement content negotiation (http://jsonapi.org/format/1.0/#content-negotiation), add content_negotiation keyword argument to acts_as_jasonapi_resources
2
+ VERSION = '0.2.0'
4
3
  end
@@ -1,13 +1,12 @@
1
1
 
2
- # TODO: content negotiation tests
2
+ # TODO: README: add instructions for using sparse fieldsets for setting the
3
+ # set of attributes to return (ignore binaries etc)
3
4
  # TODO: apply sparse fieldsets _after_ including related resources
4
5
  # TODO: Location header
5
- # TODO: add 'links' to documents
6
6
  # TODO: return conformant HTTP status codes
7
- # TODO: options for 'acts_as_jsonapi_resources'? example: {content_negotiation: false}
8
7
  # TODO: adding optional members to documents ('jsonapi', 'meta')
9
8
  # TODO: conformant and rich 'errors' member
10
- # TODO: should included resources list their relationships (which links to primary object)?
9
+ # TODO: included resource objects must not contain a relationship that links back to the primary object
11
10
  # TODO: do not support Client-Generated IDs?
12
11
 
13
12
  # TODO: README.md: double-check the installation instructions
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsonapi_for_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0.pre
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Doga Armangil
@@ -31,7 +31,7 @@ cert_chain:
31
31
  DcNXARPdnPlz0VQedZo89pKjngah8Nur01/wD97Q18Mr/av3H4Bg1sTF+RZAplU3
32
32
  RX+xYPJjPMyjrD73KZ5UNtXo5jOP1T85SqXOl6ICfedTVgZMk9mzK8+PkP1croHi
33
33
  -----END CERTIFICATE-----
34
- date: 2016-03-11 00:00:00.000000000 Z
34
+ date: 2016-03-12 00:00:00.000000000 Z
35
35
  dependencies:
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rails
@@ -84,6 +84,7 @@ files:
84
84
  - lib/jsonapi_for_rails/controller/actions/relationship.rb
85
85
  - lib/jsonapi_for_rails/controller/before_actions/content_negotiation.rb
86
86
  - lib/jsonapi_for_rails/controller/before_actions/include.rb
87
+ - lib/jsonapi_for_rails/controller/before_actions/links.rb
87
88
  - lib/jsonapi_for_rails/controller/before_actions/record.rb
88
89
  - lib/jsonapi_for_rails/controller/before_actions/relationship.rb
89
90
  - lib/jsonapi_for_rails/controller/before_actions/sparse_fieldsets.rb
@@ -107,9 +108,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
107
108
  version: '2.0'
108
109
  required_rubygems_version: !ruby/object:Gem::Requirement
109
110
  requirements:
110
- - - ">"
111
+ - - ">="
111
112
  - !ruby/object:Gem::Version
112
- version: 1.3.1
113
+ version: '0'
113
114
  requirements: []
114
115
  rubyforge_project:
115
116
  rubygems_version: 2.5.1
metadata.gz.sig CHANGED
Binary file