lib/urban_dictionary/word.rb in urban_dictionary-0.0.1 vs lib/urban_dictionary/word.rb in urban_dictionary-0.0.2

- old
+ new

@@ -8,15 +8,17 @@ # Can raise SocketError if unable to connect to specified URL def self.from_url(url) html = open(url).read doc = Nokogiri::HTML(html) - word = doc.css('.word').first.content.strip - definitions = doc.css('.definition').map{|d| d.content.strip } - examples = doc.css('.example').map{|e| e.content.strip } - entries = definitions.zip(examples).map{|d,e| Entry.new(d, e)} + if doc.css('.word').any? + word = doc.css('.word').first.content.strip + definitions = doc.css('.definition').map{|d| d.content.strip } + examples = doc.css('.example').map{|e| e.content.strip } + entries = definitions.zip(examples).map{|d,e| Entry.new(d, e)} - Word.new(word, entries) + Word.new(word, entries) + end end def initialize(word, entries) @word = word @entries = entries \ No newline at end of file