Skip to content

Commit

Permalink
cleaned up XKCD#comic
Browse files Browse the repository at this point in the history
  • Loading branch information
Fraser Murray committed May 20, 2012
1 parent 72d39dd commit e482a5d
Showing 1 changed file with 3 additions and 10 deletions.
13 changes: 3 additions & 10 deletions lib/xkcd.rb
Expand Up @@ -13,20 +13,13 @@ class XKCD
# => "http://xkcd.com/891/" # => "http://xkcd.com/891/"


def self.comic() def self.comic()
# This method gets random comic links from xkcd, the uri is the redirected location found in the resp headers open('http://dynamic.xkcd.com/random/comic/').base_uri.to_s
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"]
end end


def self.img def self.img
url = 'http://dynamic.xkcd.com/random/comic/' url = 'http://dynamic.xkcd.com/random/comic/'
html = open(url).read() html = open(url).read()
imgs = URI.extract(html).select{ |l| l[/comics\//]} imgs = URI.extract(html).select{ |l| l[/comics\//]}
imgs.first imgs.first
end end
end end

0 comments on commit e482a5d

Please sign in to comment.