class CorpusGenerator::CLI
Attributes
current_poems_alphabetized[RW]
current_poets_alphabetized[RW]
Public Instance Methods
accept_command_line_options()
click to toggle source
# File lib/random_poetry_scraper/cli.rb, line 21 def accept_command_line_options opts = Trollop::options do version <<-EOS š Random Poetry Scraper Version 1.0 | July 2018 Hayden Betts | @hayden_betts EOS banner <<~EOS \nš Random Poetry Scraper is a command line gem which returns the text of poems scraped from poemhunter.com. \n Usage: \n EOS opt :num_poems, "Number of poems to return", :type => :integer opt :json, "Output poems and their attributes directly to json" opt :pleasure, "Scrape poems and then enter a CLI for pleasure reading" end opts.select { |k, v| opts[k] } end
call(commandline_options = nil)
click to toggle source
# File lib/random_poetry_scraper/cli.rb, line 4 def call(commandline_options = nil) commandline_options = accept_command_line_options if !commandline_options if commandline_options == {} handle_no_options_passed_message else if commandline_options[:num_poems] == nil handle_no_num_poems_message elsif commandline_options[:json] && commandline_options[:pleasure] handle_json_and_pleasure_message else handle_valid_command_line_options(commandline_options) end end end
display_poem(poem)
click to toggle source
# File lib/random_poetry_scraper/cli.rb, line 234 def display_poem(poem) title_string = "#{poem.name} - #{poem.poet.name}" puts "" puts Array.new(title_string.length, "*").join('') puts title_string puts Array.new(title_string.length, "*").join('') puts "" puts "\n#{poem.text}" puts "" puts Array.new(title_string.length, "*").join('') end
get_poems_with_status_updates(num_poems)
click to toggle source
# File lib/random_poetry_scraper/cli.rb, line 74 def get_poems_with_status_updates(num_poems) num_poems.times do |i| poem_attributes = CorpusGenerator::Scraper.new.scrape_poem_page # TODO possibly factor out? if poem = CorpusGenerator::Poem.new(poem_attributes) puts "#{i + 1} poem(s) fetched succesfully." else puts "Failed. Trying again." i -= 1 end end set_current_poems_alphabetically end
get_poems_without_status_updates(num_poems)
click to toggle source
# File lib/random_poetry_scraper/cli.rb, line 91 def get_poems_without_status_updates(num_poems) num_poems.times do |i| poem_attributes = CorpusGenerator::Scraper.new.scrape_poem_page # TODO possibly factor out? if poem = CorpusGenerator::Poem.new(poem_attributes) next else i -= 1 end end set_current_poems_alphabetically end
goodbye()
click to toggle source
# File lib/random_poetry_scraper/cli.rb, line 217 def goodbye puts "Thanks for using Pleasure reader!" exit end
handle_json_and_pleasure_message()
click to toggle source
# File lib/random_poetry_scraper/cli.rb, line 50 def handle_json_and_pleasure_message puts "Cannot run with both the --json and --pleasure flags selected" puts "Run with --help for help." end
handle_no_num_poems_message()
click to toggle source
# File lib/random_poetry_scraper/cli.rb, line 55 def handle_no_num_poems_message puts "Cannot run without a --num-poems selected" puts "Run with --help for help." end
handle_no_options_passed_message()
click to toggle source
# File lib/random_poetry_scraper/cli.rb, line 42 def handle_no_options_passed_message puts "" puts "š Random Poetry Scraper requires you to pass in options indicating" puts "the number of poems you would like to return, and their desired output format." puts "Run with --help for help." puts "" end
handle_valid_command_line_options(commandline_options)
click to toggle source
# File lib/random_poetry_scraper/cli.rb, line 60 def handle_valid_command_line_options(commandline_options) if commandline_options[:pleasure] get_poems_with_status_updates(commandline_options[:num_poems]) pleasure_reading_menu elsif commandline_options[:json] get_poems_without_status_updates(commandline_options[:num_poems]) json = CorpusGenerator::Poem.poems_to_json(self.current_poems_alphabetized) puts json return json end end
list_current_poems()
click to toggle source
list_poets_alphabetically()
click to toggle source
# File lib/random_poetry_scraper/cli.rb, line 272 def list_poets_alphabetically self.current_poets_alphabetized = CorpusGenerator::Poet.all.sort_by {|poet| poet.name} self.current_poets_alphabetized.each.with_index(1) do |poet, index| puts "#{index}. #{poet.name}" end puts "" end
pleasure_reading_header()
click to toggle source
# File lib/random_poetry_scraper/cli.rb, line 204 def pleasure_reading_header header_path = File.join( File.dirname(__FILE__), './pleasure_reading_header' ) File.read(header_path) end
poem_selection_instructions()
click to toggle source
# File lib/random_poetry_scraper/cli.rb, line 210 def poem_selection_instructions puts "\nType the number of a poem to read it." puts "Or type menu to go up one menu." puts "Or type exit to end the program." puts "" end
poet_selection_instructions()
click to toggle source
> Pleasure Reading Interface Poet Selection Menu Helpers¶ ↑
# File lib/random_poetry_scraper/cli.rb, line 264 def poet_selection_instructions puts "" puts "Type the number of a poet whose poems you would like to read." puts "Or type menu to go up one menu." puts "Or type exit to end the program." puts "" end
quit_or_continue_reading()
click to toggle source
# File lib/random_poetry_scraper/cli.rb, line 249 def quit_or_continue_reading puts "" puts "To exit now, type exit" puts "To return to the list of poems, press enter" puts "" input = nil input = gets.strip goodbye if input == 'exit' end
set_current_poems_alphabetically()
click to toggle source
# File lib/random_poetry_scraper/cli.rb, line 107 def set_current_poems_alphabetically self.current_poems_alphabetized = CorpusGenerator::Poem.all.sort_by {|poem| poem.name} end
set_poems_alphabetically_by_poet(selected_poet)
click to toggle source
# File lib/random_poetry_scraper/cli.rb, line 280 def set_poems_alphabetically_by_poet(selected_poet) self.current_poems_alphabetized = selected_poet.poems.sort_by {|poem| poem.name} end