module Jok class JokeFactory BASE_URI = "http://www.laughfactory.com/jokes/".freeze def list @list ||= begin dom.xpath('//div[@class="left-nav"]/ul/li/a').map(&:text) end end def da joke end def method_missing(method, *args, &block) method = method.to_s super unless method_list.include? method self.class.class_eval <<-EOT, __FILE__, __LINE__ + 1 def #{method} @#{method} ||= begin joke("#{method}") end end EOT send method end def method_list @method_list ||= begin list.map do |element| element.split(" ").map(&:downcase).join("_") end.push "da" end end private def dom(method="") response_body = HTTParty.get("#{BASE_URI}/#{method.gsub("_", "-")}") dom = Nokogiri::HTML(response_body) end def joke(topic="") dom(topic).xpath('//div[@class="joke3-pop-box"]/p').map(&:text).map(&:strip) end end end