exceptions-resource 0.0.1p01 → 0.0.2p01
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 +4 -4
- data/README.md +6 -2
- data/lib/exceptions/base.rb +26 -24
- data/lib/exceptions/model.rb +70 -71
- data/lib/exceptions/resource.rb +24 -22
- data/lib/exceptions/simple.rb +1 -1
- data/lib/exceptions/unauthorized_application.rb +1 -1
- data/lib/exceptions/version.rb +1 -1
- data/lib/exceptions.rb +1 -0
- 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: 971078a4e0ce20ab029da61fc524539fd749058d
|
4
|
+
data.tar.gz: 8597a9572f35b160c54786854d8f17a3d6f1bf76
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a942f17789d19bb10f27a5e7af9476cbe44b23a4451e22459b9bb715ee8ba7121fd102700865d136f8c51f26ab3e8192bf404d96d448cd0213b8116b0db80e2c
|
7
|
+
data.tar.gz: d40a27f0a5dfcabab54bc3af91e0ffd9aec2d84e7e5bbfcd788b836472d03485d1b93088f3ab11c59d44cae1a62e09f1128eae5de79460ae68ac080fe66db139
|
data/README.md
CHANGED
@@ -8,7 +8,11 @@ needs `ActiveModel` as dependency if you aren't working with rails.
|
|
8
8
|
Add this line to your application's Gemfile:
|
9
9
|
|
10
10
|
```ruby
|
11
|
-
|
11
|
+
# direct from github
|
12
|
+
gem 'exceptions-resource', github: 'xdougx/exceptions-resource'
|
13
|
+
|
14
|
+
# from rubygems
|
15
|
+
gem 'exceptions-resource'
|
12
16
|
```
|
13
17
|
|
14
18
|
And then execute:
|
@@ -17,7 +21,7 @@ And then execute:
|
|
17
21
|
|
18
22
|
Or install it yourself as:
|
19
23
|
|
20
|
-
$ gem install exceptions
|
24
|
+
$ gem install exceptions-resource
|
21
25
|
|
22
26
|
## Usage
|
23
27
|
|
data/lib/exceptions/base.rb
CHANGED
@@ -1,29 +1,31 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
module Exceptions
|
2
|
+
class Base < StandardError
|
3
|
+
attr_accessor :object, :type
|
3
4
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
5
|
+
# starts a new instance with an object
|
6
|
+
# @param [Object] object
|
7
|
+
def initialize object
|
8
|
+
self.object = object
|
9
|
+
end
|
9
10
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
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
|
17
18
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
19
|
+
# return if is as simple error
|
20
|
+
# @return [Boolean]
|
21
|
+
def simple?
|
22
|
+
self.class.name.demodulize.tableize.singularize == "simple"
|
23
|
+
end
|
23
24
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
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
|
29
31
|
end
|
data/lib/exceptions/model.rb
CHANGED
@@ -1,88 +1,87 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
1
|
+
module Exceptions
|
2
|
+
class Model < Base
|
3
|
+
# for model errors this method build a hash with all necessary information
|
4
|
+
# @return [String] json string
|
5
|
+
def error
|
6
|
+
self.is_nested? ? self.build_nested : self.build_normal
|
7
|
+
end
|
8
8
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
9
|
+
def build_nested
|
10
|
+
{
|
11
|
+
error: {
|
12
|
+
model: self.nested_model.camelcase,
|
13
|
+
field: "#{self.nested_model}[#{self.nested_attr}]",
|
14
|
+
attribute: self.nested_attr,
|
15
|
+
message: self.message,
|
16
|
+
full_message: "#{self.nested_attr_human} #{self.message}"
|
17
|
+
}
|
18
|
+
}
|
19
|
+
end
|
20
20
|
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
21
|
+
def build_normal
|
22
|
+
{
|
23
|
+
error: {
|
24
|
+
model: self.model.camelcase,
|
25
|
+
field: "#{self.model}[#{self.attribute}]",
|
26
|
+
attribute: self.attribute,
|
27
|
+
message: self.message,
|
28
|
+
full_message: "#{self.attribute_human} #{self.message}"
|
29
|
+
}
|
30
|
+
}
|
31
|
+
end
|
32
32
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
33
|
+
# return what model is
|
34
|
+
# @return [String]
|
35
|
+
def model
|
36
|
+
self.object.class.name.demodulize.tableize.singularize.downcase
|
37
|
+
end
|
38
38
|
|
39
|
-
|
40
|
-
|
41
|
-
|
39
|
+
def attribute
|
40
|
+
self.object.errors.first[0]
|
41
|
+
end
|
42
42
|
|
43
|
-
|
44
|
-
|
45
|
-
|
43
|
+
def model_human
|
44
|
+
self.object.class.model_name.human.demodulize.tableize.singularize.downcase
|
45
|
+
end
|
46
46
|
|
47
|
-
|
48
|
-
|
49
|
-
|
47
|
+
def attribute_human
|
48
|
+
self.object.class.human_attribute_name(self.object.errors.first[0])
|
49
|
+
end
|
50
50
|
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
51
|
+
# return the error message
|
52
|
+
# @return [String]
|
53
|
+
def message
|
54
|
+
"#{self.object.errors.first[1]}"
|
55
|
+
end
|
56
56
|
|
57
|
-
|
58
|
-
|
59
|
-
|
57
|
+
def status
|
58
|
+
422
|
59
|
+
end
|
60
60
|
|
61
|
-
|
62
|
-
|
61
|
+
def is_nested?
|
62
|
+
attribute = self.object.errors.first[0]
|
63
63
|
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
64
|
+
if self.object.errors.first[0].to_s.split(".").size > 1
|
65
|
+
self.object.respond_to?(attribute) ? false : true
|
66
|
+
else
|
67
|
+
false
|
68
|
+
end
|
68
69
|
end
|
69
|
-
end
|
70
70
|
|
71
|
-
|
72
|
-
|
73
|
-
|
71
|
+
def nested_model
|
72
|
+
self.object.errors.first[0].to_s.split(".").first.singularize.downcase
|
73
|
+
end
|
74
74
|
|
75
|
-
|
76
|
-
|
77
|
-
|
75
|
+
def nested_model_human
|
76
|
+
self.nested_model.capitalize.constantize.model_name.human
|
77
|
+
end
|
78
78
|
|
79
|
-
|
80
|
-
|
81
|
-
|
79
|
+
def nested_attr
|
80
|
+
self.object.errors.first[0].to_s.split(".").last
|
81
|
+
end
|
82
82
|
|
83
|
-
|
84
|
-
|
83
|
+
def nested_attr_human
|
84
|
+
self.nested_model.capitalize.constantize.human_attribute_name(self.nested_attr)
|
85
|
+
end
|
85
86
|
end
|
86
|
-
|
87
|
-
|
88
87
|
end
|
data/lib/exceptions/resource.rb
CHANGED
@@ -1,26 +1,28 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
1
|
+
module Exceptions
|
2
|
+
class Resource < Base
|
3
|
+
# for standard errors this method build a hash
|
4
|
+
# @return [String] json string
|
5
|
+
def error
|
6
|
+
{
|
7
|
+
error: {
|
8
|
+
model: self.object["model"],
|
9
|
+
attribute: self.object["attribute"],
|
10
|
+
field: self.object["field"],
|
11
|
+
message: self.object["message"],
|
12
|
+
full_message: "#{self.object["attribute"]} #{self.object["message"]}"
|
13
|
+
}
|
14
|
+
}
|
15
|
+
end
|
15
16
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
17
|
+
# return the error message
|
18
|
+
# @return [String]
|
19
|
+
def message
|
20
|
+
self.error[:message]
|
21
|
+
end
|
21
22
|
|
22
|
-
|
23
|
-
|
24
|
-
|
23
|
+
# return the error status
|
24
|
+
def status
|
25
|
+
406
|
26
|
+
end
|
25
27
|
end
|
26
28
|
end
|
data/lib/exceptions/simple.rb
CHANGED
data/lib/exceptions/version.rb
CHANGED
data/lib/exceptions.rb
CHANGED