Why did Hamas attack, and why now? What does it hope to gain?

The operation reflects a pattern of four wars and regular outbreaks of violence between Israel and Hamas militants in Gaza since 2005, when Israel withdrew its military posts and forcibly removed 9,000 Israeli settlers from the territory.

Source: Why did Hamas attack, and why now? What does it hope to gain?