Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Convert Plugins to Instances
* Refactor to instantiate plugins instead of invoking class methods.  *
Add HasConfig mixing for Plugins and Connections.
  • Loading branch information
John Wulff committed Sep 6, 2011
1 parent 6bdc6b4 commit 8232cce
Show file tree
Hide file tree
Showing 31 changed files with 395 additions and 283 deletions.
24 changes: 15 additions & 9 deletions lib/moneypenny.rb
Expand Up @@ -4,18 +4,24 @@
$LOAD_PATH.unshift dir unless $LOAD_PATH.include?(dir)

require 'moneypenny/moneypenny'
require 'moneypenny/responder'
require 'moneypenny/listener'

require 'moneypenny/mixins/has_config'

require 'moneypenny/connection'
require 'moneypenny/connections/campfire'
require 'moneypenny/connections/echo'

require 'moneypenny/plugin'
require 'moneypenny/plugins/listener'
require 'moneypenny/plugins/responder'

#responders
require 'moneypenny/responders/weather'
require 'moneypenny/responders/wikipedia'
require 'moneypenny/responders/image'
require 'moneypenny/responders/help'
require 'moneypenny/responders/define'
require 'moneypenny/responders/version'
require 'moneypenny/plugins/responders/weather'
require 'moneypenny/plugins/responders/wikipedia'
require 'moneypenny/plugins/responders/image'
require 'moneypenny/plugins/responders/help'
require 'moneypenny/plugins/responders/define'
require 'moneypenny/plugins/responders/version'

#listeners
require 'moneypenny/listeners/thats_what_she_said'
require 'moneypenny/plugins/listeners/thats_what_she_said'
13 changes: 13 additions & 0 deletions lib/moneypenny/connection.rb
@@ -0,0 +1,13 @@
module Moneypenny
module Connections
class Connection
include HasConfig

attr_reader :moneypenny

def initialize(moneypenny)
@moneypenny = moneypenny
end
end
end
end
22 changes: 11 additions & 11 deletions lib/moneypenny/connections/campfire.rb
Expand Up @@ -2,36 +2,36 @@

module Moneypenny
module Connections
class Campfire
def initialize(subdomain, room_name, api_token)
@subdomain = subdomain
@room_name = room_name
@api_token = api_token
class Campfire < Connection
def default_config
{ 'subdomain' => nil,
'room_name' => nil,
'api_token' => nil }
end

def room
unless @room
campfire = Tinder::Campfire.new @subdomain, :token => @api_token
campfire = Tinder::Campfire.new config['subdomain'], :token => config['api_token']
@id = campfire.me['id']
@room = campfire.find_room_by_name @room_name
@room = campfire.find_room_by_name config['room_name']
raise 'Unknown Room' unless @room
end
@room
end

def reconnect
@room = nil
room
end

def say(message)
if message.include?("\n")
room.paste message
else
room.speak message
end
end

def listen(&block)
begin
room.listen do |message|
Expand Down
4 changes: 2 additions & 2 deletions lib/moneypenny/connections/echo.rb
@@ -1,10 +1,10 @@
module Moneypenny
module Connections
class Echo
class Echo < Connection
def say(message)
@listener.call message
end

def listen(&block)
@listener = block
end
Expand Down
13 changes: 0 additions & 13 deletions lib/moneypenny/listener.rb

This file was deleted.

11 changes: 0 additions & 11 deletions lib/moneypenny/listeners/thats_what_she_said.rb

This file was deleted.

16 changes: 16 additions & 0 deletions lib/moneypenny/mixins/has_config.rb
@@ -0,0 +1,16 @@
module Moneypenny
module HasConfig
def default_config
{}
end

def config
config_subset = moneypenny.config
self.class.to_s.split('::')[1..-1].each do |x|
config_subset[x] ||= {}
config_subset = config_subset[x]
end
default_config.merge config_subset
end
end
end
22 changes: 12 additions & 10 deletions lib/moneypenny/moneypenny.rb
Expand Up @@ -3,19 +3,21 @@ module Moneypenny
VERSION = (git_sha = `cd #{ROOT_PATH} && git rev-parse HEAD`.strip) == '' ? "v#{File.read(File.join(ROOT_PATH, 'VERSION')).strip}" : git_sha

class Moneypenny
attr_accessor :logger
attr_accessor :config, :logger, :listeners, :responders

def initialize(config, logger)
@config = config
@logger = logger
@config = config
@logger = logger
@responders = Plugins::Responders::Responder.all.collect do |responder_class|
responder_class.new self
end
@listeners = Plugins::Listeners::Listener.all.collect do |listeners_class|
listeners_class.new self
end
end

def connection
@connection ||= Connections::Campfire.new(
@config[:campfire][:subdomain],
@config[:campfire][:room],
@config[:campfire][:api_token]
)
@connection ||= Connections::Campfire.new self
end

def listen!
Expand All @@ -42,7 +44,7 @@ def hear(message)
logger.debug "Heard: #{message}"
if matched_message = matching_message(message)
responded = false
Responder.all.each do |responder|
responders.each do |responder|
response = responder.respond matched_message
if response
say response
Expand All @@ -53,7 +55,7 @@ def hear(message)
apologize
end
else
Listener.all.each do |listener|
listeners.each do |listener|
if response = listener.respond( message )
say response
end
Expand Down
11 changes: 11 additions & 0 deletions lib/moneypenny/plugin.rb
@@ -0,0 +1,11 @@
module Moneypenny
class Plugin
include HasConfig

attr_reader :moneypenny

def initialize(moneypenny)
@moneypenny = moneypenny
end
end
end
18 changes: 18 additions & 0 deletions lib/moneypenny/plugins/listener.rb
@@ -0,0 +1,18 @@
module Moneypenny
module Plugins
module Listeners
class Listener < Plugin
@registered_listeners = []

def self.inherited(subclass)
@registered_listeners << subclass
end

def self.all
@registered_listeners
end
end
end
end
end

26 changes: 26 additions & 0 deletions lib/moneypenny/plugins/listeners/thats_what_she_said.rb
@@ -0,0 +1,26 @@
require 'twss'

module Moneypenny
module Plugins
module Listeners
class ThatsWhatSheSaid < Listener
def initialize(moneypenny)
super
TWSS.threshold = config['threshold']
end

def default_config
{ 'threshold' => 5.0 }
end

def respond(message)
if TWSS(message)
"That's what she said!"
else
false
end
end
end
end
end
end
18 changes: 18 additions & 0 deletions lib/moneypenny/plugins/responder.rb
@@ -0,0 +1,18 @@
module Moneypenny
module Plugins
module Responders
class Responder < Plugin
@registered_responders = []

def self.inherited(subclass)
@registered_responders << subclass
end

def self.all
@registered_responders
end
end
end
end
end

50 changes: 50 additions & 0 deletions lib/moneypenny/plugins/responders/define.rb
@@ -0,0 +1,50 @@
require 'nokogiri'
require 'open-uri'
require 'cgi'

module Moneypenny
module Plugins
module Responders
class Define < Responder
def help
[ 'define space', 'returns Urban Dictionary definition for space' ]
end

def respond(message)
if (query = message.match(/\Adefine\ (.+)\z/i))
term = query[1]
definitions = definitions_for_term term
if definitions.any?
definition, url = definitions.first
"#{term} is #{definition} (#{url})"
else
"I couldn't find the definition for #{term}."
end
else
false
end
end

def url_for_term(term)
"http://www.urbandictionary.com/define.php?term=#{CGI::escape term}"
end

def data_for_term(term)
open url_for_term(term)
end

def nokogiri_for_term(term)
Nokogiri::HTML data_for_term(term)
end

def definitions_for_term(term)
elements = nokogiri_for_term(term).css 'div.definition'
url = url_for_term term
elements.collect do |element|
[ element.text, "#{url}&defid=#{element.parent['id'].split('_')[1]}" ]
end
end
end
end
end
end
31 changes: 31 additions & 0 deletions lib/moneypenny/plugins/responders/help.rb
@@ -0,0 +1,31 @@
require 'nokogiri'
require 'open-uri'
require 'cgi'

module Moneypenny
module Plugins
module Responders
class Help < Responder
def help
[ 'help', 'returns a list of available commands' ]
end

def respond(message)
if (query = message.match(/\Ahelp\z/i))
helps = []
moneypenny.responders.each do |responder|
helps << responder.help rescue nil
end
helps.sort!{ |a, b| a[0] <=> b[0] }
ljust = helps.collect{ |x| x[0].size }.max + 3
helps.collect!{ |x| x[0].to_s.ljust(ljust) + x[1].to_s }
helps.join("\n")
else
false
end
end
end
end
end
end

30 changes: 30 additions & 0 deletions lib/moneypenny/plugins/responders/image.rb
@@ -0,0 +1,30 @@
require 'json'
require 'open-uri'
require 'cgi'

module Moneypenny
module Plugins
module Responders
class Image < Responder
def help
[ 'find a kitten image', 'returns a random kitten picture from Google Image search' ]
end

def respond(message)
if (match = message.match(/\Afind\ (a|an)\ (.+)\ (image|picture|photo)\z/i))
query = match[2]
url = "https://ajax.googleapis.com/ajax/services/search/images?safe=off&rsz=8&v=1.0&q=#{CGI::escape query}"
images = JSON.parse(open(url).read)['responseData']['results'] rescue []
if images.any?
images[rand(8)]['url']
else
"I couldn't find #{match[1]} #{match[2]} image."
end
else
false
end
end
end
end
end
end

0 comments on commit 8232cce

Please sign in to comment.