rest_my_case 1.9.4 → 1.10.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: 2724bb942b9966ddca16a313cd92a21159752885
4
- data.tar.gz: 2700a310ca133fadaca6673e2be21cdacea3a938
3
+ metadata.gz: 2c8fcfcdfef03f7d258b3ccd34e73c919adfb285
4
+ data.tar.gz: b647c1d6d9a57071f36480a0cc410df2ad0a8b80
5
5
  SHA512:
6
- metadata.gz: d80ea3c0c01816e07a9a7f0bb7ca3d54796a14cc07e42635945fee101435a873f9c44356c271392ee0d9fb374a65f566fe68c14cecd6809caf4868074d0060f1
7
- data.tar.gz: bcdabecbadd83fe88bc1b7082852fcf678d9b990e07cd2f8f0f62d75e4ab04b0ecbe2b4bd57f1dd0a308c5416231ad6467fe84259ea1d0591d7af208d27c8058
6
+ metadata.gz: bff3902db329514d14c1957a5d0d0140e139a0e9b92cb2c0a32aed4cc962a3a6acca3b067813427855b7d564b65d83e3b9e7b13c168554ca6f3acad28d220117
7
+ data.tar.gz: 8dd1ffb6a3f9d7a2744667502260c0eac64ac951e63c12b62b983cc333348c8fdbe6b8a2556f45bd15d9b3ff1f65a2125fd70ea6a3107b8f0aed62a7f7dc2302
@@ -9,6 +9,10 @@ module RestMyCase
9
9
  Judge::Base, DefenseAttorney::Base, RestMyCase::Base, Context::Base
10
10
  end
11
11
 
12
+ def self.trial_court=(new_trial_court)
13
+ @trial_court = new_trial_court
14
+ end
15
+
12
16
  def self.depends(*use_case_classes)
13
17
  dependencies.push(*use_case_classes)
14
18
  end
@@ -78,12 +82,16 @@ module RestMyCase
78
82
  abort && fail(Errors::Abort)
79
83
  end
80
84
 
81
- def error(message = '')
82
- abort && context.errors.add(self.class.name, message)
85
+ def error(error_data = '')
86
+ error_data = { message: error_data } unless error_data.is_a?(Hash)
87
+
88
+ error_data[:class_name] = self.class.name
89
+
90
+ abort && context.errors.add(error_data)
83
91
  end
84
92
 
85
- def error!(message = '')
86
- error(message) && fail(Errors::Abort)
93
+ def error!(error_data = '')
94
+ error(error_data) && fail(Errors::Abort)
87
95
  end
88
96
 
89
97
  def skip
@@ -2,7 +2,7 @@ module RestMyCase
2
2
  module Context
3
3
  module Errors
4
4
 
5
- class Base < Hash
5
+ class Base < Array
6
6
 
7
7
  def initialize(context)
8
8
  super()
@@ -10,20 +10,8 @@ module RestMyCase
10
10
  @context = context
11
11
  end
12
12
 
13
- def add(class_name, message)
14
- self[class_name] ||= []
15
-
16
- self[class_name].push(message.to_s)
17
- end
18
-
19
- def messages
20
- self.values.flatten
21
- end
22
-
23
- def full_messages
24
- self.map do |class_name, messages|
25
- "#{class_name}: #{messages.join(', ')}"
26
- end
13
+ def add(error)
14
+ self.push(error)
27
15
  end
28
16
 
29
17
  end
@@ -4,12 +4,14 @@ module RestMyCase
4
4
 
5
5
  class Status < Base
6
6
 
7
- def add(error, message)
7
+ attr_reader :last_known_error
8
+
9
+ def add(error)
8
10
  super
9
11
 
10
- status = message.to_s.split(' - ')[0]
12
+ @context.status.send("#{error[:status]}!")
11
13
 
12
- @context.status.send("#{status}!")
14
+ @last_known_error = error
13
15
  end
14
16
 
15
17
  end
@@ -59,10 +59,17 @@ module RestMyCase
59
59
  RAILS_HTTP_STATUS[status.to_sym]
60
60
  end
61
61
 
