Class: I2P::Hosts
Overview
I2P address book parser.
Constant Summary collapse
- DEFAULT_FILE =
Unix only
'~/.i2p/hosts.txt'
Instance Attribute Summary collapse
- #cache ⇒ Hash readonly
- #filename ⇒ String readonly
- #options ⇒ Hash readonly
Class Method Summary collapse
-
.[](hostname) ⇒ Destination
Looks up the I2P destination for ‘hostname`.
-
.open(filename = DEFAULT_FILE, options = {}) {|hosts| ... } ⇒ Hosts
Opens a ‘hosts.txt` file for reading.
Instance Method Summary collapse
-
#[](hostname) ⇒ Destination
Returns the I2P destination for ‘hostname`.
-
#count ⇒ Integer
Returns the number of hostnames in ‘hosts.txt`.
-
#each {|hostname, destination| ... } ⇒ Enumerator
Enumerates the hostnames and I2P destinations in ‘hosts.txt`.
-
#empty? ⇒ Boolean
Returns ‘true` if `hosts.txt` doesn’t contain any hostnames.
-
#include?(value) ⇒ Boolean
Returns ‘true` if `hosts.txt` includes `value`.
-
#initialize(filename = DEFAULT_FILE, options = {}) {|hosts| ... } ⇒ Hosts
constructor
A new instance of Hosts.
-
#to_a ⇒ Array
Returns all hostname mappings as an array.
-
#to_hash ⇒ Hash
Returns all hostname mappings as a hash.
-
#to_s ⇒ String
Returns all hostname mappings as a string.
Constructor Details
#initialize(filename = DEFAULT_FILE, options = {}) {|hosts| ... } ⇒ Hosts
Returns 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
#cache ⇒ Hash (readonly)
60 61 62 |
# File 'lib/i2p/hosts.rb', line 60 def cache @cache end |
#filename ⇒ String (readonly)
63 64 65 |
# File 'lib/i2p/hosts.rb', line 63 def filename @filename end |
#options ⇒ Hash (readonly)
57 58 59 |
# File 'lib/i2p/hosts.rb', line 57 def @options end |
Class Method Details
.[](hostname) ⇒ Destination
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 |
.open(filename = DEFAULT_FILE, options = {}) {|hosts| ... } ⇒ 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
#[](hostname) ⇒ Destination
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 |
#count ⇒ Integer
Returns the number of hostnames in ‘hosts.txt`.
95 96 97 |
# File 'lib/i2p/hosts.rb', line 95 def count each.count end |
#each {|hostname, destination| ... } ⇒ Enumerator
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 |
#empty? ⇒ Boolean
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 |
#include?(value) ⇒ Boolean
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 |
#to_a ⇒ Array
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 |
#to_hash ⇒ 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 |
#to_s ⇒ String
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 |