Sha256: f4b5495bbbee9d757164183979a5ce29f99a071acf8e7252c8e702621280d8c9

Contents?: true

Size: 1.94 KB

Versions: 16

Compression:

Stored size: 1.94 KB

Contents

require 'enumerator'
module Adhearsion
  module VoIP
    module Asterisk
      class ConfigurationManager
        
        class << self
          def normalize_configuration(file_contents)
            # cat sip.conf | sed -e 's/\s*;.*$//g' | sed -e '/^;.*$/d' | sed -e '/^\s*$/d'
            file_contents.split(/\n+/).map do |line|
              line.sub(/;.+$/, '').strip
            end.join("\n").squeeze("\n")
          end
        end
        
        attr_reader :filename
        
        def initialize(filename)
          @filename = filename
        end
        
        def sections
          @sections ||= read_configuration
        end
        
        def [](section_name)
          result = sections.find { |(name, *rest)| section_name == name }
          result.last if result
        end
        
        def delete_section(section_name)
          sections.reject! { |(name, *rest)| section_name == name }
        end
        
        def new_section(name, properties={})
          sections << [name, properties]
        end
        
        private
        
        def read_configuration
          normalized_file = self.class.normalize_configuration execute(read_command)
          normalized_file.split(/^\[([-_\w]+)\]$/)[1..-1].enum_slice(2).map do |(name,properties)|
            [name, hash_from_properties(properties)]
          end
        end
        
        def hash_from_properties(properties)
          properties.split(/\n+/).inject({}) do |property_hash,property|
            all, name, value = *property.match(/^\s*([^=]+?)\s*=\s*(.+)\s*$/)
            next property_hash unless name && value
            property_hash[name] = value
            property_hash
          end
        end
        
        def execute(command)
          %x[command]
        end
        
        def read_command
          "cat #{filename}"
        end
        
      end
    end
  end
end

# Read a file: cat a file
# Parse a file: separate into a two dimensional hash

Version data entries

16 entries across 16 versions & 7 rubygems

Version Path
eric-adhearsion-0.7.999 lib/adhearsion/voip/asterisk/config_manager.rb
eric-adhearsion-0.8.0 lib/adhearsion/voip/asterisk/config_manager.rb
jicksta-adhearsion-0.7.999 lib/adhearsion/voip/asterisk/config_manager.rb
jicksta-adhearsion-0.8.0 lib/adhearsion/voip/asterisk/config_manager.rb
jicksta-adhearsion-0.8.2 lib/adhearsion/voip/asterisk/config_manager.rb
jicksta-adhearsion-0.8.3 lib/adhearsion/voip/asterisk/config_manager.rb
kenwiesner-adhearsioncw-0.8.3 lib/adhearsion/voip/asterisk/config_manager.rb
mtrudel-adhearsion-0.8.3 lib/adhearsion/voip/asterisk/config_manager.rb
sant0sk1-adhearsion-0.7.999 lib/adhearsion/voip/asterisk/config_manager.rb
sevenscale-adhearsion-0.7.1000 lib/adhearsion/voip/asterisk/config_manager.rb
sevenscale-adhearsion-0.7.1003 lib/adhearsion/voip/asterisk/config_manager.rb
sevenscale-adhearsion-0.8.0 lib/adhearsion/voip/asterisk/config_manager.rb
adhearsion-0.8.3 lib/adhearsion/voip/asterisk/config_manager.rb
adhearsion-0.8.2 lib/adhearsion/voip/asterisk/config_manager.rb
adhearsion-0.8.1 lib/adhearsion/voip/asterisk/config_manager.rb
adhearsion-0.8.0 lib/adhearsion/voip/asterisk/config_manager.rb