Module: JSONtoDB::Processor

Defined in:
lib/jsontodb/processor.rb

Overview

This module processes commands passed to the command-line interface

Class Method Summary collapse

Class Method Details

.check_args(command, expected, actual) ⇒ Object



68
69
70
71
72
# File 'lib/jsontodb/processor.rb', line 68

def check_args(command, expected, actual)
  return true if actual.length == expected
  puts "ERROR: Command '#{command}' expected #{expected} parameter(s) but received #{actual.length}!"
  false
end

.check_authObject



36
37
38
39
40
41
42
43
# File 'lib/jsontodb/processor.rb', line 36

def check_auth
  @user = JSONtoDB::CONFIG['User']
  @pass = JSONtoDB::CONFIG['Pass']
  return unless JSONtoDB::CONFIG['CredPrompt']
  JSONtoDB::CLI.authentication_credentials(@user, @pass)
  @user = JSONtoDB::CLI.user
  @pass = JSONtoDB::CLI.pass
end

.run_command(args, user, pass) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/jsontodb/processor.rb', line 45

def run_command(args, user, pass)
  return if args.empty?
  command = args.shift

  case command
  when 'get'
    return unless check_args('put', 1, args)
    res = JSONtoDB::REST.get(args[0], user, pass)
    puts res
  when 'delete'
    return unless check_args('put', 1, args)
    JSONtoDB::REST.delete(args[0], user, pass)
  when 'put'
    return unless check_args('put', 2, args)
    JSONtoDB::REST.put(args[0], args[1], user, pass)
  when 'post'
    return unless check_args('put', 2, args)
    JSONtoDB::REST.post(args[0], args[1], user, pass)
  else
    puts "Unknown command '#{command}'."
  end
end

.run_configObject



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
# File 'lib/jsontodb/processor.rb', line 7

def run_config
  check_auth

  unless JSONtoDB::CONFIG['Matrix'].nil?
    JSONtoDB::CONFIG['Matrix'].each do |hash|
      files = hash['Files']
      url = hash['Url']
      command = hash['Command']

      if files.nil?
        run_command([command, url], @user, @pass)
      elsif files.is_a?(Array)
        files.each do |file|
          Dir[file].each do |f|
            run_command([command, url, f], @user, @pass)
          end
        end
      else
        entries = Dir[files].entries.sort_by { |x| -x[/\d+/].to_i }
        entries.each do |f|
          run_command([command, url, f], @user, @pass)
        end
      end
    end
  end

  JSONtoDB::CLI.continuous_cli if JSONtoDB::CONFIG['Interactive']
end