xkcd 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/xkcd.rb +9 -15
- metadata +3 -3
data/lib/xkcd.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
|
-
#!/usr/bin/
|
2
|
-
require '
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'hpricot'
|
3
3
|
require 'open-uri'
|
4
4
|
# The main XKCD driver
|
5
5
|
class XKCD
|
@@ -12,21 +12,15 @@ class XKCD
|
|
12
12
|
# >> XKCD.comic
|
13
13
|
# => "http://xkcd.com/891/"
|
14
14
|
|
15
|
-
def self.comic
|
16
|
-
|
17
|
-
uri = URI.parse 'http://dynamic.xkcd.com/random/comic/'
|
18
|
-
req = Net::HTTP::Get.new(uri.request_uri)
|
19
|
-
http = Net::HTTP.new(uri.host)
|
20
|
-
res = http.start { |server|
|
21
|
-
server.request(req)
|
22
|
-
}
|
23
|
-
res["location"]
|
15
|
+
def self.comic
|
16
|
+
open('http://dynamic.xkcd.com/random/comic/').base_uri.to_s
|
24
17
|
end
|
25
18
|
|
19
|
+
class << XKCD
|
20
|
+
alias_method :get, :comic
|
21
|
+
end
|
22
|
+
|
26
23
|
def self.img
|
27
|
-
|
28
|
-
html = open(url).read()
|
29
|
-
imgs = URI.extract(html).select{ |l| l[/comics\//]}
|
30
|
-
imgs.first
|
24
|
+
Hpricot(open('http://dynamic.xkcd.com/random/comic/')).search("#comic img").first.raw_attributes["src"]
|
31
25
|
end
|
32
26
|
end
|
metadata
CHANGED