Class: I2P::Hosts
- Inherits:
-
Object
- Object
- I2P::Hosts
- Includes:
- Enumerable
- Defined in:
- lib/i2p/hosts.rb
Overview
I2P address book parser.
Constant Summary
- DEFAULT_FILE =
Unix only
'~/.i2p/hosts.txt'
Instance Attribute Summary (collapse)
- - (Hash) cache readonly
- - (String) filename readonly
- - (Hash) options readonly
Class Method Summary (collapse)
-
+ (Destination) [](hostname)
Looks up the I2P destination for
hostname. -
+ (Hosts) open(filename = DEFAULT_FILE, options = {}) {|hosts| ... }
Opens a
hosts.txtfile for reading.
Instance Method Summary (collapse)
-
- (Destination) [](hostname)
Returns the I2P destination for
hostname. -
- (Integer) count
Returns the number of hostnames in
hosts.txt. -
- (Enumerator) each {|hostname, destination| ... }
Enumerates the hostnames and I2P destinations in
hosts.txt. -
- (Enumerator) each_line {|line| ... }
protected
Enumerates each line in the
hosts.txtfile. -
- (Boolean) empty?
Returns
trueifhosts.txtdoesn't contain any hostnames. -
- (Boolean) include?(value)
Returns
trueifhosts.txtincludesvalue. -
- (Hosts) initialize(filename = DEFAULT_FILE, options = {}) {|hosts| ... }
constructor
A new instance of Hosts.
-
- (Array(String, String)) parse_line(line)
protected
Parses a hostname mapping line from
hosts.txt. -
- (Array) to_a
Returns all hostname mappings as an array.
-
- (Hash) to_hash
Returns all hostname mappings as a hash.
-
- (String) to_s
Returns all hostname mappings as a string.
Constructor Details
- (Hosts) initialize(filename = DEFAULT_FILE, options = {}) {|hosts| ... }
A new instance of Hosts
70 71 72 73 74 75 |
# File 'lib/i2p/hosts.rb', line 70 def initialize(filename = DEFAULT_FILE, = {}, &block) @cache = {} @filename = File.(filename.to_s) @options = .dup block.call(self) if block_given? end |
Instance Attribute Details
- (Hash) cache (readonly)
60 61 62 |
# File 'lib/i2p/hosts.rb', line 60 def cache @cache end |
- (String) filename (readonly)
63 64 65 |
# File 'lib/i2p/hosts.rb', line 63 def filename @filename end |
- (Hash) options (readonly)
57 58 59 |
# File 'lib/i2p/hosts.rb', line 57 def @options end |
Class Method Details
+ (Destination) [](hostname)
Looks up the I2P destination for hostname.
29 30 31 |
# File 'lib/i2p/hosts.rb', line 29 def self.[](hostname) self.open { |hosts| hosts[hostname] } end |
+ (Hosts) open(filename = DEFAULT_FILE, options = {}) {|hosts| ... }
Opens a hosts.txt file for reading.
51 52 53 54 |
# File 'lib/i2p/hosts.rb', line 51 def self.open(filename = DEFAULT_FILE, = {}, &block) hosts = self.new(filename, ) block_given? ? block.call(hosts) : hosts end |
Instance Method Details
- (Destination) [](hostname)
Returns the I2P destination for hostname.
124 125 126 127 128 129 |
# File 'lib/i2p/hosts.rb', line 124 def [](hostname) @cache[hostname.to_s] ||= each_line.find do |line| k, v = parse_line(line) break Destination.parse(v) if hostname === k end end |
- (Integer) count
Returns the number of hostnames in hosts.txt.
95 96 97 |
# File 'lib/i2p/hosts.rb', line 95 def count each.count end |
- (Enumerator) each {|hostname, destination| ... }
Enumerates the hostnames and I2P destinations in hosts.txt.
143 144 145 146 147 148 149 150 151 |
# File 'lib/i2p/hosts.rb', line 143 def each(&block) if block_given? each_line do |line| k, v = parse_line(line) block.call(k, Destination.parse(v)) end end enum_for(:each) end |
- (Enumerator) each_line {|line| ... } (protected)
Enumerates each line in the hosts.txt file.
185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/i2p/hosts.rb', line 185 def each_line(&block) if block_given? File.open(@filename, 'rb') do |file| file.each_line do |line| line, = line.split('#', 2) if line.include?(?#) line.chomp!.strip! block.call(line) unless line.empty? end end end enum_for(:each_line) end |
- (Boolean) empty?
Returns true if hosts.txt doesn't contain any hostnames.
84 85 86 |
# File 'lib/i2p/hosts.rb', line 84 def empty? count.zero? end |
- (Boolean) include?(value)
Returns true if hosts.txt includes value. The value can be
either a hostname or an I2P destination.
108 109 110 111 112 113 114 |
# File 'lib/i2p/hosts.rb', line 108 def include?(value) case value when Destination then each.any? { |k, v| value.eql?(v) } when Regexp then each.any? { |k, v| value === k } else each.any? { |k, v| value.to_s.eql?(k) } end end |
- (Array(String, String)) parse_line(line) (protected)
Parses a hostname mapping line from hosts.txt.
203 204 205 |
# File 'lib/i2p/hosts.rb', line 203 def parse_line(line) line.chomp.split('=', 2).map(&:strip) end |
- (Array) to_a
Returns all hostname mappings as an array.
157 158 159 |
# File 'lib/i2p/hosts.rb', line 157 def to_a each.inject([]) { |result, kv| result.push(kv) } end |
- (Hash) to_hash
Returns all hostname mappings as a hash.
165 166 167 |
# File 'lib/i2p/hosts.rb', line 165 def to_hash each.inject({}) { |result, (k, v)| result.merge!(k => v) } end |
- (String) to_s
Returns all hostname mappings as a string.
173 174 175 |
# File 'lib/i2p/hosts.rb', line 173 def to_s each.inject([]) { |result, (k, v)| result.push([k, v.to_base64].join('=')) }.push('').join($/) end |