Class: I2P::Hosts

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/i2p/hosts.rb

Overview

I2P address book parser.

Examples:

Opening the default hosts.txt file

I2P::Hosts.open do |hosts|
  ...
end

Opening the given hosts.txt file

I2P::Hosts.open("/path/to/hosts.txt") do |hosts|
  ...
end

Since:

Constant Summary

DEFAULT_FILE =

Unix only

'~/.i2p/hosts.txt'

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Hosts) initialize(filename = DEFAULT_FILE, options = {}) {|hosts| ... }

A new instance of Hosts

Parameters:

  • (String, #to_s) filename (defaults to: DEFAULT_FILE)
  • (Hash{Symbol => Object}) options (defaults to: {})

Yields:

  • (hosts)

Yield Parameters:



70
71
72
73
74
75
# File 'lib/i2p/hosts.rb', line 70

def initialize(filename = DEFAULT_FILE, options = {}, &block)
  @cache    = {}
  @filename = File.expand_path(filename.to_s)
  @options  = options.dup
  block.call(self) if block_given?
end

Instance Attribute Details

- (Hash) cache (readonly)

Returns:

  • (Hash)


60
61
62
# File 'lib/i2p/hosts.rb', line 60

def cache
  @cache
end

- (String) filename (readonly)

Returns:

  • (String)


63
64
65
# File 'lib/i2p/hosts.rb', line 63

def filename
  @filename
end

- (Hash) options (readonly)

Returns:

  • (Hash)


57
58
59
# File 'lib/i2p/hosts.rb', line 57

def options
  @options
end

Class Method Details

+ (Destination) [](hostname)

Looks up the I2P destination for hostname.

Examples:

I2P::Hosts["forum.i2p"]

Parameters:

  • (String, #to_s) hostname

Returns:



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.

Examples:

Opening the default hosts.txt file

I2P::Hosts.open do |hosts|
  ...
end

Opening the given hosts.txt file

I2P::Hosts.open("/path/to/hosts.txt") do |hosts|
  ...
end

Parameters:

  • (String, #to_s) filename (defaults to: DEFAULT_FILE)
  • (Hash{Symbol => Object}) options (defaults to: {})

Yields:

  • (hosts)

Yield Parameters:

Returns:



51
52
53
54
# File 'lib/i2p/hosts.rb', line 51

def self.open(filename = DEFAULT_FILE, options = {}, &block)
  hosts = self.new(filename, options)
  block_given? ? block.call(hosts) : hosts
end

Instance Method Details

- (Destination) [](hostname)

Returns the I2P destination for hostname.

Examples:

hosts["forum.i2p"]

Parameters:

  • (String, #to_s) hostname

Returns:



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.

Examples:

hosts.count

Returns:

  • (Integer)


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.

Examples:

hosts.each do |hostname, destination|
  ...
end

Yields:

  • (hostname, destination)

Yield Parameters:

Returns:

  • (Enumerator)


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.

Yields:

  • (line)

Yield Parameters:

  • (String) line

Returns:

  • (Enumerator)


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.

Examples:

hosts.empty?

Returns:

  • (Boolean)


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.

Examples:

hosts.include?("forum.i2p")

Parameters:

Returns:

  • (Boolean)


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.

Parameters:

  • (String)

Returns:

  • (Array(String, String))


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.

Returns:

  • (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.

Returns:

  • (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.

Returns:

  • (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