codecode-common-utils 0.1.3 → 0.1.4
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/Gemfile +2 -0
- data/lib/codecode/common/exceptions.rb +41 -0
- data/lib/codecode/common/utils.rb +29 -2
- data/lib/codecode/common/utils/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4de6e51d5729e576fac90ec7988948a4d03e044b
|
4
|
+
data.tar.gz: 9bc1093a7d54848b57b77986439c481e76fceda0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2574b8144fc5a2ef86462f1a04280c2b82c9ea7c8ba8c28c85392b032c62080ee1e21abd798063dafc99626344efe40af18ae189979c32e6957854a5536d8dab
|
7
|
+
data.tar.gz: 28d70e267816181dfa5aa5bcd52ecbc7f40940186a032fc7a28eacaa726f9099641560a6397dcc423dec89d7c9c4a1fe7acbfe89e8a5c971a63c52d9dac6b2af
|
data/Gemfile
CHANGED
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
3
|
+
module CodeCode
|
4
|
+
module Common
|
5
|
+
module Exceptions
|
6
|
+
|
7
|
+
# @class [UnknownFieldException]
|
8
|
+
class UnknownFieldException < StandardError
|
9
|
+
attr_reader :status, :message, :data, :code
|
10
|
+
|
11
|
+
def initialize(message, status = 400, code = 0, data = {})
|
12
|
+
@status = status
|
13
|
+
@message = message
|
14
|
+
@data = data
|
15
|
+
@code = code
|
16
|
+
end
|
17
|
+
|
18
|
+
#
|
19
|
+
# Convert Exception contents to a Json string. All attributes must
|
20
|
+
# be Json serializable.
|
21
|
+
def to_json
|
22
|
+
JSON.generate(to_hash)
|
23
|
+
end
|
24
|
+
|
25
|
+
def to_hash
|
26
|
+
{status: @status, message: @message, code: @code, data: @data}
|
27
|
+
end
|
28
|
+
|
29
|
+
def to_response
|
30
|
+
[@status, to_json]
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
# @class [EmptyFieldException]
|
35
|
+
class EmptyFieldException < UnknownFieldException; end
|
36
|
+
|
37
|
+
# @class [NullFieldException]
|
38
|
+
class NullFieldException < UnknownFieldException; end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -1,8 +1,12 @@
|
|
1
1
|
require 'codecode/common/utils/version'
|
2
|
+
require 'codecode/common/exceptions'
|
3
|
+
|
4
|
+
include CodeCode::Common::Exceptions
|
2
5
|
|
3
6
|
module CodeCode
|
4
7
|
module Common
|
5
8
|
module Utils
|
9
|
+
|
6
10
|
# Utility methods for Hash manipulation
|
7
11
|
module Hash
|
8
12
|
# Convert Hash string keys to symbol keys
|
@@ -12,7 +16,7 @@ module CodeCode
|
|
12
16
|
def self.symbolize_keys(hash)
|
13
17
|
hash.inject({}){ |memo, (k, v)|
|
14
18
|
memo[k.to_sym] = v
|
15
|
-
memo[k.to_sym] = symbolize_keys v if v.class.
|
19
|
+
memo[k.to_sym] = symbolize_keys v if v.class.eql? ::Hash
|
16
20
|
memo
|
17
21
|
}
|
18
22
|
end
|
@@ -24,7 +28,7 @@ module CodeCode
|
|
24
28
|
def self.symbolize_keys!(hash)
|
25
29
|
new_hash = hash.inject({}){ |memo, (k, v)|
|
26
30
|
memo[k.to_sym] = v
|
27
|
-
memo[k.to_sym] = symbolize_keys v if v.class.
|
31
|
+
memo[k.to_sym] = symbolize_keys v if v.class.eql? ::Hash
|
28
32
|
memo
|
29
33
|
}
|
30
34
|
hash.replace new_hash
|
@@ -48,6 +52,29 @@ module CodeCode
|
|
48
52
|
hash.select { |k, v| keys.include?(k) }
|
49
53
|
end
|
50
54
|
end
|
55
|
+
|
56
|
+
# Utility methods for Validation
|
57
|
+
module Validation
|
58
|
+
|
59
|
+
# Check if fields used in method params have their values
|
60
|
+
# empty or keys are missing
|
61
|
+
# @param [Array[Symbol]] required_fields array of symbols
|
62
|
+
# @param [Object] params
|
63
|
+
def self.check_fields(required_fields, params)
|
64
|
+
required_fields, missing, empty, null = required_fields, [], [], []
|
65
|
+
required_fields.each { |key|
|
66
|
+
missing.push(key) unless params.keys.include?(key)
|
67
|
+
empty.push(key) if params[key.to_sym].eql?('')
|
68
|
+
null.push(key) if params[key.to_sym].nil?
|
69
|
+
}
|
70
|
+
|
71
|
+
raise UnknownFieldException.new("Missing field(s) -> #{missing.join(', ')}.", 400) if missing.length > 0
|
72
|
+
|
73
|
+
raise EmptyFieldException.new("Empty field(s) -> #{empty.join(', ')}.", 400) if empty.length > 0
|
74
|
+
|
75
|
+
raise NullFieldException.new("Null field(s) -> #{null.join(', ')}.", 400) if null.length > 0
|
76
|
+
end
|
77
|
+
end
|
51
78
|
end
|
52
79
|
end
|
53
80
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: codecode-common-utils
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Felipe Rodrigues Michetti
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-07-
|
11
|
+
date: 2017-07-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -70,6 +70,7 @@ files:
|
|
70
70
|
- bin/console
|
71
71
|
- bin/setup
|
72
72
|
- codecode-common-utils.gemspec
|
73
|
+
- lib/codecode/common/exceptions.rb
|
73
74
|
- lib/codecode/common/utils.rb
|
74
75
|
- lib/codecode/common/utils/version.rb
|
75
76
|
homepage: http://www.codecode.com.br
|