Class: Tor::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/tor/config.rb

Overview

Tor configuration.

Examples:

Parsing a Tor configuration file (1)

torrc = Tor::Config.load("/etc/tor/torrc")

Parsing a Tor configuration file (2)

Tor::Config.open("/etc/tor/torrc") do |torrc|
  puts "Tor SOCKS port: #{torrc['SocksPort']}"
  puts "Tor control port: #{torrc['ControlPort']}"
  puts "Tor exit policy:"
  torrc.each('ExitPolicy') do |key, value|
    puts "  #{value}"
  end
end

See Also:

Since:

  • 0.1.2

Constant Summary collapse

CONFDIR =

Since:

  • 0.1.2

'/etc/tor'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) {|config| ... } ⇒ Config

Returns a new instance of Config.

Parameters:

  • options (Hash{Symbol => Object}) (defaults to: {})

Yields:

  • (config)

Yield Parameters:

Since:

  • 0.1.2



65
66
67
68
# File 'lib/tor/config.rb', line 65

def initialize(options = {}, &block)
  @lines, @options = [], options.dup
  block.call(self) if block_given?
end

Class Method Details

.load(filename, options = {}) ⇒ Config

Loads the configuration options from a Tor configuration file.

Parameters:

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

Returns:

Since:

  • 0.1.2



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/tor/config.rb', line 45

def self.load(filename, options = {})
  self.new(options) do |config|
    File.open(filename.to_s, 'rb') do |file|
      file.each_line do |line|
        case line = line.strip.chomp.strip
          when ''   then next # skip empty lines
          when /^#/ then next # skip comments
          else line = line.split('#').first.strip
        end
        # TODO: support for unquoting and unescaping values
        config << line.split(/\s+/, 2)
      end
    end
  end
end

.open(filename, options = {}) {|config| ... } ⇒ Config

Opens a Tor configuration file.

Parameters:

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

Yields:

  • (config)

Yield Parameters:

Returns:

Since:

  • 0.1.2



31
32
33
34
35
36
37
# File 'lib/tor/config.rb', line 31

def self.open(filename, options = {}, &block)
  if block_given?
    block.call(self.load(filename, options))
  else
    self.load(filename, options)
  end
end

Instance Method Details

#<<(kv) ⇒ Config

Appends a new configuration option.

Parameters:

  • (Array(String, String))

Returns:

Since:

  • 0.1.2



75
76
77
78
# File 'lib/tor/config.rb', line 75

def <<(kv)
  @lines << kv
  self
end

#[](key) ⇒ String

Looks up the last value of a particular configuration option.

Parameters:

  • key (String, Regexp)

Returns:

  • (String)

Since:

  • 0.1.2



85
86
87
88
# File 'lib/tor/config.rb', line 85

def [](key)
  values = each(key).map(&:last)
  values.empty? ? nil : values.last
end

#each(key = nil) {|key, value| ... } ⇒ Enumerator

Enumerates configuration options.

Parameters:

  • key (String, Regexp) (defaults to: nil)

Yields:

  • (key, value)

Yield Parameters:

  • key (String)
  • value (String)

Returns:

  • (Enumerator)

Since:

  • 0.1.2



98
99
100
101
# File 'lib/tor/config.rb', line 98

def each(key = nil, &block)
  return enum_for(:each, key) unless block_given?
  key ? @lines.find_all { |k, v| key === k }.each(&block) : @lines.each(&block)
end