Permalink
Browse files

huge refactor

  • Loading branch information...
1 parent 2d1f47f commit f2afcb58be87c47edd3250ac1ab9a784ebb60cc0 @ubermajestix ubermajestix committed Aug 29, 2011
View
@@ -0,0 +1 @@
+script/
View
@@ -11,4 +11,8 @@ group :development do
gem "guard"
# 0.4.0.rc versions are still git-only
gem "guard-minitest", :git => "https://github.com/guard/guard-minitest.git"
+ gem "fabrication"
+ gem "ffaker", :require => "ffaker"
+ gem 'vcr'
+ gem "fakeweb"
end
View
@@ -8,7 +8,7 @@ GIT
PATH
remote: .
specs:
- dickburt (0.0.1)
+ dickburt (0.0.2)
addressable (~> 2.2.6)
eventmachine (~> 0.12.10)
json (~> 1.5.1)
@@ -23,6 +23,9 @@ GEM
specs:
addressable (2.2.6)
eventmachine (0.12.10)
+ fabrication (1.0.1)
+ fakeweb (1.3.0)
+ ffaker (1.8.0)
growl (1.0.3)
guard (0.6.2)
thor (~> 0.14.6)
@@ -42,14 +45,19 @@ GEM
eventmachine (>= 0.12.8)
http_parser.rb (~> 0.5.1)
simple_oauth (~> 0.1.4)
+ vcr (1.11.1)
PLATFORMS
ruby
DEPENDENCIES
dickburt!
+ fabrication
+ fakeweb
+ ffaker
growl
guard
guard-minitest!
minitest (~> 2.2.2)
rb-fsevent
+ vcr
View
@@ -1,6 +1,7 @@
guard 'minitest' do
# with Minitest::Spec
watch(%r|^spec/(.*)_spec\.rb|)
- watch(%r|^lib/(.*)\.rb|) { |m| "spec/#{m[1]}_spec.rb" }
- watch(%r|^spec/spec_helper\.rb|) { "spec" }
+ watch(%r|^spec/fabricators/(.*)_fabricator\.rb|) { |m| "spec/dickburt/#{m[1]}_spec.rb" }
+ watch(%r|^lib/(.*)\.rb|) { |m| "spec/#{m[1]}_spec.rb" }
+ watch(%r|^spec/spec_helper\.rb|) { "spec" }
end
View
@@ -9,13 +9,18 @@
module Dickburt
require 'dickburt/bot'
require 'dickburt/campfire'
+ require 'dickburt/logger'
require 'dickburt/message'
require 'dickburt/response'
+ require 'dickburt/retryable'
require 'dickburt/room'
require 'dickburt/server'
+ require 'dickburt/user'
+
def self.logger
@logger = Logging::Logger[self.name]
end
+
end
View
@@ -1,40 +1,136 @@
class Dickburt::Bot
- class << self
- def commands
- @commands ||= %w(imageme beerme)
- end
-
- def parse_command(text)
- commands.detect{|c| text.match(c)}
- end
+ attr_accessor :message
+ attr_accessor :user
+ attr_accessor :room
+ def initialize(message, user, room)
+ @message = message
+ @user = user
+ @room = room
+ end
+
+ def self.used_images
+ @used_images ||= {}
+ end
+
+ def self.image_page
+ @image_page ||= {}
+ end
- def imageme(query='funny pugs')
- url = google_image(query)
- Dickburt::Response.new(url, 'Upload')
+ def self.process(message, room, campfire)
+ if message[:type] == 'TextMessage' && message.body.match(/\bdickburt\b/i)
+ dickburt = Dickburt::Bot.new(message, Dickburt::User.new(message.user_id, campfire), room)
+ command = dickburt.parse_command
+ args = dickburt.parse_args
+ if command
+ response = args ? dickburt.send(command, args) : dickburt.send(command)
+ room.speak(response)
+ end
end
-
- def beerme
- Dickburt::Response.new('http://f.cl.ly/items/333i290f1x2N330u303N/beerme.png', 'Upload')
+ end
+
+ def commands
+ @commands ||= %w(imageme beerme hi fuckyeah)
+ end
+
+ def commands_regex
+ Regexp.union(commands.map{|c| /\b#{c}\b/})
+ end
+
+ def parse_command
+ command = commands.detect{|c| message.body.downcase.match(/\b#{c}\b/)}
+ command = 'whatup' if message[:type] == 'EnterMessage'
+ command
+ end
+
+ def parse_args
+ reg = Regexp.union(/dickburt:?/, commands_regex)
+ args = message.body.downcase.gsub(reg,'').strip
+ args.empty? ? nil : args
+ end
+
+ def whatup
+ Dickburt::Response.new("whatup #{user.name}", 'Text')
+ end
+
+ def fuckyeah
+ Dickburt::Response.new('FUCKYEAH BUDDIE!', 'Text')
+ end
+
+ def imageme(query='funny pugs')
+ image = google_image(query)
+ if image
+ Dickburt::Response.new(image, 'Upload')
+ else
+ Dickburt::Response.new("couldn't find any pics of #{query}", 'Text')
end
-
- def google_image(query)
- @search_server = Patron::Session.new
- @search_server.base_url = "https://ajax.googleapis.com/ajax/services/search"
- @used_images ||= []
- uri = Addressable::URI.new
- uri.query_values = {:v => "1.0", :q => query}
- response = @search_server.get("/images?#{uri.query}")
- if response.status < 400
- results = JSON.parse(response.body)["responseData"]["results"]
- results.each do |image|
+ end
+
+ def beerme
+ Dickburt::Response.new('http://f.cl.ly/items/333i290f1x2N330u303N/beerme.png', 'Upload')
+ end
+
+ def hi
+ whatup if rand(5) == 4
+ fuckyeah if rand(10) == 4
+ Dickburt::Response.new("Hi #{user.name}", 'Text')
+ end
+
+ def google_image(query='funny pugs', page=0)
+ @search_server ||= Patron::Session.new
+ @search_server.base_url = "https://ajax.googleapis.com/ajax/services/search"
+ Dickburt::Bot.used_images[query] ||= []
+ page = Dickburt::Bot.image_page[query].to_i + 1 if Dickburt::Bot.image_page[query].to_i > page
+ Dickburt::Bot.image_page[query] = page
+ uri = Addressable::URI.new
+ uri.query_values = {:v => "1.0", :q => query, :rsz=>'8', :start=>page.to_s}
+ puts "finding #{query} on page #{page}"
+ response = @search_server.get("/images?#{uri.query}")
+ if response.status < 400
+ body = JSON.parse(response.body)
+ if body["responseData"]
+ results = JSON.parse(response.body)["responseData"]["results"]
+ results.each_with_index do |image, i|
image = Map.new(image)
- unless @used_images.include?(image.imageId)
- @used_images << image.imageId
- return image.url
- end
+ # check to see if we've seen this image for this query
+ next if Dickburt::Bot.used_images[query].include?(image.imageId)
+ Dickburt::Bot.used_images[query] << image.imageId
+ # check to make sure the image doesn't 404
+ exists = image_exists?(image.url)
+ next if exists == false
+ # return the image
+ @image_url = image.url
+ break # cause we found an image
end
+ else
+ @error= true
end
+ else
+ @error = true
+ puts "="*45
+ puts response.status
+ puts response.body.inspect
+ puts "="*45
end
-
+ puts @image_url.inspect
+ puts @error.inspect
+ google_image(query, page+=1) unless @image_url or @error
+ @image_url
end
-end
+
+ def image_exists?(url)
+ begin
+ image = Patron::Session.new
+ split_url = url.split("/",4)
+ image_url = split_url.pop
+ image.base_url = split_url.join("/")
+ response = image.head("/#{image_url}")
+ response.status < 400
+ rescue Patron::ConnectionFailed, Patron::ConnectionFailed
+ puts "="*45
+ puts "DNE!"
+ puts "="*45
+ false
+ end
+ end
+
+end
View
@@ -15,6 +15,8 @@ def http
@http.headers["Content-Type"] = "application/json"
@http.username = token
@http.password = "x"
+ @http.connect_timeout = 6000
+ @http.timeout = 6000
@http
end
View
@@ -0,0 +1,7 @@
+module Dickburt
+ module Logger
+ def logger
+ Dickburt.logger
+ end
+ end
+end
View
@@ -1,21 +1,25 @@
class Dickburt::Message < Map
attr_accessor :command
+ attr_accessor :args
def initialize(json)
super(JSON.parse(json))
puts self.inspect
+ puts "="*45
self
end
def process_command
return unless @command
- Dickburt::Bot.send(@command)
+ # TODO make dickburt aware of users so he can respond to them.
+ # I really don't like this setup of not having instances of the bot.
+ # If i got an instance I could save things to it, like the user and message object
+ # Then it wouldn't be as weird to process the command here on the message object
+ @args ? Dickburt::Bot.send(@command, @args) : Dickburt::Bot.send(@command)
end
- def to_dickburt?
- @dickburt ||= self[:type] == 'TextMessage' && body.match(/^dickburt\:/i)
- @command = Dickburt::Bot.parse_command(body) if @dickburt
- @dickburt
+ def user
+
end
end
View
@@ -0,0 +1,30 @@
+module Dickburt
+ module Retryable
+ # Options:
+ # * :tries - Number of retries to perform. Defaults to 1.
+ # * :on - The Exception on which a retry will be performed. Defaults to Exception, which retries on any Exception.
+ #
+ # Example
+ # =======
+ # retryable(:tries => 1, :on => OpenURI::HTTPError) do
+ # # your code here
+ # end
+ #
+ def retryable(options = {}, &block)
+ opts = { :tries => 1, :on => Exception }.merge(options)
+
+ retry_exception, retries = opts[:on], opts[:tries]
+
+ begin
+ return yield
+ rescue retry_exception
+ puts "="*45
+ puts "#{retry_exception} raised. Retrying #{retries - 1} more times"
+ puts "="*45
+ retry if (retries -= 1) > 0
+ end
+
+ yield
+ end
+ end
+end
Oops, something went wrong.

0 comments on commit f2afcb5

Please sign in to comment.