error_response 1.0.4 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: dd43647dc406683c0562945f7bffa780d26dbb15cc3fe6595027887e492a5cc0
4
- data.tar.gz: 7820e4d0867e45a9f38e57630e967f12fd782144d078e4116696b10bbd228a53
3
+ metadata.gz: dedc84282989933797fadebac296c8e16bb282b2c7930be3145677bb76f050eb
4
+ data.tar.gz: bf979b462c681c589ad1c41839ff44c0ed2c50694070d04fb553febacb28c6ee
5
5
  SHA512:
6
- metadata.gz: 915b407b7ae666af601e28e6e023bd41ccc1c3894424ec63b17eca0d4dd47e72ee02ddab4355e308cc4439dfafd9d67652701ed8a2a7ae11596de785c79a251a
7
- data.tar.gz: 37a5c49b15cb0ea5ab80bc425376b733c512451bde1c355349ae0dca0bf6c88e33039d6651c753e9fe7155a486bd39107c7fd68c5265b8927fb042076ce417a5
6
+ metadata.gz: b32f1bf5facbda10a6a195badfb8eba1f345e9f6e4ebe95bce3f27cfafd5d8f9237e231ca04ae1cf3317446b105e76d80ba996f540199d587365ef7bf7e60746
7
+ data.tar.gz: 8f79daa54b018c55e8824b00eef2c0a2ed364d0f27ded58dfb9a061234721d47237da9d9e2b490b0db95626e9be52b24155884589a9467424c8a2bbdcbdf9855
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ErrorResponse
4
+ class Configuration
5
+ attr_accessor :yaml_config_path
6
+
7
+ def initialize
8
+ @yaml_config_path = ENV['YAML_CONFIG_PATH'] || 'config/error_response.yml'
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support'
4
+
5
+ module ErrorResponse
6
+ module Helper
7
+ extend ActiveSupport::Concern
8
+
9
+ included do
10
+ rescue_from RequestError do |e|
11
+ error_response(e.key, e.error_message, e.error_data)
12
+ end
13
+ end
14
+
15
+ def success_response(data = {})
16
+ render status: 200, json: { data: data }
17
+ end
18
+
19
+ def error_response(key, error_message = nil, error_data = {})
20
+ render_content = ErrorResponse.to_api(key, error_message).deep_dup
21
+ render_content[:json].merge!(error_data) if error_data.present? && error_data.is_a?(Hash)
22
+ render_content[:json].merge!(error_data: error_data) if error_data.present? && error_data.is_a?(Array)
23
+ render(render_content)
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ErrorResponse
4
+ class RequestError < StandardError
5
+ attr_reader :key, :error_message, :error_data
6
+
7
+ def initialize(key, error_message: nil, error_data: {})
8
+ @key = key
9
+ @error_message = error_message
10
+ @error_data = error_data
11
+ end
12
+ end
13
+ end
@@ -1,54 +1,81 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'yaml'
2
4
  require 'open-uri'
3
5
 
4
- class ErrorResponse
5
- SETTING_PATH = 'config/error_response.yml'
6
+ require 'error_response/configuration'
7
+ require 'error_response/helper'
8
+ require 'error_response/request_error'
6
9
 
7
- def self.all
8
- yaml_hash
9
- end
10
+ module ErrorResponse
10
11
 
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
12
+ class << self
13
+ attr_accessor :configuration
15
14
 
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: parse_status(json['error_code']),
27
- json: json
28
- }
29
- end
15
+ def configuration
16
+ @configuration ||= Configuration.new
17
+ end
30
18
 
31
- private
19
+ def configure(&block)
20
+ yield(configuration)
21
+ end
32
22
 
33
- def self.yaml_hash
34
- return @hash unless @hash.nil?
23
+ def all
24
+ yaml_hash
25
+ end
35
26
 
36
- settings = YAML.load(File.read(SETTING_PATH))
37
- local_array = settings['source']['local']
38
- local_hash = local_array.nil? ? {} : local_array.map { |path| YAML.load_file(path) }.inject(&:merge)
27
+ def to_hash(key)
28
+ return {} unless yaml_hash.key?(key.to_s)
29
+ yaml_hash[key.to_s].merge({ 'error_key' => key.to_s })
30
+ end
39
31
 
40
- remote_array = settings['source']['remote']
41
- remote_hash = remote_array.nil? ? {} : remote_array.map { |url| build_yaml(url) }.inject(&:merge)
32
+ def to_api(key, message=nil)
33
+ json = deep_dup(yaml_hash)
34
+ json = json[key.to_s] || { 'error_code' => 500_000, 'error_message' => key.to_s }
35
+ json['error_key'] = key.to_s
36
+ json['error_message'] += ": #{message}" unless message.nil?
37
+ {
38
+ status: parse_status(json['error_code']),
39
+ json: json
40
+ }
41
+ end
42
42
 
43
- @hash = local_hash.merge(remote_hash)
44
- end
43
+ private
45
44
 
46
- def self.build_yaml(url)
47
- content = URI.open(url){|f| f.read}
48
- YAML.load(content)
49
- end
45
+ def yaml_hash
46
+ return @hash unless @hash.nil?
50
47
 
51
- def self.parse_status(error_code)
52
- error_code.to_s[0..2].to_i
48
+ settings = YAML.load(File.read(configuration.yaml_config_path))
49
+ local_array = settings['source']['local']
50
+ local_hash = local_array.nil? ? {} : local_array.map { |path| YAML.load_file(path) }.inject(&:merge)
51
+
52
+ remote_array = settings['source']['remote']
53
+ remote_hash = remote_array.nil? ? {} : remote_array.map { |url| build_yaml(url) }.inject(&:merge)
54
+
55
+ @hash = local_hash.merge(remote_hash)
56
+ end
57
+
58
+ def build_yaml(url)
59
+ content = URI.open(url){|f| f.read}
60
+ YAML.load(content)
61
+ rescue
62
+ puts "Load yaml from URL (#{url}) failed."
63
+ {}
64
+ end
65
+
66
+ def parse_status(error_code)
67
+ error_code.to_s[0..2].to_i
68
+ end
69
+
70
+ def deep_dup(h)
71
+ Hash[h.map{|k, v| [k,
72
+ if v.is_a?(Hash)
73
+ deep_dup(v)
74
+ else
75
+ v.dup rescue v
76
+ end
77
+ ]}]
78
+ end
53
79
  end
80
+
54
81
  end
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: error_response
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.4
4
+ version: 1.1.0
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: 2021-01-13 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2022-09-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 6.1.4
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 6.1.4
13
27
  description: use for error_response
14
28
  email: dev@kdanmobile.com
15
29
  executables: []
@@ -17,6 +31,9 @@ extensions: []
17
31
  extra_rdoc_files: []
18
32
  files:
19
33
  - lib/error_response.rb
34
+ - lib/error_response/configuration.rb
35
+ - lib/error_response/helper.rb
36
+ - lib/error_response/request_error.rb
20
37
  homepage: https://github.com/kdan-mobile-software-ltd/error_response
21
38
  licenses:
22
39
  - MIT