Skip to main content

Success

And finally, success.

What is a success?

Success is a kind of result when the service objective is fully satisfied.

All errors and failures preconditions are met.

Every validation check is passed.

There is no obstacle that prohibits achieving the service goal.

In general, a service must have one and only one reason to return success.

note

No sense to hide the truth, sometimes it is really necessary to return multiple success cases from a single service (For example, fake HTTP responses for external testing systems).

As a rule of thumb, if you start to feel that you have such a scenario, always prefer to think about how to create multiple services instead.

If not, be ready to strongly argue why your situation is truly exceptional and unavoidable.

Success data

Success results may have some data (So-called Boolean Services are often without success data).

Data is a hash-like structure with values received by service processing.

Example:

class FetchPopularVideos
# ...
def result
# ...
# Actual logic to fetch videos.
# ...
success(
own_channel_videos: own_channel_videos,
followers_videos: followers_videos,
all_videos: own_channel_videos.concat(followers_videos)
)
end
end

With invocation:

result = FetchPopularVideos.result

if result.success?
# No `data` checks here, since `result.success?` guarantees that `result.data[:all_videos]` are 100% valid.
markup = result.data[:all_videos].map { |video| generate_markup(video) }.join
# ...
end