The electrical box needs to hang down below the ridge, so that the flat round fan base has clearance. But you don't want to see the box.
You must hold it there very solidly, cover the electrical box, and deal with the void between it and the peak.
Here is my home-crafted solution.
A fan-mount electrical box is attached to the end securely, aligned with the drilled hole.
The top corners of the block are cut away to get the electrical box to hang down to the required depth without the rest of the 6x6 being in the way.
All of this is nailed up in between wood blocking to hold it in the right place.
The 5 1/2" dimension just happens to be exactly the diameter of the fan canopy.
A square mount would have been easier to make, but the round one will stand out less.