-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsole.rb
More file actions
68 lines (55 loc) · 1.36 KB
/
console.rb
File metadata and controls
68 lines (55 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
require 'slop'
require 'colorize'
class Console
attr_accessor :url, :filename
def initialize
get_parameters
end
def get_parameters
opts = Slop.parse suppress_errors: true do |o|
o.string '...'
o.string '...', default: 'data'
end
check_parameters(opts.arguments)
@url = opts.arguments.first
@filename = opts.arguments.last.gsub('.csv', '')
end
def banner
puts 'Invalid arguments. Usage: '.colorize(:red)
puts "\t./task_ruby_1.rb 'URL' 'FILENAME'".colorize(:green)
end
def uri?(string)
url = URI.encode(string)
uri = URI.parse(url)
%w( http https ).include?(uri.scheme)
rescue URI::BadURIError
false
rescue URI::InvalidURIError
false
end
def check_parameters(args)
if args.size != 2
Console.banner
exit
end
unless uri?(args.first)
Console.banner
exit
end
end
def Console.fetching_page(index)
puts "Fetching page: #{index}".colorize(:blue)
end
def Console.fetching_multiproduct(index)
puts " Fetching multiproduct: #{index}".colorize(:yellow)
end
def Console.fetching_product(index)
puts " Fetching product: #{index}".colorize(:green)
end
def Console.done(count)
puts "Done.\nTotal: #{count} products".colorize(:red)
end
def Console.file_saved(name)
puts "File '#{name}.csv' saved.".colorize(:blue)
end
end