hosts_file 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -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
@@ -0,0 +1,7 @@
1
+ ---
2
+ script: 'rake spec'
3
+ rvm:
4
+ - 1.8.7
5
+ - 1.9.2
6
+ - 1.9.3
7
+ - 2.0.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in hosts_file.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Joshua Bussdieker
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,18 @@
1
+ # HostsFile
2
+
3
+ [![Build Status](https://travis-ci.org/jbussdieker/ruby-hosts_file.png?branch=master)](https://travis-ci.org/jbussdieker/ruby-hosts_file)
4
+ [![Code Climate](https://codeclimate.com/github/jbussdieker/ruby-hosts_file.png)](https://codeclimate.com/github/jbussdieker/ruby-hosts_file)
5
+ [![Gem Version](https://badge.fury.io/rb/hosts_file.png)](http://badge.fury.io/rb/hosts_file)
6
+
7
+ Basic library for reading hosts file entries.
8
+
9
+ ## Usage
10
+
11
+ require 'hosts_file'
12
+
13
+ hosts = HostsFile.read("/etc/hosts")
14
+ hosts.collect {|host| host.ip}
15
+ hosts.collect {|host| host.name}
16
+ hosts.collect {|host| host.aliases}
17
+ hosts.find {|host| host.hostname == "somehost"}
18
+ hosts.find {|host| host.ip == "1.1.1.1"}
@@ -0,0 +1,4 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'hosts_file/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "hosts_file"
8
+ spec.version = HostsFile::VERSION
9
+ spec.authors = ["Joshua Bussdieker"]
10
+ spec.email = ["jbussdieker@gmail.com"]
11
+ spec.summary = %q{Basic library for reading hosts file entries.}
12
+ spec.homepage = "http://github.com/jbussdieker/ruby-hosts_file"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files`.split($/)
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.3"
21
+ spec.add_development_dependency "rake"
22
+ spec.add_development_dependency "rspec"
23
+ end
@@ -0,0 +1,8 @@
1
+ require "hosts_file/version"
2
+ require "hosts_file/file"
3
+
4
+ module HostsFile
5
+ def self.load(filename = "/etc/hosts")
6
+ File.new(filename)
7
+ end
8
+ end
@@ -0,0 +1,39 @@
1
+ require 'hosts_file/parser'
2
+ require 'hosts_file/host'
3
+
4
+ module HostsFile
5
+ class File
6
+ include Enumerable
7
+
8
+ def initialize(filename)
9
+ @filename = filename
10
+ end
11
+
12
+ # @yield {Host}
13
+ # @return Array<Host>
14
+ def hosts(&block)
15
+ if block_given?
16
+ each(&block)
17
+ else
18
+ collect {|host| host}
19
+ end
20
+ end
21
+
22
+ # @yield {Host}
23
+ def each(&block)
24
+ parser.each do |host|
25
+ yield(host)
26
+ end
27
+ end
28
+
29
+ private
30
+
31
+ def parser
32
+ @hosts ||= Parser.new(raw_file)
33
+ end
34
+
35
+ def raw_file
36
+ @raw_file ||= ::File.read(@filename)
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,19 @@
1
+ module HostsFile
2
+ class Host
3
+ attr_accessor :ip_address, :canonical_hostname, :aliases
4
+
5
+ def initialize(ip_address, canonical_hostname, aliases = [])
6
+ @ip_address = ip_address
7
+ @canonical_hostname = canonical_hostname
8
+ @aliases = aliases
9
+ end
10
+
11
+ def hostname
12
+ canonical_hostname
13
+ end
14
+
15
+ def ip
16
+ ip_address
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,32 @@
1
+ module HostsFile
2
+ class Parser
3
+ include Enumerable
4
+
5
+ def initialize(data)
6
+ @data = data
7
+ end
8
+
9
+ def each(&block)
10
+ file_lines.each do |line|
11
+ host = parse_line(line)
12
+ yield(host) if host
13
+ end
14
+ end
15
+
16
+ private
17
+
18
+ def parse_line(line)
19
+ unless line.start_with? "#"
20
+ parts = line.split(" ")
21
+ return nil unless parts.length > 1
22
+ Host.new(parts[0], parts[1], parts[2..-1])
23
+ else
24
+ nil
25
+ end
26
+ end
27
+
28
+ def file_lines
29
+ @file_lines ||= @data.split("\n")
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,3 @@
1
+ module HostsFile
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,22 @@
1
+ require 'spec_helper'
2
+ require 'hosts_file/file'
3
+
4
+ describe HostsFile::File do
5
+ around(:each) do |example|
6
+ TempDir.create do |dir|
7
+ @dir = dir
8
+ example.run
9
+ end
10
+ end
11
+
12
+ it "should handle basic use" do
13
+ file = HostsFile::File.new("")
14
+ file.should be_kind_of(HostsFile::File)
15
+ end
16
+
17
+ it "should handle file" do
18
+ test_file = @dir.file("hosts", "1.1.1.1 hostname")
19
+ file = HostsFile::File.new(test_file)
20
+ file.hosts.length.should eql(1)
21
+ end
22
+ end
@@ -0,0 +1,4 @@
1
+ require 'hosts_file/host'
2
+
3
+ describe HostsFile::Host do
4
+ end
@@ -0,0 +1,18 @@
1
+ require 'hosts_file/parser'
2
+
3
+ describe HostsFile::Parser do
4
+ it "should parse" do
5
+ @parser = HostsFile::Parser.new("1.1.1.1 hostname\n2.2.2.2 hostname2")
6
+ @parser.collect {|h| h}.length.should eql(2)
7
+ end
8
+
9
+ it "should not parse comments" do
10
+ @parser = HostsFile::Parser.new("#1.1.1.1 hostname")
11
+ @parser.collect {|h| h}.length.should eql(0)
12
+ end
13
+
14
+ it "should not parse invalid lines" do
15
+ @parser = HostsFile::Parser.new("1.1.1.1")
16
+ @parser.collect {|h| h}.length.should eql(0)
17
+ end
18
+ end
@@ -0,0 +1,9 @@
1
+ require 'spec_helper'
2
+ require 'hosts_file'
3
+
4
+ describe HostsFile do
5
+ it "should work" do
6
+ hosts = HostsFile.load("")
7
+ hosts.should be_kind_of(HostsFile::File)
8
+ end
9
+ end
@@ -0,0 +1,40 @@
1
+ require 'tmpdir'
2
+
3
+ class TempDir
4
+ attr_accessor :path
5
+
6
+ def initialize(root, subpath)
7
+ @path = File.join(root, subpath)
8
+ Dir.mkdir(@path)
9
+ end
10
+
11
+ def tree
12
+ #`cd #{@path}; tree -pugAD`
13
+ #`cd #{@path}; find . -printf "%A@ %p\n"`
14
+ `cd #{@path}; find . -printf "%p\n"`
15
+ end
16
+
17
+ def mkdir(path)
18
+ Dir.mkdir(File.join(@path, path))
19
+ end
20
+
21
+ def file(path, contents)
22
+ filename = File.join(@path, path)
23
+ File.open(filename, 'w') {|f| f.write(contents)}
24
+ filename
25
+ end
26
+
27
+ def eql? other
28
+ tree == other.tree
29
+ end
30
+
31
+ def to_s
32
+ tree
33
+ end
34
+
35
+ def self.create(&block)
36
+ Dir.mktmpdir do |dir|
37
+ yield new(dir, "test")
38
+ end
39
+ end
40
+ end
metadata ADDED
@@ -0,0 +1,129 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hosts_file
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Joshua Bussdieker
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2013-07-24 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: bundler
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ~>
27
+ - !ruby/object:Gem::Version
28
+ hash: 9
29
+ segments:
30
+ - 1
31
+ - 3
32
+ version: "1.3"
33
+ type: :development
34
+ version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: rake
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ hash: 3
44
+ segments:
45
+ - 0
46
+ version: "0"
47
+ type: :development
48
+ version_requirements: *id002
49
+ - !ruby/object:Gem::Dependency
50
+ name: rspec
51
+ prerelease: false
52
+ requirement: &id003 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ hash: 3
58
+ segments:
59
+ - 0
60
+ version: "0"
61
+ type: :development
62
+ version_requirements: *id003
63
+ description:
64
+ email:
65
+ - jbussdieker@gmail.com
66
+ executables: []
67
+
68
+ extensions: []
69
+
70
+ extra_rdoc_files: []
71
+
72
+ files:
73
+ - .gitignore
74
+ - .travis.yml
75
+ - Gemfile
76
+ - LICENSE.txt
77
+ - README.md
78
+ - Rakefile
79
+ - hosts_file.gemspec
80
+ - lib/hosts_file.rb
81
+ - lib/hosts_file/file.rb
82
+ - lib/hosts_file/host.rb
83
+ - lib/hosts_file/parser.rb
84
+ - lib/hosts_file/version.rb
85
+ - spec/hosts_file/file_spec.rb
86
+ - spec/hosts_file/host_spec.rb
87
+ - spec/hosts_file/parser_spec.rb
88
+ - spec/hosts_file_spec.rb
89
+ - spec/spec_helper.rb
90
+ homepage: http://github.com/jbussdieker/ruby-hosts_file
91
+ licenses:
92
+ - MIT
93
+ post_install_message:
94
+ rdoc_options: []
95
+
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ none: false
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ hash: 3
104
+ segments:
105
+ - 0
106
+ version: "0"
107
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
+ none: false
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ hash: 3
113
+ segments:
114
+ - 0
115
+ version: "0"
116
+ requirements: []
117
+
118
+ rubyforge_project:
119
+ rubygems_version: 1.8.15
120
+ signing_key:
121
+ specification_version: 3
122
+ summary: Basic library for reading hosts file entries.
123
+ test_files:
124
+ - spec/hosts_file/file_spec.rb
125
+ - spec/hosts_file/host_spec.rb
126
+ - spec/hosts_file/parser_spec.rb
127
+ - spec/hosts_file_spec.rb
128
+ - spec/spec_helper.rb
129
+ has_rdoc: