Class: Distraction
- Inherits:
-
Object
- Object
- Distraction
- Defined in:
- lib/distraction.rb
Class Method Summary collapse
-
.hn ⇒ Object
display the top ten links on HN homepage.
-
.reddit_fact(open) ⇒ Object
display the top link from TIL.
-
.reddit_top(subreddit) ⇒ Object
display the top ten links on Reddit frontpage.
-
.reddit_vid ⇒ Object
picks a random video off r/videos.
Class Method Details
.hn ⇒ Object
display the top ten links on HN homepage
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/distraction.rb', line 91 def self.hn() begin ping = Net::HTTP.get(URI('http://api.ihackernews.com/page/')) listing = JSON.parse(ping) listing = listing['items'] i = 1 while (i < 11) line = " " + i.to_s.rjust(2, " ") + ". " + listing[i]['title'] i += 1 puts line end puts "\nWhich article would you like to open?" story = STDIN.gets.chomp() story = story.to_i if ((story < 11) && (story > 0)) result_url = listing[story]['url'] if result_url.start_with?("/comments/") result_url = "http://news.ycombinator.com/item?id=#{listing[story]['id']}" end puts "\nOpening link (#{result_url}) in 5 seconds... CTRL + C to quit." sleep(5) `open #{result_url}` else puts "Come on, son." end rescue puts "The HN API we're using isn't responding right now. Let's try again later." end end |
.reddit_fact(open) ⇒ Object
display the top link from TIL
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/distraction.rb', line 6 def self.reddit_fact(open) begin ping = Net::HTTP.get(URI('http://www.reddit.com/r/todayilearned.json?limit=5')) listing = JSON.parse(ping) listing = listing['data']['children'] # check if NSFW result = listing.each do |l| break l['data'] if (!l['data']['over_18']) end result_title = result['title'].gsub('TIL', 'Did you know...') result_domain = result['domain'] result_url = result['url'] case open when true puts result_title + "\n\nOpening link from (#{result_domain}) in 10 seconds... CTRL + C to quit." sleep(10) `open #{result_url}` when false puts result_title + "\n\nYou can type 'distraction fact open' to open the link in a browser." end rescue puts "Reddit isn't responding right now. Let's try again later." end end |
.reddit_top(subreddit) ⇒ Object
display the top ten links on Reddit frontpage
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/distraction.rb', line 35 def self.reddit_top(subreddit) begin ping = Net::HTTP.get(URI('http://www.reddit.com/r/' + subreddit + '.json?limit=10')) listing = JSON.parse(ping) listing = listing['data']['children'] listing.each_with_index do |l, i| line = " " + (i+1).to_s.rjust(2, " ") + ". " + l['data']['title'] + " (" + l['data']['domain'] + ")" if l['data']['over_18'] line = line + " (NSFW)" end puts line end puts "\nWhich link would you like to open?" story = STDIN.gets.chomp() story = story.to_i if ((story < 11) && (story > 0)) result = listing[story-1]['data'] result_domain = result['domain'] result_url = result['url'] puts "\nOpening link from (#{result_domain}) in 5 seconds... CTRL + C to quit." sleep(5) `open #{result_url}` else puts "Come on, son." end rescue puts "Oops, either Reddit isn't responding or that subreddit doesn't exist!" end end |
.reddit_vid ⇒ Object
picks a random video off r/videos
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/distraction.rb', line 68 def self.reddit_vid() begin ping = Net::HTTP.get(URI('http://www.reddit.com/r/videos.json')) # gets 25 top vids listing = JSON.parse(ping) listing = listing['data']['children'] nsfw_flag = true # true means it is nsfw randomVideo = 0 while nsfw_flag randomVideo = rand(24) # random between 0 and 24 nsfw_flag = listing[randomVideo]['data']['over_18'] end result = listing[randomVideo]['data'] result_domain = result['domain'] result_url = result['url'] puts "Opening video from (#{result_domain}) in 5 seconds... CTRL + C to quit." sleep(5) `open #{result_url}` rescue puts "Oops, something went wrong! Let's try again later." end end |