Quick RSS Validation
February 04, 2014If you're using Ruby and RSpec with RSS you can save a lot of quick checking time by using functional programming.
After loading in the RSS feed, you can use the following transforms on the data:
def descriptions
items.map(&:description)
end
def missing_any?(field_sym)
fields = send(field_sym)
fields.any?(&:nil?) && fields.any?(&:empty?)
end
In Gherkin/RSpec, we access them with:
Then(/^the feed items have descriptions$/) do
expect(current_feed).to_not be_missing_any(:descriptions),
'some missing descriptions in feed.'
end
If you aren't using be_*
in RSpec, you should be. It is a great way to ensure that your predicate-form methods are describing what they do. There's a certain 'smell' to some predicate methods.
For example, I started out here with feed.not_null_or_empty?(field_name)
which was just weird looking as .to be_not_null_or_empty?
and it garbled up the code in the method as well trying to negate things. missing_any?
gets at my intent much better.