Class: Dickburt::Bot
- Inherits:
-
Object
- Object
- Dickburt::Bot
- Defined in:
- lib/dickburt/bot.rb
Instance Attribute Summary collapse
-
#message ⇒ Object
Returns the value of attribute message.
-
#room ⇒ Object
Returns the value of attribute room.
-
#user ⇒ Object
Returns the value of attribute user.
Class Method Summary collapse
Instance Method Summary collapse
- #beerme ⇒ Object
- #commands ⇒ Object
- #commands_regex ⇒ Object
- #fuckyeah ⇒ Object
- #google_image(query = 'funny pugs', page = 0) ⇒ Object
- #hi ⇒ Object
- #image_exists?(url) ⇒ Boolean
- #imageme(query = 'funny pugs') ⇒ Object
-
#initialize(message, user, room) ⇒ Bot
constructor
A new instance of Bot.
- #parse_args ⇒ Object
- #parse_command ⇒ Object
- #whatup ⇒ Object
Constructor Details
#initialize(message, user, room) ⇒ Bot
Returns a new instance of Bot.
5 6 7 8 9 |
# File 'lib/dickburt/bot.rb', line 5 def initialize(, user, room) @message = @user = user @room = room end |
Instance Attribute Details
#message ⇒ Object
Returns the value of attribute message.
2 3 4 |
# File 'lib/dickburt/bot.rb', line 2 def @message end |
#room ⇒ Object
Returns the value of attribute room.
4 5 6 |
# File 'lib/dickburt/bot.rb', line 4 def room @room end |
#user ⇒ Object
Returns the value of attribute user.
3 4 5 |
# File 'lib/dickburt/bot.rb', line 3 def user @user end |
Class Method Details
.image_page ⇒ Object
15 16 17 |
# File 'lib/dickburt/bot.rb', line 15 def self.image_page @image_page ||= {} end |
.process(message, room, campfire) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/dickburt/bot.rb', line 19 def self.process(, room, campfire) if [:type] == 'TextMessage' && .body.match(/\bdickburt\b/i) dickburt = Dickburt::Bot.new(, room.find_user(.user_id), 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 end |
.used_images ⇒ Object
11 12 13 |
# File 'lib/dickburt/bot.rb', line 11 def self.used_images @used_images ||= {} end |
Instance Method Details
#beerme ⇒ Object
68 69 70 |
# File 'lib/dickburt/bot.rb', line 68 def beerme Dickburt::Response.new('http://f.cl.ly/items/333i290f1x2N330u303N/beerme.png', 'Upload') end |
#commands ⇒ Object
31 32 33 |
# File 'lib/dickburt/bot.rb', line 31 def commands @commands ||= %w(imageme beerme hi fuckyeah) end |
#commands_regex ⇒ Object
35 36 37 |
# File 'lib/dickburt/bot.rb', line 35 def commands_regex Regexp.union(commands.map{|c| /\b#{c}\b/}) end |
#fuckyeah ⇒ Object
55 56 57 |
# File 'lib/dickburt/bot.rb', line 55 def fuckyeah Dickburt::Response.new('FUCKYEAH BUDDIE!', 'Text') end |
#google_image(query = 'funny pugs', page = 0) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/dickburt/bot.rb', line 78 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) # 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 # Keep going to the next page of results unless we got an image or blowzed up. google_image(query, page+=1) unless @image_url or @error @image_url end |
#hi ⇒ Object
72 73 74 75 76 |
# File 'lib/dickburt/bot.rb', line 72 def hi whatup if rand(5) == 4 fuckyeah if rand(10) == 4 Dickburt::Response.new("Hi #{user.name}", 'Text') end |
#image_exists?(url) ⇒ Boolean
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/dickburt/bot.rb', line 121 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 |
#imageme(query = 'funny pugs') ⇒ Object
59 60 61 62 63 64 65 66 |
# File 'lib/dickburt/bot.rb', line 59 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 end |
#parse_args ⇒ Object
45 46 47 48 49 |
# File 'lib/dickburt/bot.rb', line 45 def parse_args reg = Regexp.union(/dickburt:?/, commands_regex) args = .body.downcase.gsub(reg,'').strip args.empty? ? nil : args end |
#parse_command ⇒ Object
39 40 41 42 43 |
# File 'lib/dickburt/bot.rb', line 39 def parse_command command = commands.detect{|c| .body.downcase.match(/\b#{c}\b/)} command = 'whatup' if [:type] == 'EnterMessage' command end |