Sha256: 46ceea7def645991021e2a5ce9af1393a2a5571409b139d1000d463b6f9108a0
Contents?: true
Size: 1.45 KB
Versions: 1
Compression:
Stored size: 1.45 KB
Contents
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="") 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[substring-before(@id, "_") = "joke"]').map(&:text).map(&:strip) end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
jok-0.0.3 | lib/jok/joke_factory.rb |