Module: Ronin::DB::CLI::Importable Private

Defined in:
lib/ronin/db/cli/importable.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Allows a ModelCommand to add or import records from a file.

Since:

  • 0.2.0

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(command) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Adds the --add and --import options to the command.

Parameters:

Since:

  • 0.2.0



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/ronin/db/cli/importable.rb', line 36

def self.included(command)
  command.option :add, value: {
                         type: String,
                         usage: 'VALUE'
                       },
                       desc: 'Adds a value to the database'

  command.option :import, value: {
                            type:  String,
                            usage: 'FILE'
                          },
                          desc: 'Imports the values from the FILE into the database'
end

Instance Method Details

#add(value) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Adds a value to the database.

Parameters:

  • value (String)

    The value to add.

Since:

  • 0.2.0



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/ronin/db/cli/importable.rb', line 71

def add(value)
  record = model.import(value)

  unless record.valid?
    print_error "failed to import #{value}!"

    record.errors.full_messages.each do |message|
      print_error " - #{message}"
    end
  end
end

#import_file(path) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Imports the values from the given file.

Parameters:

  • path (String)

    The path to the file.

Since:

  • 0.2.0



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/ronin/db/cli/importable.rb', line 89

def import_file(path)
  unless File.file?(path)
    print_error "no such file or directory: #{path}"
    exit(-1)
  end

  File.open(path) do |file|
    model.transaction do
      file.each_line(chomp: true) do |value|
        log_info "Importing #{value} ..." if verbose?

        add(value)
      end
    end
  end
end

#runObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Runs the command.

Since:

  • 0.2.0



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/ronin/db/cli/importable.rb', line 53

def run
  if options[:add]
    db_connect
    add(options[:add])
  elsif options[:import]
    db_connect
    import_file(options[:import])
  else
    super
  end
end