Class: Marshmallow

Inherits:
Object
  • Object
show all
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  with a password:

 bot = Marshmallow.new( :domain => 'mydomain', :ssl => true )
 bot. :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.( :url => 'aDxf3' )
  bot.say "Ponies!!"
  bot.paste "<script type='text/javascript'>\nalert('Ponies!')\n</script>"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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(options={})
  @debug  = options[:debug]
  @domain = options[:domain] || @@domain
  @ssl    = options[:ssl]
end

Instance Attribute Details

#domainObject

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

Yields:

  • (bot)


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.(: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

.versionObject



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 (options)
  options = { :method => :url, :username => 'Marshmallow' }.merge(options)
  
  @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 options[:method]
  when :url
    res = @req.post("/#{options[:url]}", "name=#{options[: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(options[:username])}&password=#{CGI.escape(options[:password])}"
    puts params if @debug
    res = @req.post("/login/", params, headers)
    @room_id = options[: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(message)
  say(message, true)
end

#say(message, paste = false) ⇒ Object



119
120
121
122
123
# File 'lib/cerberus/publisher/campfire.rb', line 119

def say(message, paste=false)
  puts "Posting #{message}" if @debug
  res = @req.post("/room/#{@room_id}/speak", "#{'paste=true&' if paste}message=#{CGI.escape(message.to_s)}", @headers)
  puts res.body if @debug
end