Class: Marshmallow
- Inherits:
-
Object
- Object
- Marshmallow
- Defined in:
- lib/cerberus/publisher/campfire.rb
Overview
Marshmallow, the campfire chatbot
You need to know one the following:
(a) the secret public URL, or
(b) an account/password for your room and the room number.
Usage:
to login with a password:
bot = Marshmallow.new( :domain => 'mydomain', :ssl => true )
bot.login :method => :login,
:username => "[email protected]",
:password => "passw0rd",
:room => "11234"
bot.say("So many ponies in here! I want one!")
to use the public url:
Marshmallow.domain = 'mydomain'
bot = Marshmallow.new
bot.login( :url => 'aDxf3' )
bot.say "Ponies!!"
bot.paste "<script type='text/javascript'>\nalert('Ponies!')\n</script>"
Instance Attribute Summary collapse
-
#domain ⇒ Object
Returns the value of attribute domain.
Class Method Summary collapse
- .connect(to) {|bot| ... } ⇒ Object
- .paste(to, what) ⇒ Object
- .say(to, what) ⇒ Object
- .version ⇒ Object
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Marshmallow
constructor
A new instance of Marshmallow.
- #login(options) ⇒ Object
- #paste(message) ⇒ Object
- #say(message, paste = false) ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ Marshmallow
Returns a new instance of Marshmallow.
75 76 77 78 79 |
# File 'lib/cerberus/publisher/campfire.rb', line 75 def initialize(={}) @debug = [:debug] @domain = [:domain] || @@domain @ssl = [:ssl] end |
Instance Attribute Details
#domain ⇒ Object
Returns the value of attribute domain.
51 52 53 |
# File 'lib/cerberus/publisher/campfire.rb', line 51 def domain @domain end |
Class Method Details
.connect(to) {|bot| ... } ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/cerberus/publisher/campfire.rb', line 62 def self.connect(to) if to =~ %r{(http|https)://([^:]+):(.+)@([^.]+).campfirenow.com/room/(\d+)} protocol, username, password, domain, room = $1, $2, $3, $4, $5 else raise "#{to} didn't match format, try something like https://david:[email protected]/room/11234" end bot = new(:domain => domain, :ssl => (protocol == "https")) bot.login(:username => username, :password => password, :method => :login, :room => room) yield bot end |
.paste(to, what) ⇒ Object
57 58 59 |
# File 'lib/cerberus/publisher/campfire.rb', line 57 def self.paste(to, what) connect(to) { |bot| bot.paste(what) } end |
.say(to, what) ⇒ Object
53 54 55 |
# File 'lib/cerberus/publisher/campfire.rb', line 53 def self.say(to, what) connect(to) { |bot| bot.say(what) } end |
.version ⇒ Object
47 48 49 |
# File 'lib/cerberus/publisher/campfire.rb', line 47 def self.version "0.2.5" end |
Instance Method Details
#login(options) ⇒ Object
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 |
# File 'lib/cerberus/publisher/campfire.rb', line 81 def login() = { :method => :url, :username => 'Marshmallow' }.merge() @req = Net::HTTP::new("#{@domain}.campfirenow.com", @ssl ? 443 : 80) @req.use_ssl = @ssl @req.verify_mode = OpenSSL::SSL::VERIFY_NONE if @ssl headers = { 'Content-Type' => 'application/x-www-form-urlencoded' } case [:method] when :url res = @req.post("/#{[:url]}", "name=#{[:username]}", headers) # parse our response headers for the room number.. magic! @room_id = res['location'].scan(/room\/(\d+)/).to_s puts res.body if @debug when :login params = "email_address=#{CGI.escape([:username])}&password=#{CGI.escape([:password])}" puts params if @debug res = @req.post("/login/", params, headers) @room_id = [:room] puts "Logging into room #{@room_id}" if @debug puts res.body if @debug end @headers = { 'Cookie' => res.response['set-cookie'] } res2 = @req.get(res['location'], @headers) puts res2.body if @debug # refresh our headers @headers = { 'Cookie' => res.response['set-cookie'] } @req.get("/room/#{@room_id}/") # join the room if necessary return @headers end |
#paste(message) ⇒ Object
115 116 117 |
# File 'lib/cerberus/publisher/campfire.rb', line 115 def paste() say(, true) end |
#say(message, paste = false) ⇒ Object
119 120 121 122 123 |
# File 'lib/cerberus/publisher/campfire.rb', line 119 def say(, paste=false) puts "Posting #{}" if @debug res = @req.post("/room/#{@room_id}/speak", "#{'paste=true&' if paste}message=#{CGI.escape(.to_s)}", @headers) puts res.body if @debug end |