xkcd 1.1.0 → 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/lib/xkcd.rb +48 -54
- metadata +10 -16
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: fa947e7bf08d1f3f0599731f01a15f6cfc208dfe
|
4
|
+
data.tar.gz: f3642a40560a81b75d982921645c877ff89c910a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3d530ae60f796b3743c6fbbe05eaaf3456da4d6125b72d7c643f8c6283386b4a99c163cbd3a66349a45c8f25ef1585cfb3c6c9478104c4eabfb521b029c63460
|
7
|
+
data.tar.gz: 140a6ea25180ddd19e4adc31b962b5a7c44fd1b507fa3b03993f7827c22f5ec8f07b4da5c7a63204673199b25635d0ee37b2d2cd5496fdcb7ad9302cf6d7d8d1
|
data/lib/xkcd.rb
CHANGED
@@ -1,62 +1,56 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
-
|
3
|
-
|
4
|
-
require
|
5
|
-
require
|
6
|
-
require
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require "nokogiri"
|
5
|
+
require "open-uri"
|
6
|
+
require "json"
|
7
|
+
require "google-search"
|
8
|
+
require "uri"
|
7
9
|
|
8
10
|
# The main XKCD driver
|
9
11
|
class XKCD
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
end
|
12
|
+
# Get img/comic URL from xkcd
|
13
|
+
#
|
14
|
+
# Example:
|
15
|
+
# >> XKCD.img
|
16
|
+
# => "https://imgs.xkcd.com/comics/hell.png"
|
17
|
+
#
|
18
|
+
# >> XKCD.comic
|
19
|
+
# => "https://xkcd.com/891/"
|
20
|
+
|
21
|
+
def self.comic
|
22
|
+
open("https://dynamic.xkcd.com/random/comic/").base_uri.to_s
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.search(query)
|
26
|
+
comic_id = 149 # Default to make me a sandwich if nothing can be found
|
27
|
+
Google::Search::Web.new(query: "xkcd " + query).each do |result|
|
28
|
+
current_url = URI(result.uri) # Parse the search result URL
|
29
|
+
next unless current_url.hostname == "xkcd.com" # Ignore all of URLs which are not from XKCD.com
|
30
|
+
|
31
|
+
id = current_url.path.gsub(/\//, "").to_i # Checking to make sure that only a URL with a comic strip ID is taken
|
32
|
+
if id != 0
|
33
|
+
comic_id = id
|
34
|
+
break
|
34
35
|
end
|
35
|
-
|
36
|
-
self.get_comic(comic_id)
|
37
|
-
end
|
38
|
-
|
39
|
-
class << XKCD
|
40
|
-
alias_method :get, :comic
|
41
36
|
end
|
42
37
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
end
|
38
|
+
get_comic(comic_id)
|
39
|
+
end
|
40
|
+
|
41
|
+
class << XKCD
|
42
|
+
alias get comic
|
43
|
+
end
|
44
|
+
|
45
|
+
def self.img
|
46
|
+
max = JSON.parse(open("https://xkcd.com/info.0.json").read)["num"]
|
47
|
+
comic_num = 1 + rand(max - 1)
|
48
|
+
comic_num = 1 if comic_num == 404 # Avoid 404th comic ;)
|
49
|
+
get_comic(comic_num)
|
50
|
+
end
|
51
|
+
|
52
|
+
def self.get_comic(id)
|
53
|
+
comic = JSON.parse(open("https://xkcd.com/#{id}/info.0.json").read)
|
54
|
+
"#{comic["alt"]} : #{comic["img"]}"
|
55
|
+
end
|
62
56
|
end
|
metadata
CHANGED
@@ -1,8 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: xkcd
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.1.
|
5
|
-
prerelease:
|
4
|
+
version: 1.1.1
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Hemanth.HM
|
@@ -14,33 +13,29 @@ dependencies:
|
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: nokogiri
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
16
|
requirements:
|
19
|
-
- -
|
17
|
+
- - ">="
|
20
18
|
- !ruby/object:Gem::Version
|
21
19
|
version: 1.5.0
|
22
20
|
type: :runtime
|
23
21
|
prerelease: false
|
24
22
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
23
|
requirements:
|
27
|
-
- -
|
24
|
+
- - ">="
|
28
25
|
- !ruby/object:Gem::Version
|
29
26
|
version: 1.5.0
|
30
27
|
- !ruby/object:Gem::Dependency
|
31
28
|
name: google-search
|
32
29
|
requirement: !ruby/object:Gem::Requirement
|
33
|
-
none: false
|
34
30
|
requirements:
|
35
|
-
- -
|
31
|
+
- - ">="
|
36
32
|
- !ruby/object:Gem::Version
|
37
33
|
version: '0'
|
38
34
|
type: :runtime
|
39
35
|
prerelease: false
|
40
36
|
version_requirements: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
37
|
requirements:
|
43
|
-
- -
|
38
|
+
- - ">="
|
44
39
|
- !ruby/object:Gem::Version
|
45
40
|
version: '0'
|
46
41
|
description: A simple gem to get random img/comic url from xkcd
|
@@ -51,30 +46,29 @@ extensions: []
|
|
51
46
|
extra_rdoc_files: []
|
52
47
|
files:
|
53
48
|
- Rakefile
|
54
|
-
- lib/xkcd.rb
|
55
49
|
- bin/xkcd
|
50
|
+
- lib/xkcd.rb
|
56
51
|
- test/test_karam.rb
|
57
52
|
homepage: http://rubygems.org/gems/xkcd
|
58
53
|
licenses: []
|
54
|
+
metadata: {}
|
59
55
|
post_install_message:
|
60
56
|
rdoc_options: []
|
61
57
|
require_paths:
|
62
58
|
- lib
|
63
59
|
required_ruby_version: !ruby/object:Gem::Requirement
|
64
|
-
none: false
|
65
60
|
requirements:
|
66
|
-
- -
|
61
|
+
- - ">="
|
67
62
|
- !ruby/object:Gem::Version
|
68
63
|
version: '0'
|
69
64
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
70
|
-
none: false
|
71
65
|
requirements:
|
72
|
-
- -
|
66
|
+
- - ">="
|
73
67
|
- !ruby/object:Gem::Version
|
74
68
|
version: '0'
|
75
69
|
requirements: []
|
76
70
|
rubyforge_project:
|
77
|
-
rubygems_version:
|
71
|
+
rubygems_version: 2.6.14
|
78
72
|
signing_key:
|
79
73
|
specification_version: 3
|
80
74
|
summary: XCKD random img urls!
|