error_response 1.0.6 → 1.1.1

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
  SHA256:
3
- metadata.gz: ded66c5dc08f6d4164b17618f20e6b4fd36b983ca3f63b9fcbfbc04bf28b5cd0
4
- data.tar.gz: 53cf4d755c81d1e169716db23f7244fab36592ac2fc479b86ae8ceebfb50183e
3
+ metadata.gz: 8a6481a842281a50cbbd0cd724944835901ae86ee3e94a53ea0006f5956a1bb9
4
+ data.tar.gz: a5e60809e055f5a384648bde3fa4d6173c8886bd321ad46c2a956a6341245074
5
5
  SHA512:
6
- metadata.gz: 48497c941b8498cba732ebc2f9aea1c927eab8bc3ec4aaf54b8f0d2c089f92fa1d79b7cafdd2d45d67525eff73076349eacc81418df130ed7be7c4f9edc27060
7
- data.tar.gz: 857d2852a7df842e616da27c4bd2affe5860b00a08a34eb9f95477cc39c7b6df49155577ec8052a85c78f77c91dbf93a60175a5808dd6890673bb8793ac31c17
6
+ metadata.gz: df7e0bfae844c91c6b9fa84e8219f74b14f639aee0351bd348c2c69e4807021482dc669307b0ffc7a10f2ddbf6ff9929524c7b4fa817aafeafb6fcc67bf72692
7
+ data.tar.gz: becc8d002c3c340d1852e131a92b7d9e6442c58e3f110422b58e359dda5e7f14feb558c5fa522811b613fd582b85b8233a3a637e6b4b0149550fe7f34070f81d
@@ -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,10 +1,25 @@
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'
9
+
10
+ module ErrorResponse
6
11
 
7
12
  class << self
13
+ attr_accessor :configuration
14
+
15
+ def configuration
16
+ @configuration ||= Configuration.new
17
+ end
18
+
19
+ def configure(&block)
20
+ yield(configuration)
21
+ end
22
+
8
23
  def all
9
24
  yaml_hash
10
25
  end
@@ -30,7 +45,7 @@ class ErrorResponse
30
45
  def yaml_hash
31
46
  return @hash unless @hash.nil?
32
47
 
33
- settings = YAML.load(File.read(SETTING_PATH))
48
+ settings = YAML.load(File.read(configuration.yaml_config_path))
34
49
  local_array = settings['source']['local']
35
50
  local_hash = local_array.nil? ? {} : local_array.map { |path| YAML.load_file(path) }.inject(&:merge)
36
51
 
@@ -43,6 +58,9 @@ class ErrorResponse
43
58
  def build_yaml(url)
44
59
  content = URI.open(url){|f| f.read}
45
60
  YAML.load(content)
61
+ rescue
62
+ puts "Load yaml from URL (#{url}) failed."
63
+ {}
46
64
  end
47
65
 
48
66
  def parse_status(error_code)
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.6
4
+ version: 1.1.1
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-05-18 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2023-02-16 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.7.2
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.7.2
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
@@ -36,7 +53,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
36
53
  - !ruby/object:Gem::Version
37
54
  version: '0'
38
55
  requirements: []
39
- rubygems_version: 3.0.3
56
+ rubygems_version: 3.4.6
40
57
  signing_key:
41
58
  specification_version: 4
42
59
  summary: A tool for API error response