Class: PleaseValidate::CLI

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(arguments) ⇒ CLI

Takes the arguments, passes it to validate for validation and displays the result



39
40
41
42
# File 'lib/please_validate/cli.rb', line 39

def initialize(arguments)
  @result = validate arguments
  @msg = display
end

Class Method Details

.execute(stdout, arguments = []) ⇒ Object

Execute the CLI



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
# File 'lib/please_validate/cli.rb', line 8

def execute(stdout, arguments=[])
  options = {}
  mandatory_options = %w(  )

  parser = OptionParser.new do |opts|
    opts.banner = <<-BANNER.gsub(/^          /,'')
      Please Validate does some lovely markup validation of your (X)HTML files.

      Usage: #{File.basename($0)} /path/to/file [options]

      Options are:
    BANNER
    opts.separator ""
    # opts.on("-p", "--path=PATH", String,
    #                   "This is a sample message.",
    #                   "For multiple lines, add more strings.",
    #                   "Default: ~") { |arg| options[:path] = arg }
    opts.on("-h", "--help",
            "Show this help message.") { stdout.puts opts; exit }
    opts.parse!(arguments)

    if mandatory_options && mandatory_options.find { |option| options[option.to_sym].nil? }
      stdout.puts opts; exit
    end
  end
  validation = new(arguments)
  stdout.puts validation.msg
end

Instance Method Details

#displayObject

Displays the request’s results



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/please_validate/cli.rb', line 51

def display
  @result.inject('') do |msg,result|
    if result.is_a? Hash
      msg += "#{result[:status].to_s.capitalize}: #{result[:file]}".send(result[:status] == :valid ? :on_green : :on_red)
      if result[:status] == :invalid
        msg += "\n#{result[:error_count]} error#{result[:error_count] == 1 ? nil:'s'}:"
        result[:errors].each do |error|
          msg += "\nLine #{error[:line]}, Column #{error[:col]}".red + ": #{error[:message]}"
        end
      end
    elsif result.is_a? String
      msg += result
    end
    msg += "\n\n"
    msg
  end
end

#msgObject

Displays the message for the command line result



70
71
72
# File 'lib/please_validate/cli.rb', line 70

def msg
  @msg
end

#validate(items) ⇒ Object

Creates a new request object and returns its result



45
46
47
48
# File 'lib/please_validate/cli.rb', line 45

def validate(items)
  request = PleaseValidate::Request.new(items)
  request.result
end