lib/jok/joke_factory.rb in jok-0.0.1 vs lib/jok/joke_factory.rb in jok-0.0.3
- old
+ new
@@ -36,14 +36,28 @@
end
private
def dom(method="")
- response_body = HTTParty.get("#{BASE_URI}/#{method.gsub("_", "-")}")
- dom = Nokogiri::HTML(response_body)
+ topic = "#{BASE_URI}/#{method.gsub("_", "-")}"
+ response_body = HTTParty.get(topic)
+ dom_tree = Nokogiri::HTML(response_body)
+
+ return dom_tree if method == ""
+
+ pagination_items = dom_tree.xpath('//div[@class="pagination"]/ul/li').map(&:text).map(&:strip)
+
+ if pagination_items.length > 0
+ selected_item = pagination_items[srand % (pagination_items.length - 1)]
+
+ response_body = HTTParty::get("#{topic}/#{selected_item}")
+ Nokogiri::HTML(response_body)
+ else
+ dom_tree
+ end
end
def joke(topic="")
- dom(topic).xpath('//div[@class="joke3-pop-box"]/p').map(&:text).map(&:strip)
+ dom(topic).xpath('//div[@class="joke3-pop-box"]/p[substring-before(@id, "_") = "joke"]').map(&:text).map(&:strip)
end
end
end