class Redditsherpa::CLI
Public Instance Methods
comments(topicnumber)
click to toggle source
# File lib/redditsherpa/cli.rb, line 46 def comments(topicnumber) comments_url = @array[topicnumber.to_i - 1] +".json" response2 = Faraday.get comments_url json_response2 = JSON.parse(response2.body) thread = json_response2[0]["data"]["children"][0] puts puts "***************************COMMENTS***************************" puts puts "______________________________________________________________" puts Rainbow("Title: #{thread["data"]["title"]}").magenta.bright puts "Author: #{thread["data"]["author"]}" puts "Number of Comments: #{thread["data"]["num_comments"]}" puts "______________________________________________________________" top_level_comments = json_response2[1]["data"]["children"] top_level_comments.each do |comment| print_subcomments_recursively(comment) end puts ("Read another subreddit! Type 'read SUBREDDIT' with a specific subreddit or 'surprise me' for a random subreddit.").color(:cyan) end
open_up(topicnumber)
click to toggle source
# File lib/redditsherpa/cli.rb, line 68 def open_up(topicnumber) target_url = @array[topicnumber.to_i - 1] puts "Opening #{target_url}..." Launchy.open(target_url) end
read(subreddit)
click to toggle source
# File lib/redditsherpa/cli.rb, line 24 def read(subreddit) puts "reading subreddit..." response = client.get_posts(subreddit) posts = response[:data][:children] i = 1 @array = [] posts.take(25).each do |post| post = post[:data] post_id = post[:id] @array << "http://www.reddit.com/r/#{subreddit}/comments/#{post_id}/" puts ("#{i}. " + post[:title]).bright puts "#{post[:url]}" puts "____________________________________________________________" puts ("Upvotes: #{post[:ups]}").color(:green) + "|" + Rainbow("Downvotes: #{post[:downs]}").color(:red) + "| Number of Comments #{post[:num_comments]}" puts i += 1 end puts ("To look at comments for a specific topic in the console, type 'comments TOPICNUMBER'. Ex. 'comments 25'").color(:cyan) puts ("To open a link in the browser, type 'open TOPICNUMBER'. Ex. 'open 12'").color(:cyan) puts ("Done browsing? Type 'exit' or 'x' to quit redditsherpa").color(:cyan) end
search(topic)
click to toggle source
# File lib/redditsherpa/cli.rb, line 9 def search(topic) response = client.search(topic) result = response[:data][:children] puts "***********************************" puts ("Search Results:").color(:magenta).underline.bright result.each_with_index do |hash, i| unless i == 0 puts Rainbow(hash[:data][:title]).bright puts (hash[:data][:description]) puts "_____________________________________________" end end puts ("Search for something else or read a subreddit! Type 'read SUBREDDIT' with a specific subreddit or 'surprise me' for a random subreddit. Or type help for more.").color(:cyan) end
Private Instance Methods
client()
click to toggle source
# File lib/redditsherpa/cli.rb, line 76 def client @client ||= Redditsherpa::Client.new end
print_subcomments_recursively(comment, depth=0)
click to toggle source
# File lib/redditsherpa/cli.rb, line 80 def print_subcomments_recursively(comment, depth=0) puts "\t"*depth + Rainbow("Level #{depth}: ").bright + "#{comment["data"]["body"]} -- by #{comment["data"]["author"]}\n\n" if comment["data"]["replies"] == "" or !comment["data"].has_key?("replies") puts "-------------------------END THREAD-------------------------\n" else sub_comments = comment["data"]["replies"]["data"]["children"] sub_comments.each do |subcomment| print_subcomments_recursively(subcomment, depth + 1) end end end