gbwd 0.0.1

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.
Binary file
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in gbwd.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Thomas Brewer
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,53 @@
1
+ # Gbwd
2
+
3
+ A little Gem to help eliminate website distractions. It basically routes any requests for the domains that are being blocked to [http://getbacktoworkdummy.com](http://getbacktoworkdummy.com "http://getbacktoworkdummy.com")
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'gbwd'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install gbwd
18
+
19
+ ## Usage
20
+
21
+ *NOTE: Please back up your /etc/hosts file before running any commands.*
22
+
23
+ Add a domain to be blocked:
24
+
25
+ sudo gbwd add -d www.youtube.com
26
+
27
+ Remove a domain from being blocked:
28
+
29
+ sudo gbwd remove -d www.youtube.com
30
+
31
+ List all domains being blocked:
32
+
33
+ sudo gbwd list
34
+
35
+ Stop all domains from being blocked:
36
+
37
+ sudo gbwd disable
38
+
39
+ Start blocking all domains again:
40
+
41
+ sudo gbwd enable
42
+
43
+ ## Todos
44
+ 1. Make a copy of /etc/hosts file before modifying it
45
+
46
+
47
+ ## Contributing
48
+
49
+ 1. Fork it
50
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
51
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
52
+ 4. Push to the branch (`git push origin my-new-feature`)
53
+ 5. Create new Pull Request
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,41 @@
1
+ #!/usr/bin/env ruby -w
2
+
3
+ require 'subcommand'
4
+ require 'gbwd'
5
+
6
+ include Subcommands
7
+
8
+ options = {}
9
+
10
+ command :add do |opts|
11
+ opts.banner = "Usage: add -d www.youtube.com"
12
+ opts.description = "Add a domain to the list"
13
+ opts.on('-d', '-domain DOMAIN', 'Domain name (without the http://)') do |domain|
14
+ options[:domain] = domain
15
+ end
16
+ end
17
+
18
+ command :remove do |opts|
19
+ opts.banner = "Usage: remove -d www.youtube.com"
20
+ opts.description = "Removes a domain to the list"
21
+ opts.on('-d', '-domain DOMAIN', 'Domain name (without the http://)') do |domain|
22
+ options[:domain] = domain
23
+ end
24
+ end
25
+
26
+ command :disable do |opts|
27
+ opts.banner = "Usage: disable"
28
+ opts.description = "Disables all domains being blocked."
29
+ end
30
+
31
+ command :enable do |opts|
32
+ opts.banner = "Usage: enable"
33
+ opts.description = "Enables all domains being blocked."
34
+ end
35
+
36
+ command :list do |opts|
37
+ opts.banner = "Usage: list"
38
+ opts.description = "Lists all domain on the list"
39
+ end
40
+
41
+ puts Gbwd.start(opt_parse(), options)
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/gbwd/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Thomas Brewer"]
6
+ gem.email = ["tom@21purple.com"]
7
+ gem.description = %q{Command line tool for the highly performant and scalable GBWD service}
8
+ gem.summary = gem.description
9
+ gem.homepage = ""
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = %w(gbwd)
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = "gbwd"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Gbwd::VERSION
17
+ gem.add_development_dependency('fakefs')
18
+ gem.add_development_dependency('rspec')
19
+ gem.add_dependency('subcommand')
20
+ end
Binary file
@@ -0,0 +1,14 @@
1
+ require "gbwd/version"
2
+ require "gbwd/cli"
3
+ require "gbwd/commands"
4
+
5
+ module Gbwd
6
+
7
+ def self.start(command, options)
8
+ commands = Commands.new
9
+ cli = Cli.new(commands)
10
+ cli.run(command, options)
11
+ end
12
+
13
+ end
14
+
Binary file
@@ -0,0 +1,19 @@
1
+ module Gbwd
2
+
3
+ class Cli
4
+
5
+ def initialize(commands)
6
+ @commands = commands
7
+ end
8
+
9
+ def commands
10
+ @commands
11
+ end
12
+
13
+ def run(command, options)
14
+ commands.public_send(command.to_sym, options)
15
+ end
16
+
17
+ end
18
+
19
+ end
@@ -0,0 +1,119 @@
1
+ module Gbwd
2
+
3
+ class Commands
4
+
5
+ def initialize
6
+ @hosts_file_path = '/etc/hosts'
7
+ @ip_address = '69.55.54.215'
8
+ end
9
+
10
+ def add(options = {})
11
+ modify(options) do |domains, domain|
12
+ domains << [ip_address, domain].join(' ')
13
+ "You added #{domain}."
14
+ end
15
+ end
16
+
17
+ def remove(options = {})
18
+ modify(options) do |domains, domain|
19
+ domains.select! do |domain_to_check|
20
+ (Regexp.new(domain) =~ domain_to_check).nil?
21
+ end
22
+ "You removed #{domain}."
23
+ end
24
+ end
25
+
26
+ def enable(options = {})
27
+ modify(options) do |domains, domain|
28
+ domains.map! do |line|
29
+ line[1..-1] if line.start_with?('#')
30
+ end
31
+ "All domains have been enabled."
32
+ end
33
+ end
34
+
35
+ def disable(options = {})
36
+ modify(options) do |domains, domain|
37
+ domains.map! do |line|
38
+ "#" + line
39
+ end
40
+ "All domains have been disabled."
41
+ end
42
+ end
43
+
44
+ def list(options = {})
45
+ install unless installed?
46
+ domains = get_domains_from_file
47
+ if domains.size == 0
48
+ [
49
+ "You do not have any blocked domains. You can add one by using the add command.",
50
+ " ex. ~ sudo gbwd add -d www.youtube.com"
51
+ ].join("\n")
52
+ else
53
+ domains = domains.map do |line|
54
+ line.split(' ')[1]
55
+ end
56
+ "The following domains are currenlty being blocked:\n" + domains.join("\n")
57
+ end
58
+ end
59
+
60
+ private
61
+
62
+ def modify(options)
63
+ raise ArgumentError.new('You must pass a block to the modify method.') unless block_given?
64
+ install unless installed?
65
+ domain = options.fetch(:domain, nil)
66
+ domains = get_domains_from_file
67
+ response = yield(domains, domain)
68
+ write_to_hosts_file(domains)
69
+ response
70
+ end
71
+
72
+ def hosts_file_path
73
+ @hosts_file_path
74
+ end
75
+
76
+ def ip_address
77
+ @ip_address
78
+ end
79
+
80
+ def get_domains_from_file
81
+ file = File.open(hosts_file_path, 'r').read
82
+ matches = file.match(/#-GBWD START(.*)#-GBWD END/m)
83
+ domains = []
84
+ unless matches.nil?
85
+ matches.captures[0].split("\n").each do |domain|
86
+ unless domain.empty?
87
+ domains << domain
88
+ end
89
+ end
90
+ end
91
+ domains
92
+ end
93
+
94
+ def wrap_with_header_and_footer(lines)
95
+ lines.insert(0, '#-GBWD START')
96
+ lines << '#-GBWD END'
97
+ end
98
+
99
+ def write_to_hosts_file(lines)
100
+ updated_content = wrap_with_header_and_footer(lines).join("\n")
101
+ file = File.open(hosts_file_path, 'r').read
102
+ updated_file = file.gsub(/#-GBWD START.*#-GBWD END/m, updated_content)
103
+ File.open(hosts_file_path, 'w').write(updated_file)
104
+ end
105
+
106
+ def installed?
107
+ file = File.open(hosts_file_path, 'r').read
108
+ (file.match(/#-GBWD START/)) ? true : false
109
+ end
110
+
111
+ def install
112
+ File.open(hosts_file_path, 'a+') do |file|
113
+ file.puts("#-GBWD START\n#-GBWD END")
114
+ end
115
+ end
116
+
117
+ end
118
+
119
+ end
@@ -0,0 +1,3 @@
1
+ module Gbwd
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,13 @@
1
+ require 'spec_helper'
2
+
3
+ describe Gbwd::Cli do
4
+
5
+ it "#run should call the appropriate sub command when passed a command" do
6
+ commands = double("Commands")
7
+ commands.should_receive(:add)
8
+ cli = Gbwd::Cli.new(commands)
9
+ options = double("options")
10
+ cli.run('add', options)
11
+ end
12
+
13
+ end
@@ -0,0 +1,86 @@
1
+ require 'spec_helper'
2
+
3
+ describe Gbwd::Commands, fakefs: true do
4
+
5
+ before do
6
+ FileUtils.mkdir("/etc")
7
+ File.open("/etc/hosts", "w") do |f|
8
+ f.puts("##")
9
+ f.puts("# Host Database")
10
+ f.puts("#")
11
+ f.puts("# localhost is used to configure the loopback interface")
12
+ f.puts("# when the system is booting. Do not change this entry.")
13
+ f.puts("##")
14
+ f.puts("127.0.0.1 localhost")
15
+ f.puts("255.255.255.255 broadcasthost")
16
+ f.puts("::1 localhost")
17
+ f.puts("fe80::1%lo0 localhost")
18
+ f.puts("\n")
19
+ f.puts("\n")
20
+ f.puts("# VHX START")
21
+ f.puts("127.0.0.1 test.dev")
22
+ f.puts("fe80::1%lo0 test.dev")
23
+ f.puts("# VHX STOP")
24
+ end
25
+ end
26
+
27
+ it "#add should add a domain to the hosts file" do
28
+ commands = Gbwd::Commands.new
29
+ commands.add({domain: 'www.youtube.com'})
30
+ file = File.open("/etc/hosts", 'r').read
31
+ file.match(/www.youtube.com/m).should_not eq(nil)
32
+ end
33
+
34
+ it "#remove should remove a domain to the hosts file" do
35
+ commands = Gbwd::Commands.new
36
+ commands.add({domain: 'www.youtube.com'})
37
+ commands.remove({domain: 'www.youtube.com'})
38
+ file = File.open("/etc/hosts", 'r').read
39
+ file.match(/www.youtube.com/m).should eq(nil)
40
+ end
41
+
42
+ it "#list should return a list of all domains" do
43
+ commands = Gbwd::Commands.new
44
+ commands.add({domain: 'www.youtube.com'})
45
+ commands.add({domain: 'www.facebook.com'})
46
+ commands.list.should eq("The following domains are currenlty being blocked:\nwww.youtube.com\nwww.facebook.com")
47
+ end
48
+
49
+ it "#disable should comment out all lines" do
50
+ commands = Gbwd::Commands.new
51
+ commands.add({domain: 'www.youtube.com'})
52
+ commands.add({domain: 'www.facebook.com'})
53
+ commands.disable({})
54
+ file = File.open("/etc/hosts", 'r').read
55
+ matches = file.match(/#-GBWD START(.*)#-GBWD END/m)
56
+ unless matches.nil?
57
+ matches.captures[0].split("\n").each do |domain|
58
+ unless domain.empty?
59
+ domain.start_with?('#').should eq(true)
60
+ end
61
+ end
62
+ else
63
+ raise 'No domains found'
64
+ end
65
+ end
66
+
67
+ it "#enable should uncomment all lines" do
68
+ commands = Gbwd::Commands.new
69
+ commands.add({domain: 'www.youtube.com'})
70
+ commands.add({domain: 'www.facebook.com'})
71
+ commands.disable({})
72
+ commands.enable({})
73
+ file = File.open("/etc/hosts", 'r').read
74
+ matches = file.match(/#-GBWD START(.*)#-GBWD END/m)
75
+ unless matches.nil?
76
+ matches.captures[0].split("\n").each do |domain|
77
+ unless domain.empty?
78
+ domain.start_with?('#').should_not eq(true)
79
+ end
80
+ end
81
+ else
82
+ raise 'No domains found'
83
+ end
84
+ end
85
+
86
+ end
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ describe Gbwd do
4
+
5
+
6
+
7
+ end
@@ -0,0 +1,10 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'rspec'
3
+ require 'gbwd'
4
+ require 'fakefs/spec_helpers'
5
+
6
+ RSpec.configure do |config|
7
+ config.mock_with :rspec
8
+ config.include FakeFS::SpecHelpers, fakefs: true
9
+ end
10
+
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gbwd
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Thomas Brewer
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-08 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: fakefs
16
+ requirement: &70343999590760 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70343999590760
25
+ - !ruby/object:Gem::Dependency
26
+ name: rspec
27
+ requirement: &70343999590320 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70343999590320
36
+ - !ruby/object:Gem::Dependency
37
+ name: subcommand
38
+ requirement: &70343999589720 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: *70343999589720
47
+ description: Command line tool for the highly performant and scalable GBWD service
48
+ email:
49
+ - tom@21purple.com
50
+ executables:
51
+ - gbwd
52
+ extensions: []
53
+ extra_rdoc_files: []
54
+ files:
55
+ - .DS_Store
56
+ - .gitignore
57
+ - Gemfile
58
+ - LICENSE
59
+ - README.md
60
+ - Rakefile
61
+ - bin/gbwd
62
+ - gbwd.gemspec
63
+ - lib/.DS_Store
64
+ - lib/gbwd.rb
65
+ - lib/gbwd/.DS_Store
66
+ - lib/gbwd/cli.rb
67
+ - lib/gbwd/commands.rb
68
+ - lib/gbwd/version.rb
69
+ - spec/gbwd/cli_spec.rb
70
+ - spec/gbwd/commands_spec.rb
71
+ - spec/gbwd_spec.rb
72
+ - spec/spec_helper.rb
73
+ homepage: ''
74
+ licenses: []
75
+ post_install_message:
76
+ rdoc_options: []
77
+ require_paths:
78
+ - lib
79
+ required_ruby_version: !ruby/object:Gem::Requirement
80
+ none: false
81
+ requirements:
82
+ - - ! '>='
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ none: false
87
+ requirements:
88
+ - - ! '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubyforge_project:
93
+ rubygems_version: 1.8.10
94
+ signing_key:
95
+ specification_version: 3
96
+ summary: Command line tool for the highly performant and scalable GBWD service
97
+ test_files:
98
+ - spec/gbwd/cli_spec.rb
99
+ - spec/gbwd/commands_spec.rb
100
+ - spec/gbwd_spec.rb
101
+ - spec/spec_helper.rb