Class: Marvel101::CLI
- Inherits:
-
Object
- Object
- Marvel101::CLI
- Defined in:
- lib/marvel_101/cli.rb
Constant Summary collapse
- SOURCE =
"http://marvel.com/characters/"
- STARTING_PAGES =
[ ["Popular Teams", "list/997/titanic_teams"], ["Popular Heroes", "list/994/top_marvel_heroes"], ["Popular Villains", "list/995/bring_on_the_bad_guys"], ["Featured Characters", "browse"], ["The Women of Marvel", "list/996/women_of_marvel"] ]
- LINE_LEN =
80
Instance Method Summary collapse
- #call ⇒ Object
- #display_main ⇒ Object
- #display_topic(topic) ⇒ Object
- #error(topic) ⇒ Object
- #exit_message ⇒ Object
- #main_menu ⇒ Object
- #open_link(url, topic) ⇒ Object
- #options_message(topic) ⇒ Object
- #topic_menu(topic) ⇒ Object
Instance Method Details
#call ⇒ Object
15 16 17 18 |
# File 'lib/marvel_101/cli.rb', line 15 def call puts "\nWelcome to Marvel 101!" end |
#display_main ⇒ Object
54 55 56 57 58 59 60 61 |
# File 'lib/marvel_101/cli.rb', line 54 def display_main puts "\n" + "-" * LINE_LEN puts "Here are your primary options:" STARTING_PAGES.each.with_index(1) {|page, idx| puts "#{idx}. #{page[0]}!"} puts "-" * LINE_LEN puts "You can also enter (E)xit to... exit" puts "Select a number from the options above and we'll get started!" end |
#display_topic(topic) ⇒ Object
63 64 65 66 67 68 69 |
# File 'lib/marvel_101/cli.rb', line 63 def display_topic(topic) break_len = (LINE_LEN - topic.name.size) / 2.0 puts "\n" + "-" * break_len.floor + "#{topic.name}" + "-" * break_len.ceil topic.display puts "-" * LINE_LEN (topic) end |
#error(topic) ⇒ Object
87 88 89 90 |
# File 'lib/marvel_101/cli.rb', line 87 def error(topic) puts "\nSorry, that wasn't a valid option. Let's try again." topic == "main" ? : (topic) end |
#exit_message ⇒ Object
76 77 78 |
# File 'lib/marvel_101/cli.rb', line 76 def puts "\nOh ok, well have a super day!" end |
#main_menu ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/marvel_101/cli.rb', line 20 def display_main print ">> " input = gets.chomp.downcase if input.to_i.between?(1, STARTING_PAGES.size) name, url = STARTING_PAGES[input.to_i - 1] (Marvel101::List.find_or_create_by_name(name, SOURCE + url)) elsif input == "exit" || input == "e" else error("main") end end |
#open_link(url, topic) ⇒ Object
71 72 73 74 |
# File 'lib/marvel_101/cli.rb', line 71 def open_link(url, topic) Launchy.open(topic.urls[url]) if topic.urls.include?(url) (topic) end |
#options_message(topic) ⇒ Object
80 81 82 83 84 85 |
# File 'lib/marvel_101/cli.rb', line 80 def (topic) puts "Enter an option number for more info!" if topic.takes_input? puts "You can enter (M)ain to go back to the main menu or (E)xit to... exit" puts "Type (L)ist to return to #{topic.list.name} menu" if !topic.list? puts "Type (T)eam to return to #{topic.team.name} menu" if topic.has_team? end |
#topic_menu(topic) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/marvel_101/cli.rb', line 35 def (topic) topic.get_info unless topic.scraped display_topic(topic) print ">> " input = gets.chomp.downcase case input when "101","wiki" then open_link("url_#{input}".to_sym, topic) when "source" then open_link(:url, topic) when "e","exit" then when "m","main" then when "l","list" then topic.list? ? error(topic) : (topic.list) when "t","team" then topic.has_team? ? (topic.team) : error(topic) else output = topic.valid_input?(input.to_i) output ? (output) : error(topic) end end |