Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Cleaned up lib/xkcd.rb and added XKCD.get to unsure tests pass #1

Merged
merged 4 commits into from

2 participants

@intolerable

No description provided.

@intolerable

herpdy derp, *ensure

@hemanth hemanth merged commit eea20e1 into hemanth:master
@hemanth
Owner

@intolerable That was enlightening! :)

@intolerable

happy to help ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 20, 2012
  1. cleaned up XKCD#comic

    Fraser Murray authored
  2. switched evn for env

    Fraser Murray authored
  3. tests now pass, aliased comic => get

    Fraser Murray authored
  4. using Hpricot for a cleaner #img method

    Fraser Murray authored
This page is out of date. Refresh to see the latest.
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
Something went wrong with that request. Please try again.