Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Gem is ready

  • Loading branch information...
commit 7a3d7bb74166434aa02b831c9b52dd5beec45e78 0 parents
@hemanth authored
8 Rakefile
@@ -0,0 +1,8 @@
+require 'rake/testtask'
+
+Rake::TestTask.new do |t|
+ t.libs << 'test'
+end
+
+desc "Run tests"
+task :default => :test
4 bin/xkcd
@@ -0,0 +1,4 @@
+#!/usr/bin/env ruby
+require 'xkcd'
+puts XKCD.get
+
32 lib/xkcd.rb
@@ -0,0 +1,32 @@
+#!/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
8 test/test_karam.rb
@@ -0,0 +1,8 @@
+require 'test/unit'
+require 'xkcd'
+
+class XKCDTest < Test::Unit::TestCase
+ def test_XKCD_get
+ assert_not_nil XKCD.get
+ end
+end
27 xkcd.gemspec
@@ -0,0 +1,27 @@
+Gem::Specification.new do |s|
+ s.name = "xkcd"
+ s.version = "0.0.3"
+ s.default_executable = "xkcd"
+
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
+ s.authors = ["Hemanth.HM"]
+ s.date = %q{2012-05-20}
+ s.description = %q{A simple gem to get random img/comic url from xkcd}
+ s.email = %q{hemanth.hm@gmail.com}
+ s.files = ["Rakefile", "lib/xkcd.rb", "bin/xkcd"]
+ s.test_files = Dir.glob('test/*.rb')
+ s.homepage = %q{http://rubygems.org/gems/xkcd}
+ s.require_paths = ["lib"]
+ s.rubygems_version = %q{1.6.2}
+ s.summary = %q{XCKD random img urls!}
+
+ if s.respond_to? :specification_version then
+ s.specification_version = 3
+
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
+ else
+ end
+ else
+ end
+end
+
Please sign in to comment.
Something went wrong with that request. Please try again.