error_response 1.0.5 → 1.0.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/error_response.rb +54 -40
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 942aeb3cb71ffd7c3adff6fd0d30d200ec1179eac0252f9c3d73c19530ef3226
4
- data.tar.gz: 077bcdd308abe7d3eaf1a849594b9f25d056e52447c83a7b93ce6014d440c19b
3
+ metadata.gz: ded66c5dc08f6d4164b17618f20e6b4fd36b983ca3f63b9fcbfbc04bf28b5cd0
4
+ data.tar.gz: 53cf4d755c81d1e169716db23f7244fab36592ac2fc479b86ae8ceebfb50183e
5
5
  SHA512:
6
- metadata.gz: 64c7a9948a528a8cb85c188538cb885802483e4752aa8ad98fcad13e5f4cd65245e78dc5f5c8967a26c3345d3668b46b06840a2bd77207396f4b2075ca97d62b
7
- data.tar.gz: a27250609dee2141be6f848f95588ea150b8f6ff9d080886ba47175b6fea7476e56fa13487e5e335e4508e9de479c993a4c3a0b76a53e712d43a24f70a88703a
6
+ metadata.gz: 48497c941b8498cba732ebc2f9aea1c927eab8bc3ec4aaf54b8f0d2c089f92fa1d79b7cafdd2d45d67525eff73076349eacc81418df130ed7be7c4f9edc27060
7
+ data.tar.gz: 857d2852a7df842e616da27c4bd2affe5860b00a08a34eb9f95477cc39c7b6df49155577ec8052a85c78f77c91dbf93a60175a5808dd6890673bb8793ac31c17
@@ -4,46 +4,60 @@ require 'open-uri'
4
4
  class ErrorResponse
5
5
  SETTING_PATH = 'config/error_response.yml'
6
6
 
7
- def self.all
8
- yaml_hash
7
+ class << self
8
+ def all
9
+ yaml_hash
10
+ end
11
+
12
+ def to_hash(key)
13
+ return {} unless yaml_hash.key?(key.to_s)
14
+ yaml_hash[key.to_s].merge({ 'error_key' => key.to_s })
15
+ end
16
+
17
+ def to_api(key, message=nil)
18
+ json = deep_dup(yaml_hash)
19
+ json = json[key.to_s] || { 'error_code' => 500_000, 'error_message' => key.to_s }
20
+ json['error_key'] = key.to_s
21
+ json['error_message'] += ": #{message}" unless message.nil?
22
+ {
23
+ status: parse_status(json['error_code']),
24
+ json: json
25
+ }
26
+ end
27
+
28
+ private
29
+
30
+ def yaml_hash
31
+ return @hash unless @hash.nil?
32
+
33
+ settings = YAML.load(File.read(SETTING_PATH))
34
+ local_array = settings['source']['local']
35
+ local_hash = local_array.nil? ? {} : local_array.map { |path| YAML.load_file(path) }.inject(&:merge)
36
+
37
+ remote_array = settings['source']['remote']
38
+ remote_hash = remote_array.nil? ? {} : remote_array.map { |url| build_yaml(url) }.inject(&:merge)
39
+
40
+ @hash = local_hash.merge(remote_hash)
41
+ end
42
+
43
+ def build_yaml(url)
44
+ content = URI.open(url){|f| f.read}
45
+ YAML.load(content)
46
+ end
47
+
48
+ def parse_status(error_code)
49
+ error_code.to_s[0..2].to_i
50
+ end
51
+
52
+ def deep_dup(h)
53
+ Hash[h.map{|k, v| [k,
54
+ if v.is_a?(Hash)
55
+ deep_dup(v)
56
+ else
57
+ v.dup rescue v
58
+ end
59
+ ]}]
60
+ end
9
61
  end
10
62
 
11
- def self.to_hash(key)
12
- return {} unless yaml_hash.key?(key.to_s)
13
- yaml_hash[key.to_s].merge({ 'error_key' => key.to_s })
14
- end
15
-
16
- def self.to_api(key, message=nil)
17
- json = yaml_hash[key.to_s] || { 'error_code' => 500_000, 'error_message' => key.to_s }
18
- json['error_key'] = key.to_s
19
- json['error_message'] += ": #{message}" unless message.nil?
20
- {
21
- status: parse_status(json['error_code']),
22
- json: json
23
- }
24
- end
25
-
26
- private
27
-
28
- def self.yaml_hash
29
- return @hash unless @hash.nil?
30
-
31
- settings = YAML.load(File.read(SETTING_PATH))
32
- local_array = settings['source']['local']
33
- local_hash = local_array.nil? ? {} : local_array.map { |path| YAML.load_file(path) }.inject(&:merge)
34
-
35
- remote_array = settings['source']['remote']
36
- remote_hash = remote_array.nil? ? {} : remote_array.map { |url| build_yaml(url) }.inject(&:merge)
37
-
38
- @hash = local_hash.merge(remote_hash)
39
- end
40
-
41
- def self.build_yaml(url)
42
- content = URI.open(url){|f| f.read}
43
- YAML.load(content)
44
- end
45
-
46
- def self.parse_status(error_code)
47
- error_code.to_s[0..2].to_i
48
- end
49
63
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: error_response
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.5
4
+ version: 1.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kdan Mobile Software Developer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-03-16 00:00:00.000000000 Z
11
+ date: 2022-05-18 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: use for error_response
14
14
  email: dev@kdanmobile.com