breath 0.1.3 → 0.3.1

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: 1cf89b5445e113d7198d4a9a9aa4ddfc1af8a09c0faec9eb4af344ad151b2af1
4
- data.tar.gz: 3efb2e128154bc6e7c0ecbfad3997d338f15d871f7c898d3ab943d53e7aecd91
3
+ metadata.gz: f7a68cdfbe3fc69697178c18e1d8f73d62028a2a3d659334daaa06724333f568
4
+ data.tar.gz: ad43f74a1fedfaf4c900c5b78f8d99f59098c1a516bb87ab08b19447dcf3a2e3
5
5
  SHA512:
6
- metadata.gz: 1e267a0b8ec0aee65ad28e47d3c3e58daceddb9f37e019c6ea548e61906af86d413ab266c64b35a444c3bac4876c67cb27578a737984d79b8e9161dfdbc9f4d8
7
- data.tar.gz: 1b2d1fbbb58ac30cd6e7a3dd3283ba33f807724cf962e96f1cbc1b0c228bb7fcda18854ee27a368173616fc5bb46b373ab0162e3f3e401ee4e31c32c7ff88efc
6
+ metadata.gz: df992b967d1f6ec8e5ef6339323c5a42a6b680b4e819b6a7c15eb046971e4c000c4ff9569705fd277dc2201be3711988b6b8ea3c70a2e77b6c2f3e760dfa6886
7
+ data.tar.gz: 6e206371189f9c6aea5651daa5fcf75c6341948b7f6ff9ee996314c8a9584f3ebaef87db6bedde7cdaf6960801897d85ca542cc80e675f03d92194a3af5db98a
data/README.md CHANGED
@@ -86,6 +86,24 @@ end
86
86
  `Breath::ApplicationControllerHelper` intoroduce the user's authorization.<br/>
87
87
  `Breath::SessionsControllerHelper` introduce the actions `login`, and `logout`.
88
88
 
89
+ Then, you don't need write the codes to introduce authorizations.<br/>
90
+
91
+ You can use `current_user` method which is current logined user.
92
+
93
+ #### Route
94
+ Write `route.rb`
95
+ ```ruby
96
+ Rails.application.routes.draw do
97
+ breath :users
98
+
99
+ ...or...
100
+
101
+ breath :users do
102
+ get "test" => "sessions#test"
103
+ end
104
+ end
105
+ ```
106
+
89
107
  After you added these lines, show `bundle exec rails routes` command.<br/>
90
108
  You can see these routes are added.
91
109
  ```
@@ -93,8 +111,7 @@ GET /users/login
93
111
  POST /users/login
94
112
  DELETE /users/logout
95
113
  ```
96
-
97
- Then, you don't need write the codes to introduce authorizations.
114
+ Or, nested users routes.
98
115
 
99
116
  #### Config
100
117
  This plugin need cookie, and you can configure the cookie expires like bellow.<br/>
@@ -109,6 +126,67 @@ end
109
126
  ```
110
127
  If you don't configure this, cookie is set permanently.
111
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
+ render_409 e
172
+ end
173
+ end
174
+ ```
175
+ Breath plugin automatically rescues from CSRF token error which is status 422, and Internal Server Error with status code 500.<br/>
176
+ And you can overwrite these rescue methods.
177
+ ```ruby
178
+ class Users::ApplicationController < ApplicationController
179
+ ...
180
+
181
+ def render_500(e)
182
+ ...
183
+ super
184
+ end
185
+
186
+ ...
187
+ end
188
+ ```
189
+
112
190
  #### Last Work
113
191
  You need to create view side.<br/>
114
192
  In view side, you have remaining works.<br/>
@@ -5,6 +5,10 @@ module Breath
5
5
  class AuthenticationError < StandardError; end
6
6
 
7
7
  included do
8
+ rescue_from StandardError, with: :render_500
9
+ rescue_from ActionController::InvalidAuthenticityToken, with: :render_422
10
+
11
+
8
12
  target_class = to_s.split("::")[-2].singularize.constantize
9
13
  target_name = target_class.to_s.underscore
10
14
  current_target = "current_#{target_name}"
@@ -20,7 +24,7 @@ module Breath
20
24
 
21
25
  raise AuthenticationError if target.nil?
22
26
  rescue StandardError => e
23
- send :render_401, e.to_s
27
+ send :render_401, e
24
28
  end
25
29
 
26
30
  define_method current_target do
@@ -66,6 +70,12 @@ module Breath
66
70
 
67
71
  render json: res, status: 409
68
72
  end
73
+
74
+ def render_422(res)
75
+ Rails.logger.error error_message(res)
76
+
77
+ render json: res, status: 422
78
+ end
69
79
 
70
80
  def render_500(res)
71
81
  Rails.logger.error error_message(res)
@@ -29,7 +29,7 @@ module Breath
29
29
 
30
30
  render status: 200
31
31
  rescue StandardError => e
32
- send :render_401, e.to_s
32
+ send :render_401, e
33
33
  end
34
34
 
35
35
  # DELETE /schedule_kun/target/logout
@@ -1,3 +1,3 @@
1
1
  module Breath
2
- VERSION = "0.1.3"
2
+ VERSION = "0.3.1"
3
3
  end
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.1.3
4
+ version: 0.3.1
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-24 00:00:00.000000000 Z
11
+ date: 2023-09-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails