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

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