Forked from davydovanton/dry_monads_with_handler.rb
Created
February 20, 2019 13:06
-
-
Save aablinov/30f693edaf13a8ace178a2c0f7ec13a4 to your computer and use it in GitHub Desktop.
dry_monads_with_handler
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def result_handler(result) | |
| case result | |
| when Success, Some | |
| puts 'success, user created' | |
| when Failure { |message, _| message == :http_error } | |
| puts "HTTP request error, #{result.failure[2]}" | |
| when Failure { |message, _| message == :validation_error } | |
| puts "validation error, #{result.failure[2]}" | |
| when Failure(ROM::Error) | |
| puts 'User not created' | |
| end | |
| end | |
| def save_user_from_url(url, params) | |
| http.get(url, params) | |
| .bind { |body| validator.call(body).to_result } | |
| .bind { |payload| Try(ROM::Error) { (user_repository.create(payload) } } | |
| .fmap { |user| NotificationWorker.perform_async(user.id) } | |
| end | |
| result_handler save_user_from_url('site.com/posts/1', option: 1) # => HTTP request error | |
| result_handler save_user_from_url('site.com/users/1', option: :invalid) # => validation Error | |
| result_handler save_user_from_url('site.com/users/1', option: :dublicated) # => persistance Error | |
| result_handler save_user_from_url('site.com/users/1', option: :valid) # => 'success, user created' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment