class Redcli::Application
Constants
- COLORS
- ROOT
Public Class Methods
new(subreddit:, stdout:STDOUT, stdin: STDIN)
click to toggle source
# File lib/redcli/application.rb, line 11 def initialize(subreddit:, stdout:STDOUT, stdin: STDIN) @subreddit = subreddit @stdout = stdout @stdin = stdin end
Public Instance Methods
link(link_index)
click to toggle source
# File lib/redcli/application.rb, line 32 def link(link_index) @current_link = @links[link_index] display_topic(link_index) prompt_link_input act_on_input end
links()
click to toggle source
# File lib/redcli/application.rb, line 26 def links display_links prompt_links_input act_on_input end
open_current_link()
click to toggle source
# File lib/redcli/application.rb, line 39 def open_current_link url = @current_link["data"]["url"] `open #{url}` end
run()
click to toggle source
# File lib/redcli/application.rb, line 17 def run @links = get_links if @links links else signal_failure end end
Private Instance Methods
act_on_input()
click to toggle source
# File lib/redcli/application.rb, line 85 def act_on_input input = @stdin.gets.chomp if input =~ /q/ return elsif input =~ /[0-9]/ link(input.to_i - 1) elsif input =~ /b/ links elsif input =~ /o/ open_current_link end end
display_links()
click to toggle source
# File lib/redcli/application.rb, line 62 def display_links @stdout.puts "\n" @links.each_with_index do |link, i| title = link.fetch("data", { "title" => "No Title"}).fetch("title") @stdout.puts "#{i+1}) #{title}".send(COLORS[i%2]) end @stdout.puts "\n" end
display_topic(topic_index)
click to toggle source
# File lib/redcli/application.rb, line 71 def display_topic(topic_index) @stdout.puts "\n\n\n" @stdout.puts @links[topic_index].fetch("data", { "selftext" => "No Information Available" }).fetch("selftext") @stdout.puts "\n" end
get_links()
click to toggle source
# File lib/redcli/application.rb, line 50 def get_links response = Faraday.get(url) body = JSON.parse(response.body) if body body["data"]["children"].first(10) end end
prompt_link_input()
click to toggle source
# File lib/redcli/application.rb, line 81 def prompt_link_input @stdout.puts "(b)ack | (q)uit | (o)pen in browser" end
prompt_links_input()
click to toggle source
# File lib/redcli/application.rb, line 77 def prompt_links_input @stdout.puts "(1-10) | (q)uit" end
signal_failure()
click to toggle source
# File lib/redcli/application.rb, line 58 def signal_failure @stdout.puts "Sorry, reddit is either not responding or the subreddit does not exist, try again" end
url()
click to toggle source
# File lib/redcli/application.rb, line 46 def url ROOT + "/r/" + @subreddit + ".json" end