marr 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 4c09a278383e2aaff5752cedcccc2e2c4251f4b58c99cec751b730507ad83a72
4
+ data.tar.gz: 4cbf6d1a32256ed9ddfe08be9ba7395ca8c80121b5dcaa7e492181eb6cee5298
5
+ SHA512:
6
+ metadata.gz: b04a39184f988a8d32b176605f91cdd620c3b6efcda8914b179f1cfec5594d7c8b14201d194f84a87b1a6ccdef942ce57d724cf484306d2f5e3fc28dd1165ac5
7
+ data.tar.gz: bed427a55d7b4213cf254f3fd1d2ddf46fa2c42c95dcc774ff732f3a6fc33da04173a6d20d7feeb3a3249f0c1b48eb027cd65d089aca7aa886f5a8ddb2be6099
@@ -0,0 +1,18 @@
1
+ name: Ruby
2
+
3
+ on: [push,pull_request]
4
+
5
+ jobs:
6
+ build:
7
+ runs-on: ubuntu-latest
8
+ steps:
9
+ - uses: actions/checkout@v2
10
+ - name: Set up Ruby
11
+ uses: ruby/setup-ruby@v1
12
+ with:
13
+ ruby-version: 2.6.8
14
+ - name: Run the default task
15
+ run: |
16
+ gem install bundler -v 2.2.3
17
+ bundle install
18
+ bundle exec rake
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+
13
+ # Ignore all files with the .gem extension
14
+ *.gem
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,10 @@
1
+ Style/StringLiterals:
2
+ Enabled: true
3
+ EnforcedStyle: double_quotes
4
+
5
+ Style/StringLiteralsInInterpolation:
6
+ Enabled: true
7
+ EnforcedStyle: double_quotes
8
+
9
+ Layout/LineLength:
10
+ Max: 120
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gemspec
6
+
7
+ gem "rake", "~> 13.0"
8
+
9
+ gem "rspec", "~> 3.0"
10
+
11
+ gem "rubocop", "~> 0.80"
data/Gemfile.lock ADDED
@@ -0,0 +1,70 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ marr (0.1.0)
5
+ activesupport (>= 6.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activesupport (6.1.7.3)
11
+ concurrent-ruby (~> 1.0, >= 1.0.2)
12
+ i18n (>= 1.6, < 2)
13
+ minitest (>= 5.1)
14
+ tzinfo (~> 2.0)
15
+ zeitwerk (~> 2.3)
16
+ ast (2.4.2)
17
+ concurrent-ruby (1.2.2)
18
+ diff-lcs (1.5.0)
19
+ i18n (1.13.0)
20
+ concurrent-ruby (~> 1.0)
21
+ minitest (5.18.0)
22
+ parallel (1.23.0)
23
+ parser (3.2.2.1)
24
+ ast (~> 2.4.1)
25
+ rainbow (3.1.1)
26
+ rake (13.0.6)
27
+ regexp_parser (2.8.0)
28
+ rexml (3.2.5)
29
+ rspec (3.12.0)
30
+ rspec-core (~> 3.12.0)
31
+ rspec-expectations (~> 3.12.0)
32
+ rspec-mocks (~> 3.12.0)
33
+ rspec-core (3.12.2)
34
+ rspec-support (~> 3.12.0)
35
+ rspec-expectations (3.12.3)
36
+ diff-lcs (>= 1.2.0, < 2.0)
37
+ rspec-support (~> 3.12.0)
38
+ rspec-mocks (3.12.5)
39
+ diff-lcs (>= 1.2.0, < 2.0)
40
+ rspec-support (~> 3.12.0)
41
+ rspec-support (3.12.0)
42
+ rubocop (0.93.1)
43
+ parallel (~> 1.10)
44
+ parser (>= 2.7.1.5)
45
+ rainbow (>= 2.2.2, < 4.0)
46
+ regexp_parser (>= 1.8)
47
+ rexml
48
+ rubocop-ast (>= 0.6.0)
49
+ ruby-progressbar (~> 1.7)
50
+ unicode-display_width (>= 1.4.0, < 2.0)
51
+ rubocop-ast (1.28.0)
52
+ parser (>= 3.2.1.0)
53
+ ruby-progressbar (1.13.0)
54
+ tzinfo (2.0.6)
55
+ concurrent-ruby (~> 1.0)
56
+ unicode-display_width (1.8.0)
57
+ zeitwerk (2.6.8)
58
+
59
+ PLATFORMS
60
+ ruby
61
+
62
+ DEPENDENCIES
63
+ bundler
64
+ marr!
65
+ rake (~> 13.0)
66
+ rspec (~> 3.0)
67
+ rubocop (~> 0.80)
68
+
69
+ BUNDLED WITH
70
+ 2.2.3
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Micah Bowie
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,121 @@
1
+ # Marr (Memereply Api Rescue Response)
2
+
3
+ Dynamically handle exceptions and render a structured JSON response to client applications.
4
+ Formerly (Memereply sad pepe)
5
+
6
+ ![sad pepe on the floor](https://www.meme-arsenal.com/memes/1adf62bd401ea536f9e6d4df9097201b.jpg)
7
+ ![pepe the enginer](https://images-cdn.9gag.com/photo/ajq1ePg_700b.jpg)
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'marr'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install marr
24
+
25
+ ## Example Response
26
+
27
+ ```json
28
+ {
29
+ "errors": {
30
+ "code": "GroupError",
31
+ "title": "GroupInvalid",
32
+ "detail": "The Group can not be saved. Invalid or missing data.",
33
+ "meta": {
34
+ "object_errors": [
35
+ {
36
+ "pointer": "owner",
37
+ "detail": "Owner can't be blank"
38
+ },
39
+ {
40
+ "pointer": "name",
41
+ "detail": "Name can't be blank"
42
+ }
43
+ ],
44
+ "trace_id": "6CA01AF9E592595F"
45
+ }
46
+ }
47
+ }
48
+ ```
49
+
50
+ ## Usage
51
+
52
+ There is a method automatically created for each each class that inherits from Marr::ApiError. The method is preprended with 'raise'.
53
+
54
+ ```ruby
55
+ raise_meme_error
56
+ ```
57
+
58
+ You can also pass in options to your method for a more robust response:
59
+
60
+ ```ruby
61
+ raise_meme_error(controller: self, subcode: :meme_invalid, object: @meme)
62
+ ```
63
+
64
+ ## Setup
65
+
66
+ Configure the gem. For the gem to recognize the descendant classes you have to provide the name space the errors are under.
67
+
68
+ ```ruby
69
+ Marr::Api::Error.configure do |config|
70
+ config.namespaces = ['Api::V1::Errors']
71
+ config.trace_id_length = 16
72
+ end
73
+ ```
74
+
75
+ Create a new Error that inherits from the ApiError class. The class needs to be under the configured name space. NOTE: The `message` method must be implemented.
76
+
77
+ ```ruby
78
+ module Api
79
+ module V1
80
+ module Errors
81
+ class MemeError < ::Marr::ApiError
82
+ def message
83
+ "There was an issue processing the meme"
84
+ end
85
+
86
+ def subcodes
87
+ super({
88
+ meme_invalid: 'This meme is invalid.',
89
+ meme_too_large: 'This meme is too damn big nephew',
90
+ })
91
+ end
92
+ end
93
+ end
94
+ end
95
+ end
96
+ ```
97
+
98
+ Include the ErrorEngine module in your base api class
99
+
100
+ ```ruby
101
+ include ::Marr::Api::ErrorEngine
102
+ ```
103
+
104
+ Next rescue all your api errors. This method could be in your base api class.
105
+
106
+ ```ruby
107
+ rescue_from 'Marr::ApiError' do |exception|
108
+ render exception.render, status: exception.status
109
+ end
110
+ ```
111
+
112
+ If you are custom rendering using a gem like Jbuilder you can do something like this:
113
+
114
+ ```ruby
115
+ # you would overide the custom_render in your class to return the file path you want to use
116
+ #=> 'api/internal/v1/errors/error'
117
+ rescue_from 'Marr::ApiError' do |error|
118
+ @error = error
119
+ render @error.render, status: @error.status
120
+ end
121
+ ```
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,7 @@
1
+ module Marr
2
+ module Api
3
+ module Error
4
+ VERSION = "0.1.0"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,28 @@
1
+ require 'marr/api/error/version'
2
+ require 'marr/configuration'
3
+ require 'marr/api_error'
4
+
5
+ module Marr
6
+ module Api
7
+ module Error
8
+ class << self
9
+ def configuration
10
+ @configuration ||= Configuration.new
11
+ end
12
+
13
+ def configure
14
+ yield(configuration)
15
+ load 'marr/api/error_engine'
16
+ end
17
+
18
+ def all_errors
19
+ return [] if self.configuration.namespaces.blank?
20
+
21
+ self.configuration.namespaces.map(&:constantize).map do |namespace|
22
+ namespace.constants.map { |x| { namespace: namespace, name: x.to_s.underscore } }
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,19 @@
1
+ require 'active_support'
2
+ require 'marr/api/error'
3
+
4
+ module Marr
5
+ module Api
6
+ module ErrorEngine
7
+ ::Marr::Api::Error.all_errors.each do |error|
8
+ name = error[:name]
9
+ namespace = error[:namespace]
10
+
11
+ define_method "raise_#{name}".to_sym do |*args|
12
+ api_error = "#{namespace}::#{name.camelcase}".constantize.new(args)
13
+
14
+ raise api_error
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,122 @@
1
+ # frozen_string_literal: true
2
+ require 'json'
3
+ require 'securerandom'
4
+
5
+ module Marr
6
+ class ApiError < StandardError
7
+ attr_accessor :resource, :object, :request, :response, :add_context, :add_error_context
8
+
9
+ def initialize(attributes={})
10
+ @controller = attributes[:controller]
11
+ @object = attributes[:object]
12
+ @resource = attributes[:resource] || 'Resource'
13
+ @subcode = attributes[:subcode]
14
+ @override_detail = attributes[:override_detail]
15
+ @override_status = attributes[:override_status]
16
+ @add_context = attributes[:add_context] || {}
17
+ @add_error_context = attributes[:add_error_context] || {}
18
+ @request = @controller.try(:request)
19
+ @response = @controller.try(:response)
20
+ set_resource
21
+ end
22
+
23
+ # Error name => code
24
+ # Status => status
25
+ # Subcodes => title
26
+ # message => detail
27
+ def object_errors
28
+ return [] unless @object.present?
29
+ return [] unless @object&.errors&.full_messages.present?
30
+
31
+ attributes = @object.errors.details.map(&:first)
32
+ @object_errors = []
33
+
34
+ attributes.each do |attrb|
35
+ @object.errors.full_messages_for(attrb).each do |msg|
36
+ error = { pointer: attrb.to_s }
37
+ error_with_message = error.merge(detail: msg)
38
+
39
+ @object_errors << error_with_message
40
+ end
41
+ end
42
+ @object_errors
43
+ end
44
+
45
+ def status(status: 422)
46
+ @override_status&.integer? ? @override_status : status
47
+ end
48
+
49
+ def message
50
+ raise NotImplementedError, 'Message must be implemented. Add Error message method.'
51
+ end
52
+
53
+ def subcodes(hash=nil)
54
+ return {} if hash.blank
55
+
56
+ hash.with_indifferent_access
57
+ end
58
+
59
+ def subcode
60
+ return '' unless subcodes[@subcode].present?
61
+
62
+ @subcode.to_s.camelcase
63
+ end
64
+
65
+ def detail
66
+ return '' unless @override_detail.present? || subcodes[@subcode].present?
67
+
68
+ @override_detail.present? ? override_detail : subcodes[@subcode]
69
+ end
70
+
71
+ def trace_id
72
+ trace_id ||= SecureRandom.hex(trace_id_length).upcase
73
+ end
74
+
75
+ def type
76
+ self.class.name.split('::').last
77
+ end
78
+
79
+ def render
80
+ if Marr::Api::Error.configuration.custom_render
81
+ custom_render
82
+ else
83
+ default_render.to_json
84
+ end
85
+ end
86
+
87
+ def custom_render
88
+ if Marr::Api::Error.configuration.custom_render
89
+ raise NotImplementedError, 'Message must be implemented. Add Error message method.'
90
+ else
91
+ nil
92
+ end
93
+ end
94
+
95
+ def default_render
96
+ {
97
+ errors: {
98
+ code: type,
99
+ title: subcode,
100
+ detail: message,
101
+ meta: {
102
+ object_errors: object_errors,
103
+ trace_id: trace_id,
104
+ }
105
+ }.merge(add_error_context)
106
+ }.merge(add_context)
107
+ end
108
+
109
+ private
110
+
111
+ def trace_id_length
112
+ Marr::Api::Error.configuration.trace_id_length / 2
113
+ end
114
+
115
+ def set_resource
116
+ return unless @object.present?
117
+ return unless @object.errors&.full_messages.present?
118
+
119
+ @resource = @object.class.name.split('::').last if @resource == 'Resource'
120
+ end
121
+ end
122
+ end
@@ -0,0 +1,9 @@
1
+ class Configuration
2
+ attr_accessor :namespaces, :trace_id_length, :custom_render
3
+
4
+ def initialize
5
+ @namespaces = []
6
+ @trace_id_length = 8
7
+ @custom_render = false
8
+ end
9
+ end
data/marr.gemspec ADDED
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/marr/api/error/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "marr" # Memereply Api Rescue Response aka marr
7
+ spec.version = Marr::Api::Error::VERSION
8
+ spec.authors = ["Memereply", "Micah Bowie"]
9
+ spec.email = ["engineering@memereply.io"]
10
+
11
+ spec.summary = "Dynamically rescue errors and render a structured JSON response to client applications"
12
+ spec.description = "Dynamically rescue errors and render a structured JSON response to client applications"
13
+ spec.homepage = "https://github.com/meme-reply/marr"
14
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
15
+
16
+ spec.metadata["homepage_uri"] = spec.homepage
17
+ spec.metadata["source_code_uri"] = "https://github.com/meme-reply/marr"
18
+
19
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
20
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
21
+ end
22
+ spec.bindir = "exe"
23
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ["lib"]
25
+
26
+ spec.add_development_dependency "bundler"
27
+ spec.add_development_dependency "rake"
28
+ spec.add_development_dependency "rspec"
29
+
30
+ spec.add_dependency "activesupport", " >= 6.0"
31
+ end
metadata ADDED
@@ -0,0 +1,120 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: marr
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Memereply
8
+ - Micah Bowie
9
+ autorequire:
10
+ bindir: exe
11
+ cert_chain: []
12
+ date: 2023-05-01 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: '0'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: '0'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rake
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rspec
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: activesupport
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: '6.0'
63
+ type: :runtime
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '6.0'
70
+ description: Dynamically rescue errors and render a structured JSON response to client
71
+ applications
72
+ email:
73
+ - engineering@memereply.io
74
+ executables: []
75
+ extensions: []
76
+ extra_rdoc_files: []
77
+ files:
78
+ - ".github/workflows/main.yml"
79
+ - ".gitignore"
80
+ - ".rspec"
81
+ - ".rubocop.yml"
82
+ - Gemfile
83
+ - Gemfile.lock
84
+ - LICENSE.txt
85
+ - README.md
86
+ - Rakefile
87
+ - bin/console
88
+ - bin/setup
89
+ - lib/marr/api/error.rb
90
+ - lib/marr/api/error/version.rb
91
+ - lib/marr/api/error_engine.rb
92
+ - lib/marr/api_error.rb
93
+ - lib/marr/configuration.rb
94
+ - marr.gemspec
95
+ homepage: https://github.com/meme-reply/marr
96
+ licenses: []
97
+ metadata:
98
+ homepage_uri: https://github.com/meme-reply/marr
99
+ source_code_uri: https://github.com/meme-reply/marr
100
+ post_install_message:
101
+ rdoc_options: []
102
+ require_paths:
103
+ - lib
104
+ required_ruby_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: 2.3.0
109
+ required_rubygems_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ requirements: []
115
+ rubygems_version: 3.0.3.1
116
+ signing_key:
117
+ specification_version: 4
118
+ summary: Dynamically rescue errors and render a structured JSON response to client
119
+ applications
120
+ test_files: []