Permalink
Browse files

Avoided parsing all together and using json API, a bit slow but more …

…robust, also added License file
  • Loading branch information...
hemanth committed May 21, 2012
1 parent ad04942 commit 3200c3f0dbf7ed152762172199b49fde88695ead
Showing with 14 additions and 2 deletions.
  1. +1 −0 LICENSE
  2. +12 −1 lib/xkcd.rb
  3. +1 −1 xkcd.gemspec
View
@@ -0,0 +1 @@
+GNU AFFERO GENERAL PUBLIC LICENSE
View
@@ -1,6 +1,8 @@
#!/usr/bin/env ruby
require 'nokogiri'
require 'open-uri'
+require 'json'
+
# The main XKCD driver
class XKCD
# Get img/comic URL from xkcd
@@ -19,11 +21,20 @@ def self.comic
class << XKCD
alias_method :get, :comic
end
-
+
+=begin
def self.img
img = Nokogiri::HTML(open('http://dynamic.xkcd.com/random/comic/')).css('#comic img')[0]
img_url = img.attributes["src"].value
img_title = img.attributes["title"].value
"#{img_title} : #{img_url}"
end
+=end
+ def self.img
+ max = JSON.parse(open('http://xkcd.com/info.0.json').read)["num"]
+ comic_num = Random.rand(max)
+ comic_num = 1 if comic_num == 404 # Avoid 404th comic ;)
+ comic = JSON.parse(open("http://xkcd.com/#{random_comic}/info.0.json").read)
+ "#{comic['alt']} : #{comic['img']}"
+ end
end
View
@@ -1,6 +1,6 @@
Gem::Specification.new do |s|
s.name = "xkcd"
- s.version = "0.0.6"
+ s.version = "0.0.7"
s.default_executable = "xkcd"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=

0 comments on commit 3200c3f

Please sign in to comment.