First principles homogenization of periodic metamaterials and application to wire media