Class: CorpusGenerator::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/random_poetry_scraper/cli.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#current_poems_alphabetizedObject

Returns the value of attribute current_poems_alphabetized.



2
3
4
# File 'lib/random_poetry_scraper/cli.rb', line 2

def current_poems_alphabetized
  @current_poems_alphabetized
end

#current_poets_alphabetizedObject

Returns the value of attribute current_poets_alphabetized.



2
3
4
# File 'lib/random_poetry_scraper/cli.rb', line 2

def current_poets_alphabetized
  @current_poets_alphabetized
end

Instance Method Details

#accept_command_line_optionsObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# 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) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 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) ⇒ Object



234
235
236
237
238
239
240
241
242
243
244
245
246
247
# 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) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# 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) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# 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

#goodbyeObject



217
218
219
220
# File 'lib/random_poetry_scraper/cli.rb', line 217

def goodbye
		puts "Thanks for using Pleasure reader!"
		exit
end

#handle_json_and_pleasure_messageObject



50
51
52
53
# 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_messageObject



55
56
57
58
# 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_messageObject



42
43
44
45
46
47
48
# 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) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
# 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_poemsObject

> Pleasure Reading Interface Poem Selection Menu Helpers



227
228
229
230
231
232
# 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_alphabeticallyObject



272
273
274
275
276
277
278
# 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_headerObject



204
205
206
207
208
# 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_menuObject

> The pleasure reading interface



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# 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_instructionsObject

> Pleasure Reading Interface General Helpers



194
195
196
197
198
199
200
201
202
# 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_instructionsObject



210
211
212
213
214
215
# 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) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# 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_instructionsObject

> Pleasure Reading Interface Poet Selection Menu Helpers



264
265
266
267
268
269
270
# 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_menuObject



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# 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_readingObject



249
250
251
252
253
254
255
256
257
258
# 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_alphabeticallyObject



107
108
109
# 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) ⇒ Object



280
281
282
# 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