error_response 0.2.6 → 1.0.0

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 +34 -26
  3. metadata +4 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a0f90a8aea1f1e2d6f12af804b4ae00a5ccd94b28dbb5a6ffd43409fa146c430
4
- data.tar.gz: 8c7beb929a1b26a171620e5775821f826bd20ecf4a472dda785935c07f90863f
3
+ metadata.gz: ebe8d02da14d5ee8e61570632107f19771e72abecde069d7135bb34c605ab690
4
+ data.tar.gz: e474c47b4ea2a7451706d07323cd1b0cd62c29a04abba20b5cdfb184c8d616fe
5
5
  SHA512:
6
- metadata.gz: 2bc4768ae9f29f625c921f780e0d8149d880b531d16910f637ed889ec38a3f98ef3bc00522d1d295ed7b856924561181bd6db9a9b418d7aa94dc7228b0fb2487
7
- data.tar.gz: '019c8cf0ff9b531903520730ec3af687a93543c0d95700e030525e46e3867313d76549477e50b57edae5b4a785ef507c653e638a9711a1bd9ec37099bbf90495'
6
+ metadata.gz: 51fa6d71acb509c7713c19089a5cfa431a25adc6866fbb4db52c775fa08cd3117b40b9e8be0ad91059b3c8a099bbb3df3f2b95ef4ce310ca1797b2761ec4be32
7
+ data.tar.gz: 68e1fb3cafb7f2d42e7e1595dab4404d71e1e1c10ccbeabe4ce20acd23148a5a0e9980fe842cb91ac2480d295e2e148126fbae2ef9215d91a7856f9fe827b192
@@ -1,39 +1,47 @@
1
1
  require 'yaml'
2
+ require 'open-uri'
3
+
2
4
  class ErrorResponse
3
- YAML_PATH = 'config/error_response.yml'
5
+ SETTING_PATH = 'config/error_response.yml'
4
6
 
5
- def self.to_api(key, message=nil)
6
- err_json = yaml_hash[key.to_s]&.dup
7
- err_json['error_key'] = key.to_s if !err_json.nil? && !key.nil? && yaml_hash.key?(key.to_s)
8
- if err_json.nil?
9
- err_json = {'error_code' => 500000, 'error_message' => message}
10
- elsif !message.nil?
11
- err_json['error_message'] += ": #{message}"
12
- end
13
-
14
- status = err_json['error_code'] / 1000
15
- {
16
- status: status,
17
- json: err_json
18
- }
7
+ def self.all
8
+ yaml_hash
19
9
  end
20
10
 
21
11
  def self.to_hash(key)
22
- if yaml_hash.key?(key.to_s)
23
- result = yaml_hash[key.to_s]
24
- result['error_key'] = key.to_s
25
- else
26
- result = nil
27
- end
28
- result
12
+ return unless yaml_hash.key?(key.to_s)
13
+ yaml_hash[key.to_s].merge({ 'error_key' => key.to_s })
29
14
  end
30
15
 
31
- def self.all
32
- yaml_hash
16
+ def self.to_api(key, message=nil)
17
+ default_result = {
18
+ status: 500,
19
+ json: { 'error_code' => 500_000, 'error_message' => message }
20
+ }
21
+ return default_result unless yaml_hash.key?(key.to_s)
22
+
23
+ json = yaml_hash[key.to_s].merge({ 'error_key' => key.to_s })
24
+ json['error_message'] += ": #{message}" unless message.nil?
25
+ {
26
+ status: json['error_code'] / 1_000,
27
+ json: json
28
+ }
33
29
  end
34
30
 
35
31
  private
32
+
36
33
  def self.yaml_hash
37
- @hash ||= YAML.load_file(YAML_PATH)
34
+ return @hash unless @hash.nil?
35
+
36
+ settings = YAML.load(File.read(SETTING_PATH))
37
+ local_hash = settings['source']['local'].map { |path| YAML.load_file(path) }.inject(&:merge)
38
+ remote_hash = settings['source']['remote'].map { |url| build_yaml(url) }.inject(&:merge)
39
+
40
+ @hash = local_hash.merge(remote_hash)
41
+ end
42
+
43
+ def self.build_yaml(url)
44
+ content = open(url){|f| f.read}
45
+ YAML.load(content)
38
46
  end
39
- end
47
+ end
metadata CHANGED
@@ -1,17 +1,17 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: error_response
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.6
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
- - jameslee
7
+ - Kdan Mobile Software Developer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-08-14 00:00:00.000000000 Z
11
+ date: 2020-08-24 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: use for error_response
14
- email: jameslee@kdanmobile.com
14
+ email: dev@kdanmobile.com
15
15
  executables: []
16
16
  extensions: []
17
17
  extra_rdoc_files: []