Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: mrjordangoldstein/dudley
base: e2336835e7
...
head fork: mrjordangoldstein/dudley
compare: 3206aaabbc
Checking mergeability… Don't worry, you can still create the pull request.
  • 4 commits
  • 7 files changed
  • 0 commit comments
  • 1 contributor
View
5 .dudley.yml
@@ -0,0 +1,5 @@
+server:
+ host: 192.168.1.112
+ port: 6667
+ nick: dudley
+ realname: Mr Bot
View
21 bin/testing
@@ -2,14 +2,19 @@
require File.dirname(__FILE__) + '/../lib/dudley'
-bot = IRC.new("dudley", "192.168.1.131", "6667", "Mr Bot")
+module Dudley
+ bot = IRC.new(Config[:server][:nick],
+ Config[:server][:host],
+ Config[:server][:port],
+ Config[:server][:realname])
-IRCEvent.add_callback('endofmotd') do |event|
- bot.add_channel('#dnd')
-end
+ IRCEvent.add_callback('endofmotd') do |event|
+ bot.add_channel('#dnd')
+ end
-IRCEvent.add_callback('join') do |event|
- bot.send_message(event.channel, "Hello #{event.from}")
-end
+ IRCEvent.add_callback('join') do |event|
+ bot.send_message(event.channel, "Hello #{event.from}")
+ end
-bot.connect
+ bot.connect
+end
View
13 lib/dudley.rb
@@ -2,3 +2,16 @@
$LOAD_PATH.unshift(libdir) unless $LOAD_PATH.include?(libdir)
require 'IRC'
+require 'yaml'
+require 'dudley/config'
+require 'dudley/patches/hash'
+
+module Dudley
+ ROOTDIR = File.expand_path(File.dirname(__FILE__) + '/..')
+ SPECDIR = ROOTDIR + '/spec'
+ DATADIR = SPECDIR + '/data'
+ CONFIG_LOC = ROOTDIR + '/.dudley.yml'
+end
+
+# Load the default configuration file
+Dudley::Config.load_config
View
27 lib/dudley/config.rb
@@ -0,0 +1,27 @@
+module Dudley
+ class Config
+ def self.[] key
+ config[key]
+ end
+
+ def self.[]= key, value
+ config[key] = value
+ end
+
+ # Loads a yaml file and merges it into the existing configuration. By
+ # default, this method will use the value in Dudley::CONFIG_LOC but you can
+ # load a different config file by passing in its path as an argument.
+ def self.load_config config_path = nil
+ yaml_file = YAML.load_file(config_path || Dudley::CONFIG_LOC)
+ yaml_file.symbolize_keys!
+
+ config.merge!(yaml_file)
+ end
+
+ private
+
+ def self.config
+ @@config ||= Hash.new(nil)
+ end
+ end
+end
View
13 lib/dudley/patches/hash.rb
@@ -0,0 +1,13 @@
+class Hash
+ # Destructively symbolizes hash keys. Recursive, so if the key's value is also
+ # a hash, symbolize_keys! is also called on that hash.
+ def symbolize_keys!
+ keys.each do |key|
+ val = delete(key)
+ val.symbolize_keys! if val.is_a? Hash
+
+ self[(key.to_sym rescue key) || key] = val
+ end
+ self
+ end
+end
View
12 spec/config_spec.rb
@@ -0,0 +1,12 @@
+describe Dudley::Config do
+ it "should allow you to set config values" do
+ Dudley::Config[:test_value] = 25
+ Dudley::Config[:test_value].should == 25
+ end
+
+ it "should be able to read in config files" do
+ Dudley::Config.load_config(Dudley::DATADIR + '/sample_config.yml')
+ Dudley::Config[:server].should be_a Hash
+ Dudley::Config[:server][:host].should == "192.168.1.131"
+ end
+end
View
5 spec/data/sample_config.yml
@@ -0,0 +1,5 @@
+server:
+ host: 192.168.1.131
+ port: 6667
+ nick: dudley
+ realname: Mr Bot

No commit comments for this range

Something went wrong with that request. Please try again.