62
- def error_message
63
- {
64
- message: errors.empty? ? 'unknown error' : errors.messages.join(', ')
65
- }
62
+ def error_response
63
+
64
+ if errors.last_known_error
65
+ response = errors.last_known_error.dup
66
+ response.delete :class_name
67
+ response[:http_status] = http_status
68
+
69
+ response
70
+ else
71
+ { message: 'unkown error' }
72
+ end
66
73
  end
67
74
 
68
75
  end
@@ -1,10 +1,11 @@
1
1
  module RestMyCase
2
2
 
3
- class HttpStatus < Status
3
+ module HttpStatus
4
4
 
5
- def self.trial_court
6
- @trial_court ||= Trial::Court.new \
7
- Judge::Base, DefenseAttorney::Base, Base, Context::HttpStatus
5
+ def self.included(parent_class)
6
+ parent_class.include Status
7
+
8
+ parent_class.trial_court.context_class = Context::HttpStatus
8
9
  end
9
10
 
10
11
  end
@@ -1,20 +1,25 @@
1
1
  module RestMyCase
2
2
 
3
- class Status < Base
3
+ module Status
4
4
 
5
- def self.trial_court
6
- @trial_court ||= Trial::Court.new \
7
- Judge::Base, DefenseAttorney::Base, Base, Context::Status
5
+ def self.included(parent_class)
6
+ parent_class.trial_court.context_class = Context::Status
8
7
  end
9
8
 
10
- context_reader :status
9
+ def status
10
+ context.status
11
+ end
12
+
13
+ def failure(status, error = nil)
14
+ error = { message: error } unless error.is_a?(Hash)
15
+
16
+ error[:status] = status
11
17
 
12
- def failure(status, message = nil)
13
- error(Helpers.blank?(message) ? "#{status}" : "#{status} - #{message}")
18
+ error(error)
14
19
  end
15
20
 
16
- def failure!(status, message = nil)
17
- failure(status, message) && fail(Errors::Abort)
21
+ def failure!(status, error = nil)
22
+ failure(status, error) && fail(Errors::Abort)
18
23
  end
19
24
 
20
25
  end
@@ -1,5 +1,5 @@
1
1
  module RestMyCase
2
2
 
3
- VERSION = '1.9.4'
3
+ VERSION = '1.10.1'
4
4
 
5
5
  end
@@ -107,8 +107,7 @@ describe RestMyCase::Base do
107
107
  end
108
108
 
109
109
  it "context should contain only one error" do
110
- expect(@context.errors).to match a_hash_including({"Perform::ValidateName"=>[""]})
111
- expect(@context.errors.full_messages).to eq ["Perform::ValidateName: "]
110
+ expect(@context.errors).to match a_hash_including({ message: '', class_name: "Perform::ValidateName" })
112
111
  end
113
112
 
114
113
  it "context prove that only the correct method have ran" do
@@ -130,8 +129,7 @@ describe RestMyCase::Base do
130
129
  end
131
130
 
132
131
  it "context should contain only one error" do
133
- expect(@context.errors).to match a_hash_including({"Perform::ValidateName"=>[""]})
134
- expect(@context.errors.full_messages).to eq ["Perform::ValidateName: "]
132
+ expect(@context.errors).to match a_hash_including({ message: '', class_name: "Perform::ValidateName" })
135
133
  end
136
134
 
137
135
  it "context prove that only the correct method have ran" do
@@ -232,8 +230,7 @@ describe RestMyCase::Base do
232
230
  end
233
231
 
234
232
  it "context should contain only 2 errors" do
235
- expect(@context.errors).to match a_hash_including({"Perform::ValidateName"=>[""], "Perform::ValidateBody"=>[""]})
236
- expect(@context.errors.full_messages).to eq ["Perform::ValidateName: ", "Perform::ValidateBody: "]
233
+ expect(@context.errors).to match a_hash_including({ message: '', class_name: "Perform::ValidateName"}, { message: '', class_name: "Perform::ValidateBody" })
237
234
  end
238
235
 
239
236
  it "context prove that only the correct method have ran" do
@@ -257,8 +254,7 @@ describe RestMyCase::Base do
257
254
  end
258
255
 
259
256
  it "context should contain only 2 errors" do
260
- expect(@context.errors).to match a_hash_including({"Perform::ValidateName"=>[""], "Perform::ValidateBody"=>[""]})
261
- expect(@context.errors.full_messages).to eq ["Perform::ValidateName: ", "Perform::ValidateBody: "]
257
+ expect(@context.errors).to match a_hash_including({ message: '', class_name: "Perform::ValidateName" }, { message: '', class_name: "Perform::ValidateBody" })
262
258
  end
