xkcd 1.0.1 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +4 -0
- data/lib/xkcd.rb +26 -4
- metadata +48 -49
data/Rakefile
CHANGED
data/lib/xkcd.rb
CHANGED
@@ -1,7 +1,9 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
2
|
require 'nokogiri'
|
3
3
|
require 'open-uri'
|
4
|
-
require 'json'
|
4
|
+
require 'json'
|
5
|
+
require 'google-search'
|
6
|
+
require 'uri'
|
5
7
|
|
6
8
|
# The main XKCD driver
|
7
9
|
class XKCD
|
@@ -18,6 +20,22 @@ class XKCD
|
|
18
20
|
open('http://dynamic.xkcd.com/random/comic/').base_uri.to_s
|
19
21
|
end
|
20
22
|
|
23
|
+
def self.search(query)
|
24
|
+
comic_id = 149 #Default to make me a sandwich if nothing can be found
|
25
|
+
Google::Search::Web.new(:query => "xkcd " + query).each do |result|
|
26
|
+
current_url = URI(result.uri) #Parse the search result URL
|
27
|
+
if current_url.hostname == "xkcd.com" #Ignore all of URLs which are not from XKCD.com
|
28
|
+
id = current_url.path.gsub(/\//, "").to_i #Checking to make sure that only a URL with a comic strip ID is taken
|
29
|
+
if id != 0
|
30
|
+
comic_id = id
|
31
|
+
break
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
self.get_comic(comic_id)
|
37
|
+
end
|
38
|
+
|
21
39
|
class << XKCD
|
22
40
|
alias_method :get, :comic
|
23
41
|
end
|
@@ -30,11 +48,15 @@ class XKCD
|
|
30
48
|
"#{img_title} : #{img_url}"
|
31
49
|
end
|
32
50
|
=end
|
33
|
-
def self.img
|
51
|
+
def self.img
|
34
52
|
max = JSON.parse(open('http://xkcd.com/info.0.json').read)["num"]
|
35
53
|
comic_num = 1 + rand(max-1)
|
36
54
|
comic_num = 1 if comic_num == 404 # Avoid 404th comic ;)
|
37
|
-
|
38
|
-
|
55
|
+
self.get_comic(comic_num)
|
56
|
+
end
|
57
|
+
|
58
|
+
def self.get_comic(id)
|
59
|
+
comic = JSON.parse(open("http://xkcd.com/#{id}/info.0.json").read)
|
60
|
+
"#{comic['alt']} : #{comic['img']}"
|
39
61
|
end
|
40
62
|
end
|
metadata
CHANGED
@@ -1,83 +1,82 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: xkcd
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.1.0
|
5
5
|
prerelease:
|
6
|
-
segments:
|
7
|
-
- 1
|
8
|
-
- 0
|
9
|
-
- 1
|
10
|
-
version: 1.0.1
|
11
6
|
platform: ruby
|
12
|
-
authors:
|
7
|
+
authors:
|
13
8
|
- Hemanth.HM
|
14
9
|
autorequire:
|
15
10
|
bindir: bin
|
16
11
|
cert_chain: []
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
- !ruby/object:Gem::Dependency
|
12
|
+
date: 2012-05-20 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
21
15
|
name: nokogiri
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 1.5.0
|
22
|
+
type: :runtime
|
22
23
|
prerelease: false
|
23
|
-
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
25
|
none: false
|
25
|
-
requirements:
|
26
|
-
- -
|
27
|
-
- !ruby/object:Gem::Version
|
28
|
-
hash: 3
|
29
|
-
segments:
|
30
|
-
- 1
|
31
|
-
- 5
|
32
|
-
- 0
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
33
29
|
version: 1.5.0
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: google-search
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
34
38
|
type: :runtime
|
35
|
-
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
36
46
|
description: A simple gem to get random img/comic url from xkcd
|
37
47
|
email: hemanth.hm@gmail.com
|
38
|
-
executables:
|
48
|
+
executables:
|
39
49
|
- xkcd
|
40
50
|
extensions: []
|
41
|
-
|
42
51
|
extra_rdoc_files: []
|
43
|
-
|
44
|
-
files:
|
52
|
+
files:
|
45
53
|
- Rakefile
|
46
54
|
- lib/xkcd.rb
|
47
55
|
- bin/xkcd
|
48
56
|
- test/test_karam.rb
|
49
57
|
homepage: http://rubygems.org/gems/xkcd
|
50
58
|
licenses: []
|
51
|
-
|
52
59
|
post_install_message:
|
53
60
|
rdoc_options: []
|
54
|
-
|
55
|
-
require_paths:
|
61
|
+
require_paths:
|
56
62
|
- lib
|
57
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
63
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
58
64
|
none: false
|
59
|
-
requirements:
|
60
|
-
- -
|
61
|
-
- !ruby/object:Gem::Version
|
62
|
-
|
63
|
-
|
64
|
-
- 0
|
65
|
-
version: "0"
|
66
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ! '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
67
70
|
none: false
|
68
|
-
requirements:
|
69
|
-
- -
|
70
|
-
- !ruby/object:Gem::Version
|
71
|
-
|
72
|
-
segments:
|
73
|
-
- 0
|
74
|
-
version: "0"
|
71
|
+
requirements:
|
72
|
+
- - ! '>='
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
75
|
requirements: []
|
76
|
-
|
77
76
|
rubyforge_project:
|
78
|
-
rubygems_version: 1.8.
|
77
|
+
rubygems_version: 1.8.23
|
79
78
|
signing_key:
|
80
79
|
specification_version: 3
|
81
80
|
summary: XCKD random img urls!
|
82
|
-
test_files:
|
81
|
+
test_files:
|
83
82
|
- test/test_karam.rb
|