-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrefresher_draft.rb
51 lines (42 loc) · 900 Bytes
/
refresher_draft.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
require 'dry/monads'
require 'dry/monads/do'
require 'securerandom'
#extend Dry::Monads[:maybe, :result]
#p None().to_result( {error: :some} )
class Refresher
include Dry::Monads[:result, :do]
def initialize(token:)
@token = token
end
def call
token = yield validate_token @token
new_token = yield create_new_token token#.value!
store_token new_token#.value!
end
def validate_token token
if true
Success(token)
else
Failure(:token_not_valid)
end
end
def create_new_token token
if true
Success(SecureRandom.hex<< ' ' << token)
else
Failure(:cannot_create_token)
end
end
def store_token token
if true
$db << token
Success(token)
else
Failure(:some_errors_when_store)
end
end
end
$db = []
token = Refresher.new(token: '1').call
#p token.failure?
p token.value_or{ |err| err }