Skip to content

Instantly share code, notes, and snippets.

@davydovanton
Last active February 20, 2019 13:06
Show Gist options
  • Save davydovanton/f21d23b8d56ab76e8de7a0acd2e13265 to your computer and use it in GitHub Desktop.
Save davydovanton/f21d23b8d56ab76e8de7a0acd2e13265 to your computer and use it in GitHub Desktop.
dry_monads_with_handler
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