263
259
 
264
260
  it "context prove that only the correct method have ran" do
@@ -293,8 +289,7 @@ describe RestMyCase::Base do
293
289
  end
294
290
 
295
291
  it "context should contain only 4 errors" do
296
- expect(@context.errors).to match a_hash_including({"Perform::BuildPost"=>[""], "Perform::ValidateName"=>[""], "Perform::ValidateBody"=>[""], "Perform::SavePost"=>[""]})
297
- expect(@context.errors.full_messages).to eq ["Perform::BuildPost: ", "Perform::ValidateName: ", "Perform::ValidateBody: ", "Perform::SavePost: "]
292
+ expect(@context.errors).to match a_hash_including({ message: '', class_name: "Perform::BuildPost" }, { message: '', class_name: "Perform::ValidateName" }, { message: '', class_name: "Perform::ValidateBody" }, { message: '', class_name: "Perform::SavePost" })
298
293
  end
299
294
 
300
295
  it "context prove that only the correct method have ran" do
@@ -3,7 +3,10 @@ require 'spec_helper'
3
3
  describe RestMyCase::HttpStatus do
4
4
 
5
5
  context "when status.not_found! is used" do
6
- NotFound = Class.new(RestMyCase::HttpStatus) { def perform; status.not_found!; end }
6
+ NotFound = Class.new(RestMyCase::Base) do
7
+ include RestMyCase::HttpStatus
8
+ def perform; status.not_found!; end
9
+ end
7
10
 
8
11
  before { @context = NotFound.perform }
9
12
 
@@ -11,13 +14,16 @@ describe RestMyCase::HttpStatus do
11
14
  expect(@context.http_status).to be 404
12
15
  end
13
16
 
14
- it "@context.error_message should only list the class's dependencies" do
15
- expect(@context.error_message).to match a_hash_including({:message=>"unknown error"})
17
+ it "@context.error_response should only list the class's dependencies" do
18
+ expect(@context.error_response).to match a_hash_including({ message: 'unkown error' })
16
19
  end
17
20
  end
18
21
 
19
22
  context "when failure(:unprocessable_entity) is used" do
20
- UnprocessableEntity = Class.new(RestMyCase::HttpStatus) { def perform; failure(:unprocessable_entity); end }
23
+ UnprocessableEntity = Class.new(RestMyCase::Base) do
24
+ include RestMyCase::HttpStatus
25
+ def perform; failure(:unprocessable_entity); end
26
+ end
21
27
 
22
28
  before { @context = UnprocessableEntity.perform }
23
29
 
@@ -25,22 +31,25 @@ describe RestMyCase::HttpStatus do
25
31
  expect(@context.http_status).to be 422
26
32
  end
27
33
 
28
- it "@context.error_message should only list the class's dependencies" do
29
- expect(@context.error_message).to match a_hash_including({:message=>"unprocessable_entity"})
34
+ it "@context.error_response should only list the class's dependencies" do
35
+ expect(@context.error_response).to match a_hash_including({ status: :unprocessable_entity, http_status: 422, message: nil })
30
36
  end
31
37
  end
32
38
 
33
- context "when failure!(:internal_server_error) is used" do
34
- InternalServerError = Class.new(RestMyCase::HttpStatus) { def perform; failure!(:internal_server_error, 'failed to save'); end }
39
+ context "when failure!(:service_unavailable) is used" do
40
+ InternalServerError = Class.new(RestMyCase::Base) do
41
+ include RestMyCase::HttpStatus
42
+ def perform; failure!(:service_unavailable, { code: 404, message: 'github could not found repo' }); end
43
+ end
35
44
 
36
45
  before { @context = InternalServerError.perform }
37
46
 
38
47
  it "@context.http_status should only list the class's dependencies" do
39
- expect(@context.http_status).to be 500
48
+ expect(@context.http_status).to be 503
40
49
  end
41
50
 
