shamu 0.0.4 → 0.0.5

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.
@@ -0,0 +1,153 @@
1
+ require "rails_helper"
2
+
3
+ module JsonApiResponderSpec
4
+
5
+ class Resource < Shamu::Entities::Entity
6
+ attribute :id
7
+ attribute :name
8
+ end
9
+
10
+ class Responder < ActionController::Responder
11
+ include Shamu::Rails::JsonApiResponder
12
+ end
13
+
14
+ class ResourcesController < ActionController::Base
15
+ include Shamu::Rails::JsonApi
16
+
17
+ respond_to :json, :json_api
18
+ self.responder = Responder
19
+
20
+ def json_api_responder_spec_resource_url( * )
21
+ "/go/here"
22
+ end
23
+ end
24
+
25
+ module Resources
26
+ class ResourcePresenter < Shamu::JsonApi::Presenter
27
+ def present
28
+ builder.identifier :resource, resource.id
29
+ builder.attribute name: resource.name
30
+ end
31
+ end
32
+ end
33
+
34
+ end
35
+
36
+ describe JsonApiResponderSpec::ResourcesController, type: :controller do
37
+ controller JsonApiResponderSpec::ResourcesController do
38
+ def show
39
+ resource = resources.first
40
+ respond_with resource
41
+ end
42
+
43
+ def create
44
+ resource = resources.first
45
+ respond_with resource
46
+ end
47
+
48
+ def update
49
+ resource = resources.first
50
+ respond_with resource
51
+ end
52
+
53
+ def index
54
+ respond_with resources
55
+ end
56
+
57
+ def resources
58
+ end
59
+ end
60
+
61
+ let( :resource ) { JsonApiResponderSpec::Resource.new( id: 562, name: "Example" ) }
62
+ let( :resources ) { [ resource ] }
63
+ let( :body ) { JSON.load( response.body, nil, symbolize_names: true ) }
64
+
65
+ before( :each ) do
66
+ allow( controller ).to receive( :_routes ).and_return @routes
67
+ allow( controller ).to receive( :resources ).and_return resources
68
+ end
69
+
70
+ describe "#show" do
71
+ it "has JSON content_type" do
72
+ get :show, id: 1
73
+ expect( response.content_type ).to eq Shamu::JsonApi::MIME_TYPE
74
+ end
75
+
76
+ it "renders JSON API response" do
77
+ get :show, id: 1
78
+ expect( body ).to include data: hash_including( id: resource.id.to_s )
79
+ end
80
+
81
+ it "renders errors on validation failure" do
82
+ errors = ActiveModel::Errors.new( resource )
83
+ errors.add :name, "can't be blank"
84
+ allow( resource ).to receive( :errors ).and_return errors
85
+ allow( resource ).to receive( :valid? ).and_return false
86
+
87
+ get :show, id: 1
88
+ expect( body ).to include :errors
89
+ end
90
+ end
91
+
92
+ describe "#create" do
93
+ it "has JSON content_type" do
94
+ post :create
95
+ expect( response.content_type ).to eq Shamu::JsonApi::MIME_TYPE
96
+ end
97
+
98
+ it "includes location header" do
99
+ post :create
100
+ expect( response.location ).to be_present
101
+ end
102
+
103
+ it "returns 201 status code" do
104
+ post :create
105
+ expect( response.status ).to eq 201
106
+ end
107
+
108
+ it "includes the json entity" do
109
+ post :create
110
+ expect( body ).to include data: hash_including( id: resource.id.to_s )
111
+ end
112
+ end
113
+
114
+ describe "#update" do
115
+ it "has JSON content_type" do
116
+ put :update, id: 1
117
+ expect( response.content_type ).to eq Shamu::JsonApi::MIME_TYPE
118
+ end
119
+
120
+ it "includes location header" do
121
+ post :update, id: 1
122
+ expect( response.location ).to be_present
123
+ end
124
+
125
+ it "returns 200 status code" do
126
+ put :update, id: 1
127
+ expect( response.status ).to eq 200
128
+ end
129
+
130
+ it "includes the json entity" do
131
+ put :update, id: 1
132
+ expect( body ).to include data: hash_including( id: resource.id.to_s )
133
+ end
134
+ end
135
+
136
+ describe "#index" do
137
+ it "has JSON content_type" do
138
+ get :index
139
+ expect( response.content_type ).to eq Shamu::JsonApi::MIME_TYPE
140
+ end
141
+
142
+ it "returns 200 status code" do
143
+ get :index
144
+ expect( response.status ).to eq 200
145
+ end
146
+
147
+ it "includes the json entity" do
148
+ get :index
149
+ expect( body ).to include data: include( hash_including( id: resource.id.to_s ) )
150
+ end
151
+ end
152
+
153
+ end
@@ -8,11 +8,12 @@ module JsonApiControllerSpec
8
8
  end
