forked from moneypenny-bot/moneypenny
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
31 changed files
with
395 additions
and
283 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module Moneypenny | ||
module Connections | ||
class Connection | ||
include HasConfig | ||
|
||
attr_reader :moneypenny | ||
|
||
def initialize(moneypenny) | ||
@moneypenny = moneypenny | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module Moneypenny | ||
class Plugin | ||
include HasConfig | ||
|
||
attr_reader :moneypenny | ||
|
||
def initialize(moneypenny) | ||
@moneypenny = moneypenny | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
Oops, something went wrong.