breath 0.2.1 → 0.3.2

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: 76b40953cb6e71c98a7870cc2a90360ff11e7dbfeeacb38ba02ec212ac8995d3
4
- data.tar.gz: 7324b79742b9329015d3e527a6ed5c0abe5402e999933626fb912c2b004e017f
3
+ metadata.gz: b446e626bff570c9558dae487d91f2ba44ab0fabbfa7fe0638eb087ce49024d7
4
+ data.tar.gz: 601d49d45595acead536bdc6f9d29fa2821b9960f3664c44a6bb324058c87315
5
5
  SHA512:
6
- metadata.gz: f9c1a788f8de0f6765fc943e721f6d468fc44a57174951c5b0a4e02b9fe93971bd194d835e407dcbc8dac905997a88b1bf22fbb8d6166cd8580ddba857b3af87
7
- data.tar.gz: 53d89f967a23d992c47d9692f7c8cd9270a1f51422f9bfaf6b17b8a2d11d2cd53ba76eadc525ffd54d8b257e957910a3b089c913f1605dc2331e410dade80743
6
+ metadata.gz: 9c2dff25c5747ddb48469548c8bc0ac7b7ab5e18fd90586097f89f09f50c1648059545fd2c2ea3e8b9c6a780cb5e6f13ac8b30b99e8f1ab62de670c39360f67d
7
+ data.tar.gz: 4fc570dfabf9638035bf55404585143df9e39a23c185b6c7f32ba7f2faa661bd4df51585484d7b7a3e49b6f73b8c4e29d168b24b89b5751e4ce932daa870f22d
data/README.md CHANGED
@@ -126,6 +126,57 @@ end
126
126
  ```
127
127
  If you don't configure this, cookie is set permanently.
128
128
 
129
+ #### Error
130
+ When an error occurs when trying to log in or authorize, rescue from the error automatically.
131
+ Otherwise, you can also overwrite rescue method like bellow.<br/>
132
+ `app/controllers/users/application_controller.rb`
133
+ ```ruby
134
+ class Users::ApplicationController < ApplicationController
135
+ ...
136
+
137
+ def render_401(e)
138
+ # Write your programs. Here, e is error class.
139
+ # e.g. Rails.logger.error(e.to_s)
140
+
141
+ super({ error: 111, message: "error occur." })
142
+ end
143
+
144
+ ...
145
+ end
146
+ ```
147
+ An argument you pass to super is returned to the client side as JSON value.<br/>
148
+ In addition, breath plugin provides bellow rescue methods.
149
+ ```ruby
150
+ render_400
151
+ render_401 # Unauthorized.
152
+ render_404 # Not Found.
153
+ render_409 # Conflict.
154
+ render_422 # Unprocessable Entity.
155
+ render_500 # Internal Server Error.
156
+ ```
157
+ You can use these rescue methods in controllers like bellow.
158
+ ```ruby
159
+ class Users::HogeController < Users::ApplicationController
160
+ def index
161
+ ...
162
+ render status: 200
163
+ rescue => e
164
+ render_404 e
165
+ end
166
+
167
+ def update
168
+ ...
169
+ render status: 201
170
+ rescue => e
171
+ response_body = { error_code: 100, message: "error" }
172
+
173
+ render_409 response_body
174
+ end
175
+ end
176
+ ```
177
+ Breath plugin automatically rescues from CSRF token error which is status 422, and Internal Server Error with status code 500.<br/>
178
+ And you can overwrite these rescue methods.
179
+
129
180
  #### Last Work
130
181
  You need to create view side.<br/>
131
182
  In view side, you have remaining works.<br/>
@@ -5,12 +5,15 @@ module Breath
5
5
  class AuthenticationError < StandardError; end
6
6
 
7
7
  included do
8
- target_class = to_s.split("::")[-2].singularize.constantize
8
+ rescue_from StandardError, with: :render_500
9
+ rescue_from ActionController::InvalidAuthenticityToken, with: :render_422
10
+
11
+ target_class = to_s.deconstantize.demodulize.singularize.constantize
9
12
  target_name = target_class.to_s.underscore
10
13
  current_target = "current_#{target_name}"
11
14
 
12
15
  include ActionController::Cookies
13
-
16
+
14
17
  define_method :authenticate! do
15
18
  raise AuthenticationError unless cookies.key?("#{target_name}_id".to_sym)
16
19
  raise AuthenticationError if send(current_target).nil?
@@ -45,40 +48,40 @@ module Breath
45
48
 
46
49
  def render_400(res)
47
50
  Rails.logger.error error_message(res)
48
-
51
+
49
52
  render json: res, status: 400
50
53
  end
51
-
54
+
52
55
  def render_401(res)
53
56
  Rails.logger.error error_message(res)
54
-
57
+
55
58
  render json: res, status: 401
56
59
  end
57
-
60
+
58
61
  def render_404(res)
59
62
  Rails.logger.error error_message(res)
60
-
63
+
61
64
  render json: res, status: 404
62
65
  end
63
-
66
+
64
67
  def render_409(res)
65
68
  Rails.logger.error error_message(res)
66
-
69
+
67
70
  render json: res, status: 409
68
71
  end
69
72
 
70
73
  def render_422(res)
71
74
  Rails.logger.error error_message(res)
72
-
75
+
73
76
  render json: res, status: 422
74
77
  end
75
-
78
+
76
79
  def render_500(res)
77
80
  Rails.logger.error error_message(res)
78
-
81
+
79
82
  render json: res, status: 500
80
83
  end
81
-
84
+
82
85
  def error_message(error)
83
86
  "[ERROR] #{error.to_s}"
84
87
  end
@@ -7,9 +7,7 @@ module Breath
7
7
  class InvalidPassword < StandardError; end
8
8
 
9
9
  included do
10
- rescue_from ActionController::InvalidAuthenticityToken, with: :render_422
11
-
12
- target_class = to_s.split("::")[-2].singularize.constantize
10
+ target_class = to_s.deconstantize.demodulize.singularize.constantize
13
11
  target_name = target_class.to_s.underscore
14
12
  current_target = "current_#{target_name}"
15
13
 
@@ -1,3 +1,3 @@
1
1
  module Breath
2
- VERSION = "0.2.1"
2
+ VERSION = "0.3.2"
3
3
  end
data/lib/breath.rb CHANGED
File without changes
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: breath
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - testCodeV01
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-09-27 00:00:00.000000000 Z
11
+ date: 2023-09-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails