Class: BigBrother::Counter

Inherits:
Object
  • Object
show all
Defined in:
lib/big_brother/counter.rb

Constant Summary collapse

IGNORED_COMMANDS =
%w(export cd ls clear ssh ftp scp curl)

Class Method Summary collapse

Class Method Details

.count_commands(lines) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/big_brother/counter.rb', line 11

def self.count_commands(lines)
  commands = normalize_commands(lines)
  commands = reject_ignored_commands(commands)
  commands.each_with_object({}) do |(command, argument), count_hash|
    count_hash[command] ||= {}
    count_hash[command][argument] ||= 0
    count_hash[command][argument] += 1
  end
end

.count_commands_json(file = BigBrother::Settings.get("history_file"), api_key = nil) ⇒ Object



6
7
8
9
# File 'lib/big_brother/counter.rb', line 6

def self.count_commands_json(file = BigBrother::Settings.get("history_file"), api_key = nil)
  lines = BigBrother::Reader.lines_from_history_file(file)
  { api_key: api_key || BigBrother::Settings.get("api_key"), commands: count_commands(lines) }.to_json
end

.normalize_commands(commands) ⇒ Object



21
22
23
24
25
# File 'lib/big_brother/counter.rb', line 21

def self.normalize_commands(commands)
  commands.map do |command|
    normalize_single_command command
  end
end

.normalize_single_command(command) ⇒ Object



33
34
35
36
# File 'lib/big_brother/counter.rb', line 33

def self.normalize_single_command(command)
  command, argument = reject_flags(command)[0..1]
  [command, argument || ""]
end

.reject_flags(command) ⇒ Object



38
39
40
# File 'lib/big_brother/counter.rb', line 38

def self.reject_flags(command)
  command.split(" ").reject { |word| word[0] == "-" }
end

.reject_ignored_commands(commands) ⇒ Object



27
28
29
30
31
# File 'lib/big_brother/counter.rb', line 27

def self.reject_ignored_commands(commands)
  commands.reject do |cmd, _arg|
    IGNORED_COMMANDS.include? cmd.downcase
  end
end