Class: Schatz::Cli::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/schatz/cli/configuration.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output = STDOUT, error = STDERR, options = {}) ⇒ Configuration

Returns a new instance of Configuration.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/schatz/cli/configuration.rb', line 5

def initialize(output=STDOUT, error=STDERR,options={})
  @stdout = output
  @stderr = error
  @options =options
  @opt = OptionParser.new
  @opt.banner = "Usage: schatz [file_name]"
  @opt.separator "Options:"
  @opt.on("-f", "--file [file]")    {|file|    @options[:file_name]  = file}
  @opt.on("-h", "--help")     {|ignore|   @options[:help]       = true}
  @opt.on("--po")       {|ignore|  @options[:proxy_only] = true}
  @opt.on("--do")       {|ignore|  @options[:direct_only]  = true}
  @opt.on("-g", "--gcm [gcm]")    {|gcm|    @options[:gcm]       = "gcm000000" +gcm}
  @opt.on("-p", "--prx [proxy]")    {|proxy|    @options[:proxies]     = {1 => proxy}}
end

Instance Attribute Details

#optObject

Returns the value of attribute opt.



4
5
6
# File 'lib/schatz/cli/configuration.rb', line 4

def opt
  @opt
end

#optionsObject

Returns the value of attribute options.



4
5
6
# File 'lib/schatz/cli/configuration.rb', line 4

def options
  @options
end

#stderrObject

Returns the value of attribute stderr.



4
5
6
# File 'lib/schatz/cli/configuration.rb', line 4

def stderr
  @stderr
end

#stdoutObject

Returns the value of attribute stdout.



4
5
6
# File 'lib/schatz/cli/configuration.rb', line 4

def stdout
  @stdout
end

Instance Method Details

#full_file_name(file_name) ⇒ Object



34
35
36
37
38
# File 'lib/schatz/cli/configuration.rb', line 34

def full_file_name(file_name)
  raise FileNotExistInCurrentDirectory until File.exist?(file_name)
  raise IncorrectFileFormat until file_name =~ /\.xlsx?/ || file_name =~ /\.html?/ || file_name =~ /\.txt?/
  File.expand_path(file_name)
end

#parse!(argv) ⇒ Object

Raises:

  • (ArgumentError)


20
21
22
23
24
25
26
27
# File 'lib/schatz/cli/configuration.rb', line 20

def parse!(argv)
  raise ArgumentError, "expected array of argv" unless argv.is_a?(Array)
  raise ArgumentError, "you should provide at least a name of the incr" if argv.empty? || argv.first=="-f" && argv.length==1
  @opt.parse(argv)
  @options[:file_name] = full_file_name(@options[:file_name]) if @options[:file_name]
  @options[:proxies] ||= read_config
  show_info
end

#read_configObject



40
41
42
# File 'lib/schatz/cli/configuration.rb', line 40

def read_config
  YAML::load_file(File.expand_path(File.dirname(__FILE__)+"./../../../config/config.yml"))["proxies"]
end

#show_infoObject



29
30
31
32
# File 'lib/schatz/cli/configuration.rb', line 29

def show_info
  puts @opt if @options[:help]
  puts "GCM number is not provided so will check all links. If you want to test only those implemented after specified GCM please mention it".foreground(:yellow) unless @options[:gcm]
end