big_bro 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6e07ae3b4f37d0138a36348cb513575790c26f54
4
+ data.tar.gz: 141d96855644816733bf92b068186ccd2a3bf811
5
+ SHA512:
6
+ metadata.gz: 46875404b0821804a3579cac22e4f513dc2a01031fe0eb37ca39311aff381eee5fb6e7d9f821f95d18231652cf24a3537cbd39e8e3e58013874609f5547bf32c
7
+ data.tar.gz: 7596e709c5a0b7962d0fdbac12b5b0bed6910ac4d9f61aeb4f698e64cd58409d7123d5c151aad30f764edf02119d23b2c42e4695f2859036e86ebb9f6ddc6775
data/bin/big_bro ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require_relative "../lib/big_brother"
4
+
5
+ BigBrother::Runner.run(ARGV)
@@ -0,0 +1,41 @@
1
+ require "json"
2
+
3
+ class BigBrother::Counter
4
+ IGNORED_COMMANDS = ["export"]
5
+
6
+ def self.count_commands_json(file = BigBrother::Settings.get("history_file"), api_key = nil)
7
+ lines = BigBrother::Reader.lines_from_history_file(file)
8
+ { api_key: api_key || BigBrother::Settings.get("api_key"), commands: count_commands(lines) }.to_json
9
+ end
10
+
11
+ def self.count_commands(lines)
12
+ commands = normalize_commands(lines)
13
+ commands = reject_ignored_commands(commands)
14
+ commands.each_with_object({}) do |(command, argument), count_hash|
15
+ count_hash[command] ||= {}
16
+ count_hash[command][argument] ||= 0
17
+ count_hash[command][argument] += 1
18
+ end
19
+ end
20
+
21
+ def self.normalize_commands(commands)
22
+ commands.map do |command|
23
+ normalize_single_command command
24
+ end
25
+ end
26
+
27
+ def self.reject_ignored_commands(commands)
28
+ commands.reject do |cmd, _arg|
29
+ IGNORED_COMMANDS.include? cmd.downcase
30
+ end
31
+ end
32
+
33
+ def self.normalize_single_command(command)
34
+ command, argument = reject_flags(command)[0..1]
35
+ [command, argument || ""]
36
+ end
37
+
38
+ def self.reject_flags(command)
39
+ command.split(" ").reject { |word| word[0] == "-" }
40
+ end
41
+ end
@@ -0,0 +1,7 @@
1
+ class BigBrother::Pusher
2
+ def self.push
3
+ json = BigBrother::Counter.count_commands_json
4
+ url = "http://localhost:3000/api/users"
5
+ `curl -X PUT --data big_bro='#{json}' #{url}`
6
+ end
7
+ end
@@ -0,0 +1,5 @@
1
+ class BigBrother::Reader
2
+ def self.lines_from_history_file(filename)
3
+ File.readlines(filename).map(&:strip).reject(&:empty?)
4
+ end
5
+ end
@@ -0,0 +1,68 @@
1
+ require "optparse"
2
+
3
+ class BigBrother::Runner
4
+ def self.run(cmd_line_args, options = {})
5
+ BigBrother::Settings.load
6
+ options = parse_options.merge(options)
7
+ help_message = options[:help_message]
8
+ if options[:version]
9
+ print_version
10
+ elsif options[:help] || cmd_line_args.empty? || cmd_line_args[0] == "help"
11
+ stdout.puts help_message
12
+ else
13
+ parse_cmd_line_args(cmd_line_args)
14
+ end
15
+ end
16
+
17
+ def self.parse_options
18
+ options = {}
19
+ OptionParser.new do |opts|
20
+ opts.banner = "Usage: big-bro [options]"
21
+
22
+ opts.on("-v", "--version", "Show version") do
23
+ options[:version] = true
24
+ end
25
+
26
+ opts.on("-h", "--help", "Show help") do
27
+ options[:help] = true
28
+ end
29
+ options[:help_message] = opts.to_s
30
+ end.parse!
31
+ options
32
+ end
33
+
34
+ def self.print_version
35
+ stdout.puts BigBrother::VERSION
36
+ end
37
+
38
+ def self.parse_cmd_line_args(args)
39
+ if args[0] == "json"
40
+ stdout.puts BigBrother::Counter.count_commands_json
41
+ elsif args[0] == "config"
42
+ config_commands(args)
43
+ else
44
+ stdout.puts "Big bro runner template"
45
+ end
46
+ end
47
+
48
+ def self.config_commands(args)
49
+ if args[1] == "get"
50
+ return stdout.puts "needs a value to get" unless args[2]
51
+ stdout.puts "#{args[2]} is \"\e[34m#{BigBrother::Settings.get(args[2])}\e[39m\""
52
+ elsif args[1] == "set"
53
+ return stdout.puts "needs a key and value to set" unless (args[2] && args[3])
54
+ BigBrother::Settings.set(args[2], args[3])
55
+ stdout.puts "#{args[2]} set to \"\e[34m#{args[3]}\e[39m\""
56
+ else
57
+ stdout.puts "Don't know how to use command #{args[1]}"
58
+ end
59
+ end
60
+
61
+ def self.set_output_stream(os_stream)
62
+ @output = os_stream
63
+ end
64
+
65
+ def self.stdout
66
+ @output || STDOUT
67
+ end
68
+ end
@@ -0,0 +1,41 @@
1
+ require "json"
2
+
3
+ class BigBrother::Settings
4
+ def self.settings_file
5
+ @settings_file || File.expand_path("~/.big_brother.json")
6
+ end
7
+
8
+ def self.settings_file=(path)
9
+ @settings_file = path
10
+ end
11
+
12
+ def self.defaults
13
+ {
14
+ "history_file" => File.expand_path("~/.bash_history"),
15
+ "api_key" => nil
16
+ }
17
+ end
18
+
19
+ def self.load
20
+ touch settings_file
21
+ @settings = defaults.merge(JSON.parse(File.read(settings_file)))
22
+ save
23
+ end
24
+
25
+ def self.get(setting)
26
+ @settings[setting]
27
+ end
28
+
29
+ def self.set(setting, value)
30
+ @settings[setting] = value
31
+ save
32
+ end
33
+
34
+ def self.save
35
+ File.write(settings_file, @settings.to_json)
36
+ end
37
+
38
+ def self.touch(file_name)
39
+ File.write(file_name, "{}") unless File.exists? file_name
40
+ end
41
+ end
@@ -0,0 +1,3 @@
1
+ class BigBrother
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,9 @@
1
+ class BigBrother
2
+ end
3
+
4
+ require_relative "big_brother/settings"
5
+ require_relative "big_brother/counter"
6
+ require_relative "big_brother/version"
7
+ require_relative "big_brother/runner"
8
+ require_relative "big_brother/reader"
9
+ require_relative "big_brother/pusher"
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: big_bro
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - dalexj
8
+ - vikiann
9
+ - bayendor
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2015-03-17 00:00:00.000000000 Z
14
+ dependencies: []
15
+ description: Pushes data from your terminal history up to the Ministry of Truth
16
+ email: 96dalex@gmail.com
17
+ executables:
18
+ - big_bro
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - bin/big_bro
23
+ - lib/big_brother.rb
24
+ - lib/big_brother/counter.rb
25
+ - lib/big_brother/pusher.rb
26
+ - lib/big_brother/reader.rb
27
+ - lib/big_brother/runner.rb
28
+ - lib/big_brother/settings.rb
29
+ - lib/big_brother/version.rb
30
+ homepage: https://github.com/bayendor/big_bro
31
+ licenses:
32
+ - MIT
33
+ metadata: {}
34
+ post_install_message:
35
+ rdoc_options: []
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ requirements: []
49
+ rubyforge_project:
50
+ rubygems_version: 2.2.2
51
+ signing_key:
52
+ specification_version: 4
53
+ summary: CLI hook to Big Brother
54
+ test_files: []