class Object
Constants
- BASE_API_URL
Public Instance Methods
abort_library_mode()
click to toggle source
# File exec_helpers/command_line_parse.rb, line 3 def abort_library_mode puts "Library file not found, cannot display." exit end
get_cli_options()
click to toggle source
# File exec_helpers/command_line_parse.rb, line 15 def get_cli_options options = {} OptionParser.new do |opts| opts.banner = "Usage: glibrary [options...] [query]" opts.on("-t", "--title=TITLE", "Specify a title keyword") do |t| options[:title] = t end opts.on("-a", "--author=AUTHOR", "Specify an author keyword") do |a| options[:author] = a end opts.on("-p", "--publisher=PUBLISHER", "Specify a publisher keyword") do |p| options[:publisher] = p end opts.on("-f", "--lib-file=LIBFILE", "Select a library save file. Otherwise, a default save file will be used.") do |libfile| abort_library_mode if ARGV.include?("-l") && !File.exist?(libfile) options[:filename] = libfile end opts.on("-l", "--library", "See your reading list; ignores all search options") do return { filename: options[:filename], library_mode: true } end opts.on("-h", "--help", "Prints this help") do print_help_message(opts) exit end end.parse! options[:search] = ARGV.join('+') options end
handle_successful_prompt_completion(selected_book)
click to toggle source
# File exec_helpers/user_prompt.rb, line 12 def handle_successful_prompt_completion(selected_book) puts "" puts "The book \"#{selected_book['title']}\" has been added to your reading list." end
library_mode_user_prompt(lib)
click to toggle source
# File exec_helpers/user_prompt.rb, line 26 def library_mode_user_prompt(lib) while true begin lib.pretty_print i = prompt( "Select a book number to delete, or type \"q\" to quit: ", /\A([0-9]+|[qQ])\Z/, lib) lib.delete(i) lib.save puts "Your library now looks like this." puts "" rescue SelectionError puts "\nSorry, your selection was invalid. Please try again." rescue NotABook puts "\nSorry, your selection was invalid. Make sure your selection is in the provided range." end end end
print_help_message(opts)
click to toggle source
# File exec_helpers/command_line_parse.rb, line 8 def print_help_message(opts) puts "" puts opts puts "[query]: all other arguments will be treated as general search keywords" puts "" end
process_args!(args = ARGV)
click to toggle source
# File exec_helpers/exec_helper.rb, line 1 def process_args!(args = ARGV) args << '-h' if args.empty? args << args.delete("-l") if args.include?("-l") end
process_selection(selection)
click to toggle source
# File exec_helpers/user_prompt.rb, line 8 def process_selection(selection) return selection.to_i - 1 end
prompt(prompt, regexp, library, suppress = nil)
click to toggle source
# File exec_helpers/user_prompt.rb, line 17 def prompt(prompt, regexp, library, suppress = nil) print prompt s = suppress || STDIN.gets.strip verify_selection(s, regexp) s = process_selection(s) raise NotABook unless s <= library.size return s end
search_mode_user_prompt(temp_booklist, persistent_library)
click to toggle source
# File exec_helpers/user_prompt.rb, line 48 def search_mode_user_prompt(temp_booklist, persistent_library) temp_booklist.pretty_print while true begin i = prompt( "Enter a number (1-5) to add a book to your reading list (or q to quit): ", /\A[1-5]|[qQ]\Z/, temp_booklist ) selected_book = temp_booklist[i] persistent_library << selected_book persistent_library.save handle_successful_prompt_completion(selected_book) puts "Would you like to add another?" puts "" rescue BookDuplicateError puts "\nThat book is already in your reading list. Would you like to add another?" puts "" rescue SelectionError puts "\nSorry, your selection was invalid. Please try again." puts "" rescue NotABook puts "\nSorry, your selection was invalid. Make sure your selection is within the range of available options." puts "" end end end
verify_selection(selection, regexp)
click to toggle source
# File exec_helpers/user_prompt.rb, line 3 def verify_selection(selection, regexp) raise SelectionError unless selection =~ regexp raise UserQuits if selection =~ /\A[Qq]\Z/ end
with_library_mode_error_handling() { || ... }
click to toggle source
# File exec_helpers/error_handler.rb, line 3 def with_library_mode_error_handling begin yield rescue UserQuits puts "\nEnjoy your day." rescue Errno::EACCES puts "\nYou don't have permission to write to the file you specified. Quitting immediately" puts "" end exit end
with_search_mode_error_handling() { || ... }
click to toggle source
# File exec_helpers/error_handler.rb, line 15 def with_search_mode_error_handling begin yield rescue UserQuits puts "\nThanks for browsing. Library file saved" puts "" rescue Errno::EACCES puts "\nYou don't have permission to write to the file you specified. Quitting immediately" puts "" rescue NoInternetError puts "\nNo internet connection. Please connect to the internet and try again." puts "" rescue SearchError puts "\nThere was an error with your query; be careful to format it well" puts "" rescue NoResults puts "\nNo results." puts "" end exit end