Sha256: e099135461f94f2033b3338a4ce88649f191558dfbb802aaf5e9298fd6eec60d

Contents?: true

Size: 878 Bytes

Versions: 2

Compression:

Stored size: 878 Bytes

Contents

#!/usr/bin/evn ruby
require 'net/http'
require 'open-uri'
# The main XKCD driver
class XKCD
    # Get img/comic URL from xkcd
    #
    # Example:
    #   >> XKCD.img
    #   => "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"]
    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       
    end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
xkcd-0.0.8 lib/xkcd.rb
xkcd-0.0.3 lib/xkcd.rb