lib/xkcd.rb in xkcd-0.0.3 vs lib/xkcd.rb in xkcd-0.0.4
- old
+ new
@@ -1,7 +1,7 @@
-#!/usr/bin/evn ruby
-require 'net/http'
+#!/usr/bin/env ruby
+require 'hpricot'
require 'open-uri'
# The main XKCD driver
class XKCD
# Get img/comic URL from xkcd
#
@@ -10,23 +10,17 @@
# => "http://imgs.xkcd.com/comics/hell.png"
#
# >> XKCD.comic
# => "http://xkcd.com/891/"
- def self.comic()
- # This method gets random comic links from xkcd, the uri is the redirected location found in the resp headers
- uri = URI.parse 'http://dynamic.xkcd.com/random/comic/'
- req = Net::HTTP::Get.new(uri.request_uri)
- http = Net::HTTP.new(uri.host)
- res = http.start { |server|
- server.request(req)
- }
- res["location"]
+ def self.comic
+ open('http://dynamic.xkcd.com/random/comic/').base_uri.to_s
end
+ class << XKCD
+ alias_method :get, :comic
+ end
+
def self.img
- url = 'http://dynamic.xkcd.com/random/comic/'
- html = open(url).read()
- imgs = URI.extract(html).select{ |l| l[/comics\//]}
- imgs.first
+ Hpricot(open('http://dynamic.xkcd.com/random/comic/')).search("#comic img").first.raw_attributes["src"]
end
end