Skip to content
Browse files

Restructuring, removing of useless spaces.

  • Loading branch information...
1 parent 3c3197d commit 37c64a2f8c27015f63d7378357bc24e4ec8810b5 @radar committed Nov 30, 2009
Showing with 65 additions and 37 deletions.
  1. +6 −6 README.markdown
  2. +6 −0 cleaner.rb
  3. +38 −29 lib/summer.rb
  4. +2 −2 lib/summer/handlers.rb
  5. +9 −0 spec/spec_helper.rb
  6. +4 −0 spec/summer_spec.rb
View
12 README.markdown
@@ -9,7 +9,7 @@ You need to have gemcutter.org in your sources.
Installation is as simple as:
sudo gem install summer
-
+
Or you could clone the project and run `rake install`
## Usage
@@ -18,15 +18,15 @@ To use summer, create a file like this:
require 'rubygems'
require 'summer'
-
+
class Bot < Summer::Connection
-
+
end
-
+
Bot.new("localhost")
-
+
And in the same directory create a directory called _config_ and in that put _summer.yml_ which can have the following keys:
* nick: The nickname of the bot.
* channel: A channel to join on startup.
-* channels: Channels to join on startup.
+* channels: Channels to join on startup.
View
6 cleaner.rb
@@ -0,0 +1,6 @@
+for file in Dir["**/*"]
+ if File.file?(file)
+ lines = File.readlines(file).map { |line| line.gsub(/\s+$/, "\n") }
+ File.open(file, "w+") { |f| f.write(lines.join) }
+ end
+end
View
67 lib/summer.rb
@@ -7,29 +7,38 @@
module Summer
class Connection
include Handlers
- attr_accessor :connection, :ready, :started, :config
- def initialize(server, port=6667)
-
- # Ready is set when the bot receives the end of the MOTD, or missing MOTD message
+ attr_accessor :connection, :ready, :started, :config, :server, :port
+ def initialize(server, port=6667, dry=false)
@ready = false
-
- # Started is set when +startup+ completes.
@started = false
+
+ @server = server
+ @port = port
+
+ load_config
+ connect!
- @config = HashWithIndifferentAccess.new(YAML::load_file(File.dirname($0) + "/config/summer.yml"))
-
- @connection = TCPSocket.open(server, port)
-
- @connection.puts("USER #{config[:nick]} #{config[:nick]} #{config[:nick]} #{config[:nick]}")
- @connection.puts("NICK #{config[:nick]}")
- loop do
- startup! if @ready && !@started
- parse(@connection.gets)
+ unless dry
+ loop do
+ startup! if @ready && !@started
+ parse(@connection.gets)
+ end
end
end
-
+
private
-
+
+ def load_config
+ @config = HashWithIndifferentAccess.new(YAML::load_file(File.dirname($0) + "/config/summer.yml"))
+ end
+
+ def connect!
+ @connection = TCPSocket.open(server, port)
+ response("USER #{config[:nick]} #{config[:nick]} #{config[:nick]} #{config[:nick]}")
+ response("NICK #{config[:nick]}")
+ end
+
+
# Will join channels specified in configuration.
def startup!
(@config[:channels] << @config[:channel]).compact.each do |channel|
@@ -38,18 +47,18 @@ def startup!
@started = true
try(:did_start_up)
end
-
+
# Go somewhere.
def join(channel)
response("JOIN #{channel}")
end
-
+
# Leave somewhere
def part(channel)
response("PART #{channel}")
end
-
-
+
+
# What did they say?
def parse(message)
puts "<< #{message.strip}"
@@ -75,33 +84,33 @@ def parse(message)
try(method, parse_sender(sender), channel, message) if respond_to?(method)
end
end
-
+
end
-
+
def parse_sender(sender)
nick, hostname = sender.split("!")
{ :nick => nick.gsub(/^:/, ''), :hostname => hostname }
end
-
+
# These are the raws we care about.
def raws_to_handle
["422", "376"]
end
-
+
def privmsg(message, to)
response("PRIVMSG #{to} :#{message}")
end
-
+
# Output something to the console and to the socket.
def response(message)
puts ">> #{message.strip}"
@connection.puts(message)
end
-
+
def me
config[:nick]
end
-
+
end
-
+
end
View
4 lib/summer/handlers.rb
@@ -3,8 +3,8 @@ module Handlers
def handle_422(message)
self.ready = true
end
-
+
alias_method :handle_376, :handle_422
-
+
end
end
View
9 spec/spec_helper.rb
@@ -0,0 +1,9 @@
+$LOAD_PATH.unshift(File.dirname(__FILE__))
+$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
+require 'summer2'
+require 'spec'
+require 'spec/autorun'
+
+Spec::Runner.configure do |config|
+
+end
View
4 spec/summer_spec.rb
@@ -0,0 +1,4 @@
+require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
+
+describe "Summer" do
+end

0 comments on commit 37c64a2

Please sign in to comment.
Something went wrong with that request. Please try again.