Skip to content
Browse files

Merge pull request #1 from intolerable/master

Cleaned up lib/xkcd.rb and added XKCD.get to unsure tests pass
  • Loading branch information...
2 parents 72d39dd + 6fa75f0 commit eea20e1203f1a725e75e6714181ad927022e201d @hemanth committed May 20, 2012
Showing with 9 additions and 15 deletions.
  1. +9 −15 lib/xkcd.rb
View
24 lib/xkcd.rb
@@ -1,5 +1,5 @@
-#!/usr/bin/evn ruby
-require 'net/http'
+#!/usr/bin/env ruby
+require 'hpricot'
require 'open-uri'
# The main XKCD driver
class XKCD
@@ -12,21 +12,15 @@ class XKCD
# >> 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

0 comments on commit eea20e1

Please sign in to comment.
Something went wrong with that request. Please try again.