Module: Kuport::Helper

Included in:
Kuport
Defined in:
lib/kuport/helper.rb

Instance Method Summary collapse

Instance Method Details

#clear_proxy_env_varObject



60
61
62
# File 'lib/kuport/helper.rb', line 60

def clear_proxy_env_var
  ENV.delete_if{|k,v| k =~ /_proxy/i }
end

#color_str(color_num, str) ⇒ Object



17
18
19
# File 'lib/kuport/helper.rb', line 17

def color_str(color_num, str)
  "\e[38;5;#{color_num}m#{str}\e[00m"
end

#escape_filename(filename) ⇒ Object



13
14
15
# File 'lib/kuport/helper.rb', line 13

def escape_filename(filename)
  File.basename(filename.strip)
end

#get_page_doc(page) ⇒ Object



21
22
23
# File 'lib/kuport/helper.rb', line 21

def get_page_doc(page)
  Nokogiri::HTML.parse(page.content.toutf8)
end

#get_proxy_env_varObject



46
47
48
49
50
51
52
# File 'lib/kuport/helper.rb', line 46

def get_proxy_env_var
  keys = %w[ http_proxy https_proxy all_proxy ].map{|s| [s, s.upcase]}.flatten
  keys.each do |key|
    return ENV[key] if ENV.key?(key)
  end
  return nil
end

#input_num(str) ⇒ Object



25
26
27
28
29
30
# File 'lib/kuport/helper.rb', line 25

def input_num(str)
  print str
  gets.to_i rescue nil
rescue Interrupt
  exit 1
end

#input_passwdObject



32
33
34
35
36
37
38
39
# File 'lib/kuport/helper.rb', line 32

def input_passwd
  $stderr.print 'Password> '
  pass = STDIN.noecho(&:gets).chomp rescue nil # &.chomp
  puts
  return pass
rescue Interrupt
  exit 2
end

#parse_proxy_str(str) ⇒ Object



54
55
56
57
58
# File 'lib/kuport/helper.rb', line 54

def parse_proxy_str(str)
  str = 'http://' + str if str !~ /\A\w+:\/\//
  u = URI.parse(str)
  [u.host, u.port]
end

#quit(mes, ret = false) ⇒ Object



41
42
43
44
# File 'lib/kuport/helper.rb', line 41

def quit(mes, ret=false)
  warn mes
  exit ret
end

#to_abs_url(base, part) ⇒ Object



7
8
9
10
11
# File 'lib/kuport/helper.rb', line 7

def to_abs_url(base, part)
  uri = URI.parse(part)
  uri = URI.join(base, uri) if URI::Generic === uri
  uri.to_s
end