hostupdater 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.
data/.autotest ADDED
@@ -0,0 +1,23 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'autotest/restart'
4
+
5
+ # Autotest.add_hook :initialize do |at|
6
+ # at.extra_files << "../some/external/dependency.rb"
7
+ #
8
+ # at.libs << ":../some/external"
9
+ #
10
+ # at.add_exception 'vendor'
11
+ #
12
+ # at.add_mapping(/dependency.rb/) do |f, _|
13
+ # at.files_matching(/test_.*rb$/)
14
+ # end
15
+ #
16
+ # %w(TestA TestB).each do |klass|
17
+ # at.extra_class_map[klass] = "test/test_misc.rb"
18
+ # end
19
+ # end
20
+
21
+ # Autotest.add_hook :run_command do |at|
22
+ # system "rake build"
23
+ # end
data/History.txt ADDED
@@ -0,0 +1,6 @@
1
+ === 1.0.0 / 2010-10-14
2
+
3
+ * 1 major enhancement
4
+
5
+ * Birthday!
6
+
data/Manifest.txt ADDED
@@ -0,0 +1,8 @@
1
+ .autotest
2
+ History.txt
3
+ Manifest.txt
4
+ README.txt
5
+ Rakefile
6
+ bin/hostupdater
7
+ lib/hostupdater.rb
8
+ test/test_hostupdater.rb
data/README.txt ADDED
@@ -0,0 +1,59 @@
1
+ = hostupdater
2
+
3
+ * http://github.com/zhihengz
4
+
5
+ == DESCRIPTION:
6
+
7
+ * update host files upon request
8
+
9
+ == FEATURES/PROBLEMS:
10
+
11
+ * add/delete/update ip/host records in a file like /etc/hosts
12
+
13
+ == SYNOPSIS:
14
+
15
+ HostUpdater.add '192.168.1.100', 'example1.foo.int'
16
+ HostUpdater.delete '192.168.1.100'
17
+ HostUpdater.update '192.168.1.100', 'example2.foo.int'
18
+
19
+ == REQUIREMENTS:
20
+
21
+ * None
22
+
23
+ == INSTALL:
24
+
25
+ * sudo gem install hostupdater
26
+
27
+ == DEVELOPERS:
28
+
29
+ After checking out the source, run:
30
+
31
+ $ rake newb
32
+
33
+ This task will install any missing dependencies, run the tests/specs,
34
+ and generate the RDoc.
35
+
36
+ == LICENSE:
37
+
38
+ (The MIT License)
39
+
40
+ Copyright (c) 2010 FIX
41
+
42
+ Permission is hereby granted, free of charge, to any person obtaining
43
+ a copy of this software and associated documentation files (the
44
+ 'Software'), to deal in the Software without restriction, including
45
+ without limitation the rights to use, copy, modify, merge, publish,
46
+ distribute, sublicense, and/or sell copies of the Software, and to
47
+ permit persons to whom the Software is furnished to do so, subject to
48
+ the following conditions:
49
+
50
+ The above copyright notice and this permission notice shall be
51
+ included in all copies or substantial portions of the Software.
52
+
53
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
54
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
55
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
56
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
57
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
58
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
59
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'rubygems'
4
+ require 'hoe'
5
+
6
+ Hoe.spec 'hostupdater' do
7
+ developer('Jason Zhang', 'zhihengz@gmail.com')
8
+
9
+ self.rubyforge_name = 'jzsandbox' # if different than 'hostupdater'
10
+ end
11
+
12
+ # vim: syntax=ruby
data/bin/hostupdater ADDED
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ abort "you need to write me"
@@ -0,0 +1,49 @@
1
+ require 'fileutils'
2
+
3
+ class Hostupdater
4
+ VERSION = '1.0.0'
5
+ attr_accessor :hostfile, :hostmap
6
+
7
+ def initialize( hostfile )
8
+ @hostfile = hostfile
9
+ init_empty_file
10
+ end
11
+
12
+ def init_empty_file
13
+ FileUtils.touch @hostfile
14
+ load
15
+ end
16
+
17
+ def load
18
+ @hostmap = {}
19
+ f = File.open( @hostfile, 'r' )
20
+ f.readlines.each { |line|
21
+ arr = line.split( /\s+/ )
22
+ @hostmap[ arr[1] ] = arr[0]
23
+ }
24
+ f.close
25
+ end
26
+
27
+ def store
28
+ f = File.open( @hostfile, 'w' )
29
+ @hostmap.each_key do |key|
30
+ hostname = key
31
+ hostip = @hostmap[ key ]
32
+ f.write( hostip + " " + hostname +"\n")
33
+ end
34
+ f.close
35
+ end
36
+
37
+ def add( hostname, hostip )
38
+ load
39
+ @hostmap [ hostname ] = hostip
40
+ store
41
+ end
42
+
43
+ def delete_by_hostname( hostname )
44
+ load
45
+ @hostmap.delete hostname
46
+ store
47
+ end
48
+ end
49
+
@@ -0,0 +1,69 @@
1
+ require "test/unit"
2
+ require "hostupdater"
3
+ require "fileutils"
4
+
5
+ class TestHostupdater < Test::Unit::TestCase
6
+
7
+ def setup
8
+ @hostname = "www.google.com"
9
+ @hostip = "8.8.8.8"
10
+ @hostfile = '/tmp/testfile'
11
+ @hostupdater = Hostupdater.new( @hostfile )
12
+ end
13
+
14
+ def teardown
15
+ File.delete @hostfile
16
+ end
17
+
18
+ def test_initialize
19
+ assert_not_nil @hostupdater
20
+ assert_equal @hostfile, @hostupdater.hostfile
21
+ end
22
+
23
+ def test_load_wo_data
24
+ @hostupdater.load
25
+ assert_equal 0, @hostupdater.hostmap.length
26
+ end
27
+
28
+ def test_load_with_data
29
+ write_one_line( @hostname, @hostip )
30
+ assert_hostmap
31
+ end
32
+
33
+ def test_store
34
+ @hostupdater.hostmap = { @hostname => @hostip }
35
+ @hostupdater.store
36
+ assert_hostmap
37
+ end
38
+
39
+ def test_add
40
+ @hostupdater.add( @hostname, @hostip )
41
+ assert_hostmap
42
+ end
43
+
44
+ def test_delete_hostname
45
+ write_one_line( @hostname, @hostip )
46
+ write_one_line( "www.yahoo.com", "4.4.4.4" )
47
+ @hostupdater.delete_by_hostname( "www.yahoo.com" )
48
+ assert_hostmap
49
+ end
50
+
51
+ def test_update
52
+ write_one_line( @hostname, "whatever" )
53
+ @hostupdater.add( @hostname, @hostip )
54
+ assert_hostmap
55
+ end
56
+
57
+ def write_one_line( hostname, hostip )
58
+ hostf = File.open( @hostfile, 'a' );
59
+ hostf.write( hostip + "\t" + hostname +"\n")
60
+ hostf.close
61
+ end
62
+
63
+ def assert_hostmap
64
+ @hostupdater.load
65
+ hostmap = @hostupdater.hostmap
66
+ assert_equal 1, hostmap.length
67
+ assert_equal @hostip, hostmap[ @hostname ]
68
+ end
69
+ end
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hostupdater
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease: false
6
+ segments:
7
+ - 1
8
+ - 0
9
+ - 0
10
+ version: 1.0.0
11
+ platform: ruby
12
+ authors:
13
+ - Jason Zhang
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-10-22 00:00:00 -05:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rubyforge
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 7
30
+ segments:
31
+ - 2
32
+ - 0
33
+ - 4
34
+ version: 2.0.4
35
+ type: :development
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: hoe
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 19
46
+ segments:
47
+ - 2
48
+ - 6
49
+ - 2
50
+ version: 2.6.2
51
+ type: :development
52
+ version_requirements: *id002
53
+ description: "* update host files upon request"
54
+ email:
55
+ - zhihengz@gmail.com
56
+ executables:
57
+ - hostupdater
58
+ extensions: []
59
+
60
+ extra_rdoc_files:
61
+ - History.txt
62
+ - Manifest.txt
63
+ - README.txt
64
+ files:
65
+ - .autotest
66
+ - History.txt
67
+ - Manifest.txt
68
+ - README.txt
69
+ - Rakefile
70
+ - bin/hostupdater
71
+ - lib/hostupdater.rb
72
+ - test/test_hostupdater.rb
73
+ has_rdoc: true
74
+ homepage: http://github.com/zhihengz
75
+ licenses: []
76
+
77
+ post_install_message:
78
+ rdoc_options:
79
+ - --main
80
+ - README.txt
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ hash: 3
89
+ segments:
90
+ - 0
91
+ version: "0"
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ none: false
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ hash: 3
98
+ segments:
99
+ - 0
100
+ version: "0"
101
+ requirements: []
102
+
103
+ rubyforge_project: jzsandbox
104
+ rubygems_version: 1.3.7
105
+ signing_key:
106
+ specification_version: 3
107
+ summary: "* update host files upon request"
108
+ test_files:
109
+ - test/test_hostupdater.rb