Class: BitlbeeConfig::Config

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

Overview

A configuration file for BitlBee. Has exactly one user

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Config

Returns a new instance of Config.

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):



57
58
59
# File 'lib/bitlbee_config/config.rb', line 57

def initialize(options = {})
  @user = options.delete(:user)
end

Instance Attribute Details

#userObject

Returns the value of attribute user.



4
5
6
# File 'lib/bitlbee_config/config.rb', line 4

def user
  @user
end

Class Method Details

.delete_from_directory_for_user(directory, username) ⇒ Object

Deletes file for a specified user name from the given directory

Parameters:

  • directory (String)

    Directory to check for user XML files

  • username (String)

    User to delete config for



49
50
51
52
# 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) ⇒ Array<BitlbeeConfig::User>

Create a config for every user XML in the given directory

Parameters:

  • directory (String)

    Directory to load from

Returns:



11
12
13
14
15
# 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) ⇒ BitlbeeConfig::User|nil

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

Parameters:

  • directory (String)

    Directory to load from

  • username (String)

    User to load config for

Returns:



22
23
24
# 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) ⇒ BitlbeeConfig::Config

Create a config from an XML file

Parameters:

  • file_path (String)

    Path to the XML configuration file

Returns:



30
31
32
# File 'lib/bitlbee_config/config.rb', line 30

def from_file(file_path)
  from_xml(File.read(file_path))
end

.from_xml(xml) ⇒ BitlbeeConfig::Config

Returns The created configuration object.

Parameters:

  • xml (String)

    The XML to parse

Returns:



36
37
38
39
40
41
42
43
# 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

Instance Method Details

#save_to_directory(path_to_dir) ⇒ Object

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

Parameters:

  • path_to_dir (String)

    Directory to save user xml to



73
74
75
76
77
78
79
# 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_xmlObject



61
62
63
64
65
66
67
# 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