responsable 1.0.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: 89f9a11c6cabcab46760327a3b00c0ea696c2218d19a001c0efa1100855f8311
4
+ data.tar.gz: 845c953d19e8fba04cdb2fe1f13d80e7d93b1cdf7c58ca76908a6f4231f40ad6
5
+ SHA512:
6
+ metadata.gz: 4784e9232b0fc5ac2ad23539624510685a15cb18a83184576beb6f5fb42411d083a1413e1a4b85e8cc1d8f52933bf3b00073471d372a4980e3d366b361799d31
7
+ data.tar.gz: 4538867170df252c6b063733f5ba17c011f4fb97ca8c683cedb424dbc8a02e5ac5018ddc5dc3d0fc2397a58f272a4d89c3ca3240f9adbebef9209ac0fb2e1fc4
data/.editorconfig ADDED
@@ -0,0 +1,12 @@
1
+ root = true
2
+
3
+ [*]
4
+ charset = utf-8
5
+ end_of_line = lf
6
+ insert_final_newline = true
7
+ trim_trailing_whitespace = true
8
+ indent_style = space
9
+ indent_size = 2
10
+
11
+ [*.md]
12
+ trim_trailing_whitespace = false
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,3 @@
1
+ inherit_gem:
2
+ ruboguide:
3
+ - styles/rubocop-rails.yml
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in responsable.gemspec
6
+ gemspec
7
+
8
+ gem 'rake', '~> 13.0'
9
+
10
+ gem 'rspec', '~> 3.0'
11
+
12
+ gem 'ruboguide', '~> 1.0'
data/Gemfile.lock ADDED
@@ -0,0 +1,92 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ responsable (0.1.0)
5
+ dry-configurable (~> 0.16)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activesupport (7.0.4.3)
11
+ concurrent-ruby (~> 1.0, >= 1.0.2)
12
+ i18n (>= 1.6, < 2)
13
+ minitest (>= 5.1)
14
+ tzinfo (~> 2.0)
15
+ ast (2.4.2)
16
+ concurrent-ruby (1.2.2)
17
+ diff-lcs (1.5.0)
18
+ dry-configurable (0.16.1)
19
+ dry-core (~> 0.6)
20
+ zeitwerk (~> 2.6)
21
+ dry-core (0.9.1)
22
+ concurrent-ruby (~> 1.0)
23
+ zeitwerk (~> 2.6)
24
+ i18n (1.12.0)
25
+ concurrent-ruby (~> 1.0)
26
+ minitest (5.18.0)
27
+ parallel (1.22.1)
28
+ parser (3.2.1.1)
29
+ ast (~> 2.4.1)
30
+ rack (2.2.6.4)
31
+ rainbow (3.1.1)
32
+ rake (13.0.6)
33
+ regexp_parser (2.7.0)
34
+ rexml (3.2.5)
35
+ rspec (3.12.0)
36
+ rspec-core (~> 3.12.0)
37
+ rspec-expectations (~> 3.12.0)
38
+ rspec-mocks (~> 3.12.0)
39
+ rspec-core (3.12.1)
40
+ rspec-support (~> 3.12.0)
41
+ rspec-expectations (3.12.2)
42
+ diff-lcs (>= 1.2.0, < 2.0)
43
+ rspec-support (~> 3.12.0)
44
+ rspec-mocks (3.12.5)
45
+ diff-lcs (>= 1.2.0, < 2.0)
46
+ rspec-support (~> 3.12.0)
47
+ rspec-support (3.12.0)
48
+ rubocop (1.23.0)
49
+ parallel (~> 1.10)
50
+ parser (>= 3.0.0.0)
51
+ rainbow (>= 2.2.2, < 4.0)
52
+ regexp_parser (>= 1.8, < 3.0)
53
+ rexml
54
+ rubocop-ast (>= 1.12.0, < 2.0)
55
+ ruby-progressbar (~> 1.7)
56
+ unicode-display_width (>= 1.4.0, < 3.0)
57
+ rubocop-ast (1.27.0)
58
+ parser (>= 3.2.1.0)
59
+ rubocop-performance (1.12.0)
60
+ rubocop (>= 1.7.0, < 2.0)
61
+ rubocop-ast (>= 0.4.0)
62
+ rubocop-rails (2.12.4)
63
+ activesupport (>= 4.2.0)
64
+ rack (>= 1.1)
65
+ rubocop (>= 1.7.0, < 2.0)
66
+ rubocop-rake (0.6.0)
67
+ rubocop (~> 1.0)
68
+ rubocop-rspec (2.6.0)
69
+ rubocop (~> 1.19)
70
+ ruboguide (1.0.0)
71
+ rubocop (= 1.23.0)
72
+ rubocop-performance (= 1.12.0)
73
+ rubocop-rails (= 2.12.4)
74
+ rubocop-rake (= 0.6.0)
75
+ rubocop-rspec (= 2.6.0)
76
+ ruby-progressbar (1.13.0)
77
+ tzinfo (2.0.6)
78
+ concurrent-ruby (~> 1.0)
79
+ unicode-display_width (2.4.2)
80
+ zeitwerk (2.6.7)
81
+
82
+ PLATFORMS
83
+ x86_64-linux
84
+
85
+ DEPENDENCIES
86
+ rake (~> 13.0)
87
+ responsable!
88
+ rspec (~> 3.0)
89
+ ruboguide (~> 1.0)
90
+
91
+ BUNDLED WITH
92
+ 2.4.10
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Mario Rios
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,40 @@
1
+ # Responsable
2
+
3
+ Responsable is a gem to standardize response objects and messages accross all Researchable's services.
4
+
5
+ ## Installation
6
+
7
+
8
+ Add this to your Gemfile:
9
+ ```ruby
10
+ gem 'responsable', '~> 1.0'
11
+ ```
12
+
13
+ Then execute:
14
+ ```bash
15
+ $ bundle install
16
+ ```
17
+
18
+ ## Usage
19
+
20
+ In your application controller, include the `Responsable` module:
21
+
22
+ ```ruby
23
+ class ApplicationController < ActionController::Base
24
+ include Responsable
25
+
26
+ # rest of the controller
27
+ end
28
+ ```
29
+
30
+ Then, in your controller actions, you can use any of the available response methods in lib/responsable.rb
31
+
32
+ ## Development
33
+
34
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
35
+
36
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
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]
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Responsable
4
+ VERSION = '1.0.0'
5
+ end
@@ -0,0 +1,139 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'responsable/version'
4
+ require 'dry-configurable'
5
+
6
+ # The {#Responsable} module provides a consistent way to render json responses across all Researchable's services
7
+ module Responsable
8
+ extend Dry::Configurable
9
+ setting :serializer, default: proc { |resource| resource.as_json }
10
+
11
+ # Method to render a resource in a consistent way. If the resource is not valid it will render a validation error
12
+ # @param resource the resource to render
13
+ def render_resource(resource)
14
+ if resource.errors.empty?
15
+ render json: resource
16
+ else
17
+ validation_error(resource.errors)
18
+ end
19
+ end
20
+
21
+ # Method to render validation errors in a consistent way
22
+ # @param resource_errors the errors to render
23
+ def validation_error(resource_errors)
24
+ render json: {
25
+ errors: [
26
+ {
27
+ status: '400',
28
+ title: 'Bad Request',
29
+ detail: resource_errors,
30
+ code: '100'
31
+ }
32
+ ]
33
+ }, status: :bad_request
34
+ end
35
+
36
+ # Method to render access denied errors in a consistent way
37
+ # @param resource_errors the errors to render
38
+ def access_denied(resource_errors)
39
+ render json: {
40
+ errors: [
41
+ {
42
+ status: '403',
43
+ title: 'Access Denied',
44
+ detail: resource_errors,
45
+ code: '100'
46
+ }
47
+ ]
48
+ }, status: :forbidden
49
+ end
50
+
51
+ # Method to render not found errors in a consistent way
52
+ # @param resource_errors the errors to render
53
+ def not_found(resource_errors)
54
+ render json: {
55
+ errors: [
56
+ {
57
+ status: '404',
58
+ title: 'Not Found',
59
+ detail: resource_errors,
60
+ code: '100'
61
+ }
62
+ ]
63
+ }, status: :not_found
64
+ end
65
+
66
+ # Method to render created status in a consistent way
67
+ # @param resource the resource to render
68
+ def created(resource = nil)
69
+ render json: {
70
+ result: [
71
+ {
72
+ status: '201',
73
+ title: 'created',
74
+ detail: 'resource created',
75
+ code: '100',
76
+ instance: Responsable.config.serializer.call(resource)
77
+ }
78
+ ]
79
+ }, status: :created
80
+ end
81
+
82
+ # Method to render success status in a consistent way
83
+ # @param resource the resource to render
84
+ def render_success_resource(resource)
85
+ render json: {
86
+ result: [
87
+ {
88
+ status: '200',
89
+ title: 'Success',
90
+ detail: resource,
91
+ code: '100'
92
+ }
93
+ ]
94
+ }, status: :ok
95
+ end
96
+
97
+ # Method to render destroyed status in a consistent way
98
+ def destroyed
99
+ render json: {
100
+ result: [
101
+ {
102
+ status: '200',
103
+ title: 'destroyed',
104
+ detail: 'resource destroyed',
105
+ code: '100'
106
+ }
107
+ ]
108
+ }, status: :ok
109
+ end
110
+
111
+ # Method to render unprocessable entity errors in a consistent way
112
+ # @param resource_errors the errors to render
113
+ def unprocessable_entity(resource_errors)
114
+ render json: {
115
+ errors: [
116
+ {
117
+ status: '422',
118
+ title: 'unprocessable',
119
+ detail: resource_errors,
120
+ code: '100'
121
+ }
122
+ ]
123
+ }, status: :unprocessable_entity
124
+ end
125
+
126
+ # Method to render no content in a consistent way
127
+ def no_content
128
+ render json: {
129
+ result: [
130
+ {
131
+ status: '204',
132
+ title: 'no content',
133
+ detail: 'no content provided',
134
+ code: '100'
135
+ }
136
+ ]
137
+ }, status: :no_content
138
+ end
139
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/responsable/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'responsable'
7
+ spec.version = Responsable::VERSION
8
+ spec.authors = ['Researchable']
9
+ spec.email = ['info@researchable.nl']
10
+
11
+ spec.summary = "Researchable's gem to standardize http REST responses"
12
+ spec.description = "Researchable's gem to standardize http REST responses"
13
+ spec.homepage = "https://gitlab.com/researchable/general/gems/responsable/-/blob/v#{Responsable::VERSION}/README.md"
14
+ spec.license = 'MIT'
15
+ spec.required_ruby_version = '>= 3.0'
16
+
17
+ spec.metadata['homepage_uri'] = spec.homepage
18
+ spec.metadata['source_code_uri'] = 'https://gitlab.com/researchable/general/gems/responsable'
19
+ spec.metadata['changelog_uri'] = "https://gitlab.com/researchable/general/gems/responsable/-/blob/v#{Responsable::VERSION}/CHANGELOG.md"
20
+
21
+ # Specify which files should be added to the gem when it is released.
22
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23
+ spec.files = Dir.chdir(__dir__) do
24
+ `git ls-files -z`.split("\x0").reject do |f|
25
+ (File.expand_path(f) == __FILE__) || f.start_with?(*%w[bin/ test/ spec/ features/ .git .circleci appveyor])
26
+ end
27
+ end
28
+ spec.bindir = 'exe'
29
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
30
+ spec.require_paths = ['lib']
31
+
32
+ # Register gem dependencies here
33
+ spec.add_dependency 'dry-configurable', '~> 0.16'
34
+
35
+ # For more information and examples about making a new gem, check out our
36
+ # guide at: https://bundler.io/guides/creating_gem.html
37
+ spec.metadata = {
38
+ 'rubygems_mfa_required' => 'true'
39
+ }
40
+ end
@@ -0,0 +1,4 @@
1
+ module Responsable
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: responsable
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Researchable
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-04-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: dry-configurable
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.16'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.16'
27
+ description: Researchable's gem to standardize http REST responses
28
+ email:
29
+ - info@researchable.nl
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".editorconfig"
35
+ - ".rspec"
36
+ - ".rubocop.yml"
37
+ - Gemfile
38
+ - Gemfile.lock
39
+ - LICENSE.txt
40
+ - README.md
41
+ - Rakefile
42
+ - lib/responsable.rb
43
+ - lib/responsable/version.rb
44
+ - responsable.gemspec
45
+ - sig/responsable.rbs
46
+ homepage: https://gitlab.com/researchable/general/gems/responsable/-/blob/v1.0.0/README.md
47
+ licenses:
48
+ - MIT
49
+ metadata:
50
+ rubygems_mfa_required: 'true'
51
+ post_install_message:
52
+ rdoc_options: []
53
+ require_paths:
54
+ - lib
55
+ required_ruby_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: '3.0'
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ requirements: []
66
+ rubygems_version: 3.3.26
67
+ signing_key:
68
+ specification_version: 4
69
+ summary: Researchable's gem to standardize http REST responses
70
+ test_files: []