Class: CommandLine

Inherits:
Object
  • Object
show all
Defined in:
lib/magic_tcg_finder/command_line_interface.rb

Instance Method Summary collapse

Instance Method Details

#access_listObject



53
54
55
56
57
58
59
60
61
62
# File 'lib/magic_tcg_finder/command_line_interface.rb', line 53

def access_list
	max = Card.all.size
	puts "Please make a selection"
	input = gets.strip.to_i
	if input < 0 || input > max
		puts "Invalid selection"
		card_menu
	end
	input
end

#call_scraper(input) ⇒ Object



32
33
34
35
# File 'lib/magic_tcg_finder/command_line_interface.rb', line 32

def call_scraper(input)
	new_search = Scraper.new(input)
	new_search.scrape_search_page
end

#card_menuObject



95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/magic_tcg_finder/command_line_interface.rb', line 95

def card_menu
	if Card.all.size == 1
		clear_screen
		load_card(1)
		interaction
	else
		list_cards
		input = access_list
		clear_screen
		load_card(input)
		interaction
	end

end

#clear_screenObject



4
5
6
# File 'lib/magic_tcg_finder/command_line_interface.rb', line 4

def clear_screen
	print %x{clear}
end

#colorize_cost(cost) ⇒ Object



64
65
66
67
68
69
70
71
72
73
# File 'lib/magic_tcg_finder/command_line_interface.rb', line 64

def colorize_cost(cost)
	return cost if cost.nil?
	cost = cost.gsub(/\{W\}/) { |match| match.colorize(:light_yellow) }
	cost = cost.gsub(/\{U\}/) { |match| match.colorize(:light_cyan) }
	cost = cost.gsub(/\{B\}/) { |match| match.colorize(:magenta) }
	cost = cost.gsub(/\{R\}/) { |match| match.colorize(:red) }
	cost = cost.gsub(/\{G\}/) { |match| match.colorize(:green) }
	cost = cost.gsub(/\{T\}/) { |match| match.colorize(:light_black) }
	cost = cost.gsub(/\{\d\}/) { |match| match.colorize(:light_black) }
end

#create_cards(card_arr) ⇒ Object



42
43
44
# File 'lib/magic_tcg_finder/command_line_interface.rb', line 42

def create_cards(card_arr)
	Card.create_from_collection(card_arr)
end

#get_queryObject



27
28
29
30
# File 'lib/magic_tcg_finder/command_line_interface.rb', line 27

def get_query
	puts "Please enter the name of a card you want to find"
	gets.strip
end

#interactionObject



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/magic_tcg_finder/command_line_interface.rb', line 110

def interaction
	puts "Please make a selection"
	puts "1. Return to card list"
	puts "2. New search"
	puts "3. Exit"
	input = gets.strip.downcase
	case input
	when "1"
		card_menu
	when "2"
		clear_screen
		run 
	when "3"
		clear_screen
		exit
	else
		puts "Invalid selection"
		interaction
	end
		
end

#list_cardsObject



46
47
48
49
50
51
# File 'lib/magic_tcg_finder/command_line_interface.rb', line 46

def list_cards
	clear_screen
	Card.all.each_with_index do |card, index|
		puts "#{index + 1}.#{card.name}" 
	end
end

#load_attributes(card) ⇒ Object



37
38
39
40
# File 'lib/magic_tcg_finder/command_line_interface.rb', line 37

def load_attributes(card)
	card_hash = Scraper.scrape_card_page(card.url)
	card.add_card_attributes(card_hash)
end

#load_card(input) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/magic_tcg_finder/command_line_interface.rb', line 75

def load_card(input)
	chosen_card = Card.all[input - 1]
	load_attributes(chosen_card) if chosen_card.rules_text.nil?
	puts "#{chosen_card.name} - #{colorize_cost(chosen_card.cost)}"
	puts ""
	puts "Color Identity: #{chosen_card.color}"
	puts "Type: #{chosen_card.card_type}"
	puts "Rarity: #{chosen_card.rarity}"
	puts ""
	puts "#{colorize_cost(chosen_card.rules_text)}"
	puts "#{chosen_card.flavor_text}"
	puts "#{chosen_card.combat_stats}"
	puts ""
	puts "Appears in the following set(s): #{chosen_card.sets.join(', ')}" 
	puts ""
	puts "Average price: $#{chosen_card.price}" unless chosen_card.price.nil? 
	puts "Purchase at #{chosen_card.purchase_url}"
	puts ""
end

#runObject



152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/magic_tcg_finder/command_line_interface.rb', line 152

def run
	Card.destroy_all
	query = get_query
	clear_screen
	card_arr = call_scraper(query)
	if card_arr == []
		try_again
	end
	create_cards(card_arr)
	card_menu
	interaction
end

#start_screenObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/magic_tcg_finder/command_line_interface.rb', line 8

def start_screen
	clear_screen
	puts "		    _            _____ _              ___      _   _               _             ".colorize(:light_green)
	puts "  /\\/\\   __ _  __ _(_) ___   _  /__   \\ |__   ___    / _ \\__ _| |_| |__   ___ _ __(_)_ __   __ _ ".colorize(:light_green)
	puts " /    \\ / _` |/ _` | |/ __| (_)   / /\\/ '_ \\ / _ \\  / /_\\/ _` | __| '_ \\ / _ \\ '__| | '_ \\ / _` |".colorize(:light_green)
	puts "/ /\\/\\ \\ (_| | (_| | | (__   _   / /  | | | |  __/ / /_\\\\ (_| | |_| | | |  __/ |  | | | | | (_| |".colorize(:light_green)
	puts "\\/    \\/\\__,_|\\__, |_|\\___| (_)  \\/   |_| |_|\\___| \\____/\\__,_|\\__|_| |_|\\___|_|  |_|_| |_|\\__, |".colorize(:light_green)
	puts "              |___/                                                                        |___/ ".colorize(:light_green)
	puts "                    ___              _     ___ _           _                                     ".colorize(:light_green)
	puts "                   / __\\__ _ _ __ __| |   / __(_)_ __   __| | ___ _ __                           ".colorize(:light_green)
	puts "                  / /  / _` | '__/ _` |  / _\\ | | '_ \\ / _` |/ _ \\ '__|                          ".colorize(:light_green)
	puts "                 / /__| (_| | | | (_| | / /   | | | | | (_| |  __/ |                             ".colorize(:light_green)
	puts "                 \\____/\\__,_|_|  \\__,_| \\/    |_|_| |_|\\__,_|\\___|_|                             ".colorize(:light_green)
  
  puts ""                             

  	run
end

#try_againObject



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/magic_tcg_finder/command_line_interface.rb', line 132

def try_again
		puts ""
		puts "Please make a selection:"
		puts "1. Search for a new card"
		puts "2. Exit this program"
		input = gets.strip

		case input
		when "1"
			clear_screen
			run
		when "2"
			clear_screen
			exit
		else
			"Please enter a valid selection"
			try_again
		end
end