class Reddit::CLI
Public Instance Methods
call()
click to toggle source
# File lib/reddit/cli.rb, line 2 def call @subreddit = ARGV[0] || "ruby" Reddit::Scraper.scrape(@subreddit) @posts = Reddit::Post.all puts "Here's the current hot list on r/#{@subreddit}" show_posts menu end
get_score_text(index)
click to toggle source
# File lib/reddit/cli.rb, line 40 def get_score_text(index) post = @posts[index.to_i] upvotes = post.upvotes if post.upvotes.to_i == 1 score = post.upvotes.to_i >= 0 ? 'upvote' : 'downvote' else score = post.upvotes.to_i >= 0 ? 'upvotes' : 'downvotes' end return "#{upvotes} #{score}" end
openInBrowser(url)
click to toggle source
# File lib/reddit/cli.rb, line 12 def openInBrowser(url) if RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/ system "start #{url}" elsif RbConfig::CONFIG['host_os'] =~ /darwin/ system "open #{url}" elsif RbConfig::CONFIG['host_os'] =~ /linux|bsd/ system "xdg-open #{url}" end end
show_post(index)
click to toggle source
# File lib/reddit/cli.rb, line 59 def show_post(index) post = @posts[index.to_i] puts "" puts "Title: #{post.title}" puts "Author: #{post.author}" puts "Score: #{post.upvotes}" puts "Comments: #{post.comments}" puts "Posted: #{post.timestamp}" puts "" input = nil while input != "exit" puts "> Enter the option you'd like to perform:" puts "1. Open in browser" puts "2. Show hot posts" puts "3. Exit" puts "" input = STDIN.gets.chomp.downcase if input.to_i == 1 openInBrowser(post.url) elsif input.to_i == 2 show_posts break elsif input.to_i == 3 || input == "exit" || input == "q" exit! else puts "> Not sure what you want, type list or exit." end end end
show_posts()
click to toggle source
# File lib/reddit/cli.rb, line 51 def show_posts @posts.each_with_index do |post, i| upvotes = get_score_text(i) index = "#{i + 1}" puts "#{index.bold}. #{upvotes} - #{post.title} by #{post.author.bold}" end end