class BitlbeeConfig::Config

A configuration file for BitlBee. Has exactly one user

Attributes

user[RW]

Public Class Methods

delete_from_directory_for_user(directory, username) click to toggle source

Deletes file for a specified user name from the given directory

@param [String] directory Directory to check for user XML files @param [String] username User to delete config for

# File lib/bitlbee_config/config.rb, line 49
def delete_from_directory_for_user(directory, username)
  file_to_delete = File.join(directory, BitlbeeConfig::User.username_to_filename(username))
  File.delete(file_to_delete) if File.exist?(file_to_delete)
end
from_directory(directory) click to toggle source

Create a config for every user XML in the given directory

@param [String] directory Directory to load from @return [Array<BitlbeeConfig::User>]

# File lib/bitlbee_config/config.rb, line 11
def from_directory(directory)
  Dir.glob(File.join(directory, "*.xml")).each_with_object([]) do |path_to_file, config_collection|
    config_collection << BitlbeeConfig::Config.from_file(path_to_file)
  end
end
from_directory_for_user(directory, username) click to toggle source

Create a config for one specific user, reading the XML from a directory

@param [String] directory Directory to load from @param [String] username User to load config for @return [BitlbeeConfig::User|nil]

# File lib/bitlbee_config/config.rb, line 22
def from_directory_for_user(directory, username)
  from_file(File.join(directory, BitlbeeConfig::User.username_to_filename(username)))
end
from_file(file_path) click to toggle source

Create a config from an XML file

@param [String] file_path Path to the XML configuration file @return [BitlbeeConfig::Config] The created configuration object

# File lib/bitlbee_config/config.rb, line 30
def from_file(file_path)
  from_xml(File.read(file_path))
end
from_xml(xml) click to toggle source

@param [String] xml The XML to parse @return [BitlbeeConfig::Config] The created configuration object

# File lib/bitlbee_config/config.rb, line 36
def from_xml(xml)
  doc = Nokogiri::XML(xml)

  # Bitlbee config says there can be only one user per XML file
  user_xml = doc.xpath("//user").first

  BitlbeeConfig::Config.new(user: BitlbeeConfig::User.from_xml(user_xml))
end
new(options = {}) click to toggle source

@param [Hash] options @option options [Array<BitlbeeConfig::User>] :user User this configuration belongs to

# File lib/bitlbee_config/config.rb, line 57
def initialize(options = {})
  @user = options.delete(:user)
end

Public Instance Methods

save_to_directory(path_to_dir) click to toggle source

Saves the configuration to a specified directory User files are named <username.downcase>.xml

@param [String] path_to_dir Directory to save user xml to

# File lib/bitlbee_config/config.rb, line 73
def save_to_directory(path_to_dir)
  user_file_path = File.join(path_to_dir, "#{ @user.nick.downcase }.xml")

  File.open(user_file_path, "w") do |file|
    file.write(to_xml)
  end
end
to_xml() click to toggle source
# File lib/bitlbee_config/config.rb, line 61
def to_xml
  builder = Nokogiri::XML::Builder.new do |xml_builder|
    @user.build_xml(xml_builder)
  end

  builder.doc.root.to_xml
end