error_response 1.0.6 → 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: ded66c5dc08f6d4164b17618f20e6b4fd36b983ca3f63b9fcbfbc04bf28b5cd0
4
- data.tar.gz: 53cf4d755c81d1e169716db23f7244fab36592ac2fc479b86ae8ceebfb50183e
3
+ metadata.gz: dedc84282989933797fadebac296c8e16bb282b2c7930be3145677bb76f050eb
4
+ data.tar.gz: bf979b462c681c589ad1c41839ff44c0ed2c50694070d04fb553febacb28c6ee
5
5
  SHA512:
6
- metadata.gz: 48497c941b8498cba732ebc2f9aea1c927eab8bc3ec4aaf54b8f0d2c089f92fa1d79b7cafdd2d45d67525eff73076349eacc81418df130ed7be7c4f9edc27060
7
- data.tar.gz: 857d2852a7df842e616da27c4bd2affe5860b00a08a34eb9f95477cc39c7b6df49155577ec8052a85c78f77c91dbf93a60175a5808dd6890673bb8793ac31c17
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,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.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: 2022-05-18 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