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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ae59b5b90d9f9beb0c257f5a03f75f44b1edb527
4
- data.tar.gz: 506112e55e8b1a85f7b91f5bcd6e88566b287f1f
3
+ metadata.gz: 4de6e51d5729e576fac90ec7988948a4d03e044b
4
+ data.tar.gz: 9bc1093a7d54848b57b77986439c481e76fceda0
5
5
  SHA512:
6
- metadata.gz: 980351651f4624843b787b5b0cbb973ad4cea7c6698a7b76d0598d7ba3936b4546ae0c60dd5c5a34bb7aa6098634a82fa7eb2ed250cdab4eca6eb517ffd74ac9
7
- data.tar.gz: 38da0e9a38c7d926d404223a7ab847e188be4c757fcc58a1b584243429f59193611cb61aa67903731ca080a144f23bf14887a6bdb414bd231816ac64e71ac51e
6
+ metadata.gz: 2574b8144fc5a2ef86462f1a04280c2b82c9ea7c8ba8c28c85392b032c62080ee1e21abd798063dafc99626344efe40af18ae189979c32e6957854a5536d8dab
7
+ data.tar.gz: 28d70e267816181dfa5aa5bcd52ecbc7f40940186a032fc7a28eacaa726f9099641560a6397dcc423dec89d7c9c4a1fe7acbfe89e8a5c971a63c52d9dac6b2af
data/Gemfile CHANGED
@@ -1,5 +1,7 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
+ gem 'json'
4
+
3
5
  # Specify your gem's dependencies in codecode-common-utils.gemspec
4
6
  gemspec
5
7
 
@@ -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.to_s.eql? 'Hash'
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.to_s.eql? 'Hash'
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
@@ -1,7 +1,7 @@
1
1
  module CodeCode
2
2
  module Common
3
3
  module Utils
4
- VERSION = '0.1.3'
4
+ VERSION = '0.1.4'
5
5
  end
6
6
  end
7
7
  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.3
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-08 00:00:00.000000000 Z
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