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

> Pleasure Reading Interface Poem Selection Menu Helpers

# File lib/random_poetry_scraper/cli.rb, line 227
def list_current_poems
    self.current_poems_alphabetized.each.with_index(1) do |poem, index|
        puts "#{index}. #{poem.name} - #{poem.poet.name}"
    end
    puts ""
end
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
pleasure_reading_menu() click to toggle source

> The pleasure reading interface

# File lib/random_poetry_scraper/cli.rb, line 119
def pleasure_reading_menu
    input = nil

    until input == 'exit'

        pleasure_reading_menu_instructions
        input = gets.strip
        
        case input
        when 'poems'
            poem_selection_menu  
        when 'poets'
            poet_selection_menu
        when 'exit'
            goodbye
        else
            puts "Invalid input! Please select a valid option!"
        end
    end
end
pleasure_reading_menu_instructions() click to toggle source

> Pleasure Reading Interface General Helpers

# File lib/random_poetry_scraper/cli.rb, line 194
def pleasure_reading_menu_instructions
    puts pleasure_reading_header

    puts "How would you like to find poems to read?"
    puts "To see a list of poems, type 'poems', and press enter."
    puts "To see a list of poets, type 'poets', and press enter."
    puts "To end the program, type 'exit', and press enter."
    puts ""
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
poem_selection_menu(poet = nil) click to toggle source
# File lib/random_poetry_scraper/cli.rb, line 140
def poem_selection_menu(poet = nil)
    input = nil

    while input != 'menu'

        poem_selection_instructions
        list_current_poems

        input = gets.strip
        input_valid = input.to_i > 0 && input.to_i <= self.current_poems_alphabetized.size
 
        if input_valid
            selected_poem = self.current_poems_alphabetized[input.to_i - 1]
            display_poem(selected_poem)
            quit_or_continue_reading
            next
        elsif input == 'exit'
            goodbye
        elsif input != 'menu'
            puts "Please enter a valid poem selection!"
        end
    end
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
poet_selection_menu() click to toggle source
# File lib/random_poetry_scraper/cli.rb, line 164
def poet_selection_menu
    input = nil

    while input != 'menu'

        poet_selection_instructions
        list_poets_alphabetically

        input = gets.strip

        valid_index = input.to_i > 0 && input.to_i <= self.current_poets_alphabetized.size

        if valid_index
            selected_poet = self.current_poets_alphabetized[input.to_i - 1]
            set_poems_alphabetically_by_poet(selected_poet)
            poem_selection_menu
        elsif input == 'exit'
            goodbye
        elsif input != 'menu'
            puts "Please enter a valid poet selection!"
        end

        puts "To return to the poet selection menu, enter 'menu'"
    end
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