use ensure_valid_encoding gem instead of local implementation