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:

  • 0.1.2

Constant Summary collapse

DEFAULT_FILE =

Unix only

Since:

  • 0.1.2

'~/.i2p/hosts.txt'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Hosts.

Parameters:

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

Yields:

  • (hosts)

Yield Parameters:

Since:

  • 0.1.2



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

#cacheHash (readonly)

Returns:

  • (Hash)

Since:

  • 0.1.2



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

def cache
  @cache
end

#filenameString (readonly)

Returns:

  • (String)

Since:

  • 0.1.2



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

def filename
  @filename
end

#optionsHash (readonly)

Returns:

  • (Hash)

Since:

  • 0.1.2



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

def options
  @options
end

Class Method Details

.[](hostname) ⇒ Destination

Looks up the I2P destination for ‘hostname`.

Examples:

I2P::Hosts["forum.i2p"]

Parameters:

  • hostname (String, #to_s)

Returns:

Since:

  • 0.1.2



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.

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:

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

Yields:

  • (hosts)

Yield Parameters:

Returns:

Since:

  • 0.1.2



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

#[](hostname) ⇒ Destination

Returns the I2P destination for ‘hostname`.

Examples:

hosts["forum.i2p"]

Parameters:

  • hostname (String, #to_s)

Returns:

Since:

  • 0.1.2



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

#countInteger

Returns the number of hostnames in ‘hosts.txt`.

Examples:

hosts.count

Returns:

  • (Integer)

Since:

  • 0.1.2



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`.

Examples:

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

Yields:

  • (hostname, destination)

Yield Parameters:

Returns:

  • (Enumerator)

Since:

  • 0.1.2



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.

Examples:

hosts.empty?

Returns:

  • (Boolean)

Since:

  • 0.1.2



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.

Examples:

hosts.include?("forum.i2p")

Parameters:

Returns:

  • (Boolean)

Since:

  • 0.1.2



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_aArray

Returns all hostname mappings as an array.

Returns:

  • (Array)

Since:

  • 0.1.2



157
158
159
# File 'lib/i2p/hosts.rb', line 157

def to_a
  each.inject([]) { |result, kv| result.push(kv) }
end

#to_hashHash

Returns all hostname mappings as a hash.

Returns:

  • (Hash)

Since:

  • 0.1.2



165
166
167
# File 'lib/i2p/hosts.rb', line 165

def to_hash
  each.inject({}) { |result, (k, v)| result.merge!(k => v) }
end

#to_sString

Returns all hostname mappings as a string.

Returns:

  • (String)

Since:

  • 0.1.2



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