42
- it "@context.error_message should only list the class's dependencies" do
43
- expect(@context.error_message).to match a_hash_including({:message=>"internal_server_error - failed to save"})
51
+ it "@context.error_response should only list the class's dependencies" do
52
+ expect(@context.error_response).to match a_hash_including({ status: :service_unavailable, http_status: 503, code: 404, message: "github could not found repo" })
44
53
  end
45
54
  end
46
55
 
@@ -5,9 +5,14 @@ describe RestMyCase::Status do
5
5
  describe "context#status" do
6
6
 
7
7
  context "when status.not_found! is used" do
8
- TestCase1 = Class.new(described_class) { def perform; status.accepted!; end }
8
+ StatusTestCase1 = Class.new(RestMyCase::Base) do
9
+ include RestMyCase::Status
10
+ def perform
11
+ status.accepted!
12
+ end
13
+ end
9
14
 
10
- before { @context = TestCase1.perform }
15
+ before { @context = StatusTestCase1.perform }
11
16
 
12
17
  it "@context.status.accepted? should be true" do
13
18
  expect(@context.status.accepted?).to be true
@@ -19,50 +24,77 @@ describe RestMyCase::Status do
19
24
  end
20
25
 
21
26
  context "when error(:unprocessable_entity) is used" do
22
- TestCase2 = Class.new(described_class) do
27
+ StatusTestCase2 = Class.new(RestMyCase::Base) do
28
+ include RestMyCase::Status
23
29
  def perform
24
30
  error(:unprocessable_entity)
25
31
  context.next_line = true
26
32
  end
27
33
  end
28
34
 
29
- before { @context = TestCase2.perform }
35
+ before { @context = StatusTestCase2.perform }
30
36
 
31
- it "@context.status.unprocessable_entity? should be true" do
32
- expect(@context.status.unprocessable_entity?).to be true
37
+ it "@context.status.unprocessable_entity? should be false" do
38
+ expect(@context.status.unprocessable_entity?).to be false
33
39
  end
34
40
 
35
41
  it "@context.next_line should be true" do
36
42
  expect(@context.next_line).to be true
37
43
  end
38
44
 
39
- it "@context.status.ok? should be false" do
40
- expect(@context.status.ok?).to be false
45
+ it "@context.status.ok? should be true" do
46
+ expect(@context.status.ok?).to be true
41
47
  end
42
48
 
43
49
  it "context's errors should have a proper message" do
44
- message = "unprocessable_entity"
45
-
46
- expect(@context.errors["TestCase2"]).to eq [message]
47
- expect(@context.errors.messages).to eq [message]
50
+ expect(@context.errors).to match [a_hash_including({ message: :unprocessable_entity, class_name: "StatusTestCase2" })]
48
51
  end
49
52
  end
50
53
 
51
54
  context "when error!(:internal_server_error) is used" do
52
- TestCase3 = Class.new(described_class) do
55
+ StatusTestCase3 = Class.new(RestMyCase::Base) do
56
+ include RestMyCase::Status
53
57
  def perform
54
58
  error!(:internal_server_error)
55
59
  context.next_line = true
56
60
  end
57
61
  end
58
62
 
59
- before { @context = TestCase3.perform }
63
+ before { @context = StatusTestCase3.perform }
64
+
65
+ it "@context.status.internal_server_error? should be false" do
66
+ expect(@context.status.internal_server_error?).to be false
67
+ end
68
+
69
+ it "@context.next_line should raise an error" do
70
+ expect { context.next_line }.to raise_error
71
+ end
72
+
73
+ it "@context.status.ok? should be true" do
74
+ expect(@context.status.ok?).to be true
75
+ end
76
+
77
+ it "context's errors should have a proper message" do
78
+ expect(@context.errors).to match [a_hash_including({ message: :internal_server_error, class_name: "StatusTestCase3" })]
79
+ end
80
+ end
81
+
82
+ context "when error!(status: :internal_server_error, message: 'something bad') is used" do
83
+ StatusTestCase4 = Class.new(RestMyCase::Base) do
84
+ include RestMyCase::Status
85
+ def perform
86
+ error!(status: :internal_server_error, message: 'something bad', yada: true)
87
+ context.next_line = true
88
+ end
89
+ end
90
+
91
+ before { @context = StatusTestCase4.perform }
60
92
 
61
93
  it "@context.status.internal_server_error? should be true" do
62
94
  expect(@context.status.internal_server_error?).to be true
63
95
  end
