exceptions-resource 0.0.2p04 → 0.0.2p07
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/exceptions/base.rb +24 -26
- data/lib/exceptions/model.rb +68 -72
- data/lib/exceptions/resource.rb +22 -24
- data/lib/exceptions/simple.rb +22 -24
- data/lib/exceptions/unauthorized_application.rb +20 -24
- data/lib/exceptions/version.rb +1 -1
- data/lib/exceptions.rb +203 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 565b64cc3de39009cc01639f072387b43952d8d6
|
4
|
+
data.tar.gz: 9dfa0a1cd9a56727b0960d2d36b113ec58f9b629
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2c9d40712df39460304f7f754617a7d145970028c978e5a0b8f03a9b2e664d0524a44c7650d34d1afdf377104ff891f9eb5a2ae4ce0a2b336b7aa6ae18501763
|
7
|
+
data.tar.gz: 9fd2031d9e50661c069d61f723a082d6e73df1cc8f0eb203e7ad0b506ee77973d679713d27a27cf96148de23f42b4a8bfaece09278602ecd55c8fa75a829dd14
|
data/lib/exceptions/base.rb
CHANGED
@@ -1,31 +1,29 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
attr_accessor :object, :type
|
1
|
+
class Exceptions::Base < StandardError
|
2
|
+
attr_accessor :object, :type
|
4
3
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
# standard error for Models
|
12
|
-
# @param [ActiveRecord::Base] object
|
13
|
-
# @return [Exceptions::Base]
|
14
|
-
def self.build object
|
15
|
-
exception = new object
|
16
|
-
return exception
|
17
|
-
end
|
4
|
+
# starts a new instance with an object
|
5
|
+
# @param [Object] object
|
6
|
+
def initialize object
|
7
|
+
self.object = object
|
8
|
+
end
|
18
9
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
10
|
+
# standard error for Models
|
11
|
+
# @param [ActiveRecord::Base] object
|
12
|
+
# @return [Exceptions::Base]
|
13
|
+
def self.build object
|
14
|
+
exception = new object
|
15
|
+
return exception
|
16
|
+
end
|
24
17
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
end
|
18
|
+
# return if is as simple error
|
19
|
+
# @return [Boolean]
|
20
|
+
def simple?
|
21
|
+
self.class.name.demodulize.tableize.singularize == "simple"
|
30
22
|
end
|
23
|
+
|
24
|
+
# return if is as model error
|
25
|
+
# @return [Boolean]
|
26
|
+
def model?
|
27
|
+
self.class.name.demodulize.tableize.singularize == "model"
|
28
|
+
end
|
31
29
|
end
|
data/lib/exceptions/model.rb
CHANGED
@@ -1,89 +1,85 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
def error
|
8
|
-
self.is_nested? ? self.build_nested : self.build_normal
|
9
|
-
end
|
1
|
+
class Exceptions::Model < Exceptions::Base
|
2
|
+
# for model errors this method build a hash with all necessary information
|
3
|
+
# @return [String] json string
|
4
|
+
def error
|
5
|
+
self.is_nested? ? self.build_nested : self.build_normal
|
6
|
+
end
|
10
7
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
8
|
+
def build_nested
|
9
|
+
{
|
10
|
+
error: {
|
11
|
+
model: self.nested_model.camelcase,
|
12
|
+
field: "#{self.nested_model}[#{self.nested_attr}]",
|
13
|
+
attribute: self.nested_attr,
|
14
|
+
message: self.message,
|
15
|
+
full_message: "#{self.nested_attr_human} #{self.message}"
|
16
|
+
}
|
17
|
+
}
|
18
|
+
end
|
22
19
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
20
|
+
def build_normal
|
21
|
+
{
|
22
|
+
error: {
|
23
|
+
model: self.model.camelcase,
|
24
|
+
field: "#{self.model}[#{self.attribute}]",
|
25
|
+
attribute: self.attribute,
|
26
|
+
message: self.message,
|
27
|
+
full_message: "#{self.attribute_human} #{self.message}"
|
28
|
+
}
|
29
|
+
}
|
30
|
+
end
|
34
31
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
32
|
+
# return what model is
|
33
|
+
# @return [String]
|
34
|
+
def model
|
35
|
+
self.object.class.name.demodulize.tableize.singularize.downcase
|
36
|
+
end
|
40
37
|
|
41
|
-
|
42
|
-
|
43
|
-
|
38
|
+
def attribute
|
39
|
+
self.object.errors.first[0]
|
40
|
+
end
|
44
41
|
|
45
|
-
|
46
|
-
|
47
|
-
|
42
|
+
def model_human
|
43
|
+
self.object.class.model_name.human.demodulize.tableize.singularize.downcase
|
44
|
+
end
|
48
45
|
|
49
|
-
|
50
|
-
|
51
|
-
|
46
|
+
def attribute_human
|
47
|
+
self.object.class.human_attribute_name(self.object.errors.first[0])
|
48
|
+
end
|
52
49
|
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
50
|
+
# return the error message
|
51
|
+
# @return [String]
|
52
|
+
def message
|
53
|
+
"#{self.object.errors.first[1]}"
|
54
|
+
end
|
58
55
|
|
59
|
-
|
60
|
-
|
61
|
-
|
56
|
+
def status
|
57
|
+
422
|
58
|
+
end
|
62
59
|
|
63
|
-
|
64
|
-
|
60
|
+
def is_nested?
|
61
|
+
attribute = self.object.errors.first[0]
|
65
62
|
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
end
|
63
|
+
if self.object.errors.first[0].to_s.split(".").size > 1
|
64
|
+
self.object.respond_to?(attribute) ? false : true
|
65
|
+
else
|
66
|
+
false
|
71
67
|
end
|
68
|
+
end
|
72
69
|
|
73
|
-
|
74
|
-
|
75
|
-
|
70
|
+
def nested_model
|
71
|
+
self.object.errors.first[0].to_s.split(".").first.singularize.downcase
|
72
|
+
end
|
76
73
|
|
77
|
-
|
78
|
-
|
79
|
-
|
74
|
+
def nested_model_human
|
75
|
+
self.nested_model.capitalize.constantize.model_name.human
|
76
|
+
end
|
80
77
|
|
81
|
-
|
82
|
-
|
83
|
-
|
78
|
+
def nested_attr
|
79
|
+
self.object.errors.first[0].to_s.split(".").last
|
80
|
+
end
|
84
81
|
|
85
|
-
|
86
|
-
|
87
|
-
end
|
82
|
+
def nested_attr_human
|
83
|
+
self.nested_model.capitalize.constantize.human_attribute_name(self.nested_attr)
|
88
84
|
end
|
89
85
|
end
|
data/lib/exceptions/resource.rb
CHANGED
@@ -1,28 +1,26 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
{
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
end
|
1
|
+
class Exceptions::Resource < Exceptions::Base
|
2
|
+
# for standard errors this method build a hash
|
3
|
+
# @return [String] json string
|
4
|
+
def error
|
5
|
+
{
|
6
|
+
error: {
|
7
|
+
model: self.object["model"],
|
8
|
+
attribute: self.object["attribute"],
|
9
|
+
field: self.object["field"],
|
10
|
+
message: self.object["message"],
|
11
|
+
full_message: "#{self.object["attribute"]} #{self.object["message"]}"
|
12
|
+
}
|
13
|
+
}
|
14
|
+
end
|
16
15
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
16
|
+
# return the error message
|
17
|
+
# @return [String]
|
18
|
+
def message
|
19
|
+
self.error[:message]
|
20
|
+
end
|
22
21
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
end
|
22
|
+
# return the error status
|
23
|
+
def status
|
24
|
+
406
|
27
25
|
end
|
28
26
|
end
|
data/lib/exceptions/simple.rb
CHANGED
@@ -1,31 +1,29 @@
|
|
1
1
|
# represents the simple errors
|
2
|
-
|
3
|
-
|
4
|
-
attr_accessor :status
|
2
|
+
class Exceptions::Simple < Exceptions::Base
|
3
|
+
attr_accessor :status
|
5
4
|
|
6
|
-
|
7
|
-
# for standard errors this method build a hash
|
8
|
-
# @return [String] json string
|
9
|
-
def error
|
10
|
-
{
|
11
|
-
error: {
|
12
|
-
message: self.object[:message],
|
13
|
-
full_message: "#{self.object[:field]} #{self.object[:message]} ",
|
14
|
-
field: self.object[:field]
|
15
|
-
}
|
16
|
-
}
|
17
|
-
end
|
18
5
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
6
|
+
# for standard errors this method build a hash
|
7
|
+
# @return [String] json string
|
8
|
+
def error
|
9
|
+
{
|
10
|
+
error: {
|
11
|
+
message: self.object[:message],
|
12
|
+
full_message: "#{self.object[:field]} #{self.object[:message]} ",
|
13
|
+
field: self.object[:field]
|
14
|
+
}
|
15
|
+
}
|
16
|
+
end
|
24
17
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
18
|
+
# return the error message
|
19
|
+
# @return [String]
|
20
|
+
def message
|
21
|
+
self.object[:message]
|
22
|
+
end
|
29
23
|
|
24
|
+
# return the error status
|
25
|
+
def status
|
26
|
+
406
|
30
27
|
end
|
28
|
+
|
31
29
|
end
|
@@ -1,31 +1,27 @@
|
|
1
|
-
require 'base'
|
2
|
-
|
3
1
|
# represents the simple errors
|
4
|
-
|
5
|
-
|
6
|
-
attr_accessor :status
|
2
|
+
class Exceptions::UnauthorizedApplication < Exceptions::Base
|
3
|
+
attr_accessor :status
|
7
4
|
|
8
|
-
|
9
|
-
# for standard errors this method build a hash
|
10
|
-
# @return [String] json string
|
11
|
-
def error
|
12
|
-
{
|
13
|
-
error: {
|
14
|
-
message: self.object[:message]
|
15
|
-
}
|
16
|
-
}
|
17
|
-
end
|
18
5
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
6
|
+
# for standard errors this method build a hash
|
7
|
+
# @return [String] json string
|
8
|
+
def error
|
9
|
+
{
|
10
|
+
error: {
|
11
|
+
message: self.object[:message]
|
12
|
+
}
|
13
|
+
}
|
14
|
+
end
|
24
15
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
16
|
+
# return the error message
|
17
|
+
# @return [String]
|
18
|
+
def message
|
19
|
+
self.object[:message]
|
20
|
+
end
|
29
21
|
|
22
|
+
# return the error status
|
23
|
+
def status
|
24
|
+
401
|
30
25
|
end
|
26
|
+
|
31
27
|
end
|
data/lib/exceptions/version.rb
CHANGED
data/lib/exceptions.rb
CHANGED
@@ -1,4 +1,206 @@
|
|
1
1
|
module Exceptions
|
2
|
+
class Base < StandardError
|
3
|
+
attr_accessor :object, :type
|
4
|
+
|
5
|
+
# starts a new instance with an object
|
6
|
+
# @param [Object] object
|
7
|
+
def initialize object
|
8
|
+
self.object = object
|
9
|
+
end
|
10
|
+
|
11
|
+
# standard error for Models
|
12
|
+
# @param [ActiveRecord::Base] object
|
13
|
+
# @return [Exceptions::Base]
|
14
|
+
def self.build object
|
15
|
+
exception = new object
|
16
|
+
return exception
|
17
|
+
end
|
18
|
+
|
19
|
+
# return if is as simple error
|
20
|
+
# @return [Boolean]
|
21
|
+
def simple?
|
22
|
+
self.class.name.demodulize.tableize.singularize == "simple"
|
23
|
+
end
|
24
|
+
|
25
|
+
# return if is as model error
|
26
|
+
# @return [Boolean]
|
27
|
+
def model?
|
28
|
+
self.class.name.demodulize.tableize.singularize == "model"
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
class Model < Base
|
33
|
+
# for model errors this method build a hash with all necessary information
|
34
|
+
# @return [String] json string
|
35
|
+
def error
|
36
|
+
self.is_nested? ? self.build_nested : self.build_normal
|
37
|
+
end
|
38
|
+
|
39
|
+
def build_nested
|
40
|
+
{
|
41
|
+
error: {
|
42
|
+
model: self.nested_model.camelcase,
|
43
|
+
field: "#{self.nested_model}[#{self.nested_attr}]",
|
44
|
+
attribute: self.nested_attr,
|
45
|
+
message: self.message,
|
46
|
+
full_message: "#{self.nested_attr_human} #{self.message}"
|
47
|
+
}
|
48
|
+
}
|
49
|
+
end
|
50
|
+
|
51
|
+
def build_normal
|
52
|
+
{
|
53
|
+
error: {
|
54
|
+
model: self.model.camelcase,
|
55
|
+
field: "#{self.model}[#{self.attribute}]",
|
56
|
+
attribute: self.attribute,
|
57
|
+
message: self.message,
|
58
|
+
full_message: "#{self.attribute_human} #{self.message}"
|
59
|
+
}
|
60
|
+
}
|
61
|
+
end
|
62
|
+
|
63
|
+
# return what model is
|
64
|
+
# @return [String]
|
65
|
+
def model
|
66
|
+
self.object.class.name.demodulize.tableize.singularize.downcase
|
67
|
+
end
|
68
|
+
|
69
|
+
def attribute
|
70
|
+
self.object.errors.first[0]
|
71
|
+
end
|
72
|
+
|
73
|
+
def model_human
|
74
|
+
self.object.class.model_name.human.demodulize.tableize.singularize.downcase
|
75
|
+
end
|
76
|
+
|
77
|
+
def attribute_human
|
78
|
+
self.object.class.human_attribute_name(self.object.errors.first[0])
|
79
|
+
end
|
80
|
+
|
81
|
+
# return the error message
|
82
|
+
# @return [String]
|
83
|
+
def message
|
84
|
+
"#{self.object.errors.first[1]}"
|
85
|
+
end
|
86
|
+
|
87
|
+
def status
|
88
|
+
422
|
89
|
+
end
|
90
|
+
|
91
|
+
def is_nested?
|
92
|
+
attribute = self.object.errors.first[0]
|
93
|
+
|
94
|
+
if self.object.errors.first[0].to_s.split(".").size > 1
|
95
|
+
self.object.respond_to?(attribute) ? false : true
|
96
|
+
else
|
97
|
+
false
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
def nested_model
|
102
|
+
self.object.errors.first[0].to_s.split(".").first.singularize.downcase
|
103
|
+
end
|
104
|
+
|
105
|
+
def nested_model_human
|
106
|
+
self.nested_model.capitalize.constantize.model_name.human
|
107
|
+
end
|
108
|
+
|
109
|
+
def nested_attr
|
110
|
+
self.object.errors.first[0].to_s.split(".").last
|
111
|
+
end
|
112
|
+
|
113
|
+
def nested_attr_human
|
114
|
+
self.nested_model.capitalize.constantize.human_attribute_name(self.nested_attr)
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
class Resource < Base
|
119
|
+
# for standard errors this method build a hash
|
120
|
+
# @return [String] json string
|
121
|
+
def error
|
122
|
+
{
|
123
|
+
error: {
|
124
|
+
model: self.object["model"],
|
125
|
+
attribute: self.object["attribute"],
|
126
|
+
field: self.object["field"],
|
127
|
+
message: self.object["message"],
|
128
|
+
full_message: "#{self.object["attribute"]} #{self.object["message"]}"
|
129
|
+
}
|
130
|
+
}
|
131
|
+
end
|
132
|
+
|
133
|
+
# return the error message
|
134
|
+
# @return [String]
|
135
|
+
def message
|
136
|
+
self.error[:message]
|
137
|
+
end
|
138
|
+
|
139
|
+
# return the error status
|
140
|
+
def status
|
141
|
+
406
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
145
|
+
class Simple < Base
|
146
|
+
attr_accessor :status
|
147
|
+
|
148
|
+
|
149
|
+
# for standard errors this method build a hash
|
150
|
+
# @return [String] json string
|
151
|
+
def error
|
152
|
+
{
|
153
|
+
error: {
|
154
|
+
message: self.object[:message],
|
155
|
+
full_message: "#{self.object[:field]} #{self.object[:message]} ",
|
156
|
+
field: self.object[:field]
|
157
|
+
}
|
158
|
+
}
|
159
|
+
end
|
160
|
+
|
161
|
+
# return the error message
|
162
|
+
# @return [String]
|
163
|
+
def message
|
164
|
+
self.object[:message]
|
165
|
+
end
|
166
|
+
|
167
|
+
# return the error status
|
168
|
+
def status
|
169
|
+
406
|
170
|
+
end
|
171
|
+
end
|
172
|
+
|
173
|
+
# represents the simple errors
|
174
|
+
class UnauthorizedApplication < Base
|
175
|
+
attr_accessor :status
|
176
|
+
|
177
|
+
|
178
|
+
# for standard errors this method build a hash
|
179
|
+
# @return [String] json string
|
180
|
+
def error
|
181
|
+
{
|
182
|
+
error: {
|
183
|
+
message: self.object[:message]
|
184
|
+
}
|
185
|
+
}
|
186
|
+
end
|
187
|
+
|
188
|
+
# return the error message
|
189
|
+
# @return [String]
|
190
|
+
def message
|
191
|
+
self.object[:message]
|
192
|
+
end
|
193
|
+
|
194
|
+
# return the error status
|
195
|
+
def status
|
196
|
+
401
|
197
|
+
end
|
198
|
+
|
199
|
+
end
|
200
|
+
|
2
201
|
end
|
3
202
|
|
4
|
-
|
203
|
+
# require 'exceptions/model'
|
204
|
+
# require 'exceptions/resource'
|
205
|
+
# require 'exceptions/simple'
|
206
|
+
# require 'exceptions/unauthorized_application'
|