9
9
 
10
10
  class ResourcesController < ActionController::Base
11
+ include Shamu::Rails::JsonApi
11
12
  end
12
13
 
13
14
  module Resources
14
- class ResourcePresenter
15
- def present( resource, builder )
15
+ class ResourcePresenter < Shamu::JsonApi::Presenter
16
+ def present
16
17
  builder.identifier :resource, resource.id
17
18
  builder.attribute name: resource.name
18
19
  end
@@ -24,26 +25,28 @@ end
24
25
  describe JsonApiControllerSpec::ResourcesController, type: :controller do
25
26
  controller JsonApiControllerSpec::ResourcesController do
26
27
  def show
27
- resource = JsonApiControllerSpec::Resource.new id: 562, name: "Example"
28
- json_api resource: resource
28
+ resource = resources.first
29
+ render json: json_resource( resource )
29
30
  end
30
31
 
31
32
  def index
32
- json_api collection: resources
33
+ render json: json_collection( resources )
33
34
  end
34
35
 
35
36
  def nope
36
- json_api error: StandardError.new( "Nope" )
37
+ render json: json_error( StandardError.new( "Nope" ) )
37
38
  end
38
39
 
39
40
  def resources
40
- @resources ||= [ JsonApiControllerSpec::Resource.new( id: 562, name: "Example" ) ]
41
41
  end
42
-
43
42
  end
44
43
 
44
+ let( :resource ) { JsonApiControllerSpec::Resource.new( id: 562, name: "Example" ) }
45
+ let( :resources ) { [ resource ] }
46
+
45
47
  before( :each ) do
46
48
  allow( controller ).to receive( :_routes ).and_return @routes
49
+ allow( controller ).to receive( :resources ).and_return resources
47
50
  end
48
51
 
49
52
  describe "#json_resource" do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shamu
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paul Alexander
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-04-11 00:00:00.000000000 Z
11
+ date: 2016-04-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -272,6 +272,10 @@ files:
272
272
  - lib/shamu/json_api.rb
273
273
  - lib/shamu/json_api/README.md
274
274
  - lib/shamu/json_api/base_builder.rb
275
+ - lib/shamu/json_api/builder_methods.rb
276
+ - lib/shamu/json_api/builder_methods/identifier.rb
277
+ - lib/shamu/json_api/builder_methods/link.rb
278
+ - lib/shamu/json_api/builder_methods/meta.rb
275
279
  - lib/shamu/json_api/context.rb
276
280
  - lib/shamu/json_api/error.rb
277
281
  - lib/shamu/json_api/error_builder.rb
@@ -292,6 +296,7 @@ files:
292
296
  - lib/shamu/rails/entity.rb
293
297
  - lib/shamu/rails/features.rb
294
298
  - lib/shamu/rails/json_api.rb
299
+ - lib/shamu/rails/json_api_responder.rb
295
300
  - lib/shamu/rails/railtie.rb
296
301
  - lib/shamu/rspec.rb
297
302
  - lib/shamu/rspec/matchers.rb
@@ -389,6 +394,7 @@ files:
389
394
  - spec/lib/shamu/rails/entity_spec.rb
390
395
  - spec/lib/shamu/rails/features.yml
391
396
  - spec/lib/shamu/rails/features_spec.rb
397
+ - spec/lib/shamu/rails/json_api_responder_spec.rb
392
398
  - spec/lib/shamu/rails/json_api_spec.rb
393
399
  - spec/lib/shamu/security/active_record_policy_spec.rb
394
400
  - spec/lib/shamu/security/hashed_value_spec.rb
@@ -432,7 +438,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
432
438
  version: '0'
433
439
  requirements: []
434
440
  rubyforge_project:
435
- rubygems_version: 2.5.0
441
+ rubygems_version: 2.5.1
436
442
  signing_key:
437
443
  specification_version: 4
438
444
  summary: Have a whale of a good time adding Service Oriented Architecture to your
@@ -500,6 +506,7 @@ test_files:
500
506
  - spec/lib/shamu/rails/entity_spec.rb
501
507
  - spec/lib/shamu/rails/features.yml
502
508
  - spec/lib/shamu/rails/features_spec.rb
509
+ - spec/lib/shamu/rails/json_api_responder_spec.rb
503
510
  - spec/lib/shamu/rails/json_api_spec.rb
504
511
  - spec/lib/shamu/security/active_record_policy_spec.rb
505
512
  - spec/lib/shamu/security/hashed_value_spec.rb