64
96
 
65
- it "@context.next_line should be true" do
97
+ it "@context.next_line should raise an error" do
66
98
  expect { context.next_line }.to raise_error
67
99
  end
68
100
 
@@ -71,22 +103,20 @@ describe RestMyCase::Status do
71
103
  end
72
104
 
73
105
  it "context's errors should have a proper message" do
74
- message = "internal_server_error"
75
-
76
- expect(@context.errors["TestCase3"]).to eq [message]
77
- expect(@context.errors.messages).to eq [message]
106
+ expect(@context.errors).to match [a_hash_including({ status: :internal_server_error, message: 'something bad', class_name: "StatusTestCase4", yada: true })]
78
107
  end
79
108
  end
80
109
 
81
110
  context "when failure(:unprocessable_entity) is used" do
82
- TestCase4 = Class.new(described_class) do
111
+ StatusTestCase5 = Class.new(RestMyCase::Base) do
112
+ include RestMyCase::Status
83
113
  def perform
84
114
  failure(:unprocessable_entity, 'invalid id')
85
115
  context.next_line = true
86
116
  end
87
117
  end
88
118
 
89
- before { @context = TestCase4.perform }
119
+ before { @context = StatusTestCase5.perform }
90
120
 
91
121
  it "@context.status.unprocessable_entity? should be true" do
92
122
  expect(@context.status.unprocessable_entity?).to be true
@@ -101,22 +131,20 @@ describe RestMyCase::Status do
101
131
  end
102
132
 
103
133
  it "context's errors should have a proper message" do
104
- message = "unprocessable_entity - invalid id"
105
-
106
- expect(@context.errors["TestCase4"]).to eq [message]
107
- expect(@context.errors.messages).to eq [message]
134
+ expect(@context.errors).to match [a_hash_including({ status: :unprocessable_entity, message: 'invalid id', class_name: "StatusTestCase5" })]
108
135
  end
109
136
  end
110
137
 
111
138
  context "when failure!(:internal_server_error) is used" do
112
- TestCase5 = Class.new(described_class) do
139
+ StatusTestCase6 = Class.new(RestMyCase::Base) do
140
+ include RestMyCase::Status
113
141
  def perform
114
142
  failure!(:internal_server_error, 'while saving the resource')
115
143
  context.next_line = true
116
144
  end
117
145
  end
118
146
 
119
- before { @context = TestCase5.perform }
147
+ before { @context = StatusTestCase6.perform }
120
148
 
121
149
  it "@context.status.internal_server_error? should be true" do
122
150
  expect(@context.status.internal_server_error?).to be true
@@ -131,10 +159,7 @@ describe RestMyCase::Status do
131
159
  end
132
160
 
133
161
  it "context's errors should have a proper message" do
134
- message = "internal_server_error - while saving the resource"
135
-
136
- expect(@context.errors["TestCase5"]).to eq [message]
137
- expect(@context.errors.messages).to eq [message]
162
+ expect(@context.errors).to match [a_hash_including({ status: :internal_server_error, message: 'while saving the resource', class_name: "StatusTestCase6" })]
138
163
  end
139
164
  end
140
165
 
@@ -12,7 +12,7 @@ describe RestMyCase::Validator do
12
12
  end
13
13
 
14
14
  it "@context.errors should reflect the fact that no target is defined" do
15
- expect(@context.errors).to a_hash_including({"RestMyCase::Validator"=>["no target to validate!"]})
15
+ expect(@context.errors).to a_hash_including({ message: "no target to validate!", class_name: "RestMyCase::Validator" })
16
16
  end
17
17
  end
18
18
 
@@ -28,7 +28,7 @@ describe RestMyCase::Validator do
28
28
  end
29
29
 
30
30
  it "@context.errors should include the unprocessable_entity error" do
31
- expect(@context.errors).to a_hash_including({"CustomValidator"=>["unprocessable_entity"]})
31
+ expect(@context.errors).to a_hash_including({ message: "unprocessable_entity" , class_name: "CustomValidator" })
32
32
  end
33
33
 
34
34
  it "@post.errors should mention the bad phone_number error" do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rest_my_case
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.9.4
4
+ version: 1.10.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - goncalvesjoao
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-07-16 00:00:00.000000000 Z
11
+ date: 2015-07-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pry