Could you do if res and res.code == 200 and res.body =~ ..... ?? In this way we can delete this nested if which helps to reduce code complexity