How to reduce number of hierarchical 'if statements'
In general, you can try inverting the condition on your outermost if block, and work your way in.  This will often result in less nesting, eg:

if condition1
  if condition2
    #ifblock
  else
    #elseblock2
  end
else
  #elseblock
end


Inside a loop, as you have your code, it would become this strcture, which is less nested:

if !condition
  #elseblock
  next
end
if !condition2
  #elseblock2
  next
end
#ifblock