Class: Firejab::Connection
- Inherits:
-
Object
- Object
- Firejab::Connection
- Defined in:
- lib/firejab/connection.rb
Instance Attribute Summary collapse
-
#be_noisy ⇒ Object
Returns the value of attribute be_noisy.
-
#campfire ⇒ Object
Returns the value of attribute campfire.
-
#campfire_domain ⇒ Object
Returns the value of attribute campfire_domain.
-
#campfire_options ⇒ Object
Returns the value of attribute campfire_options.
-
#campfire_room_id ⇒ Object
Returns the value of attribute campfire_room_id.
-
#campfire_uids ⇒ Object
Returns the value of attribute campfire_uids.
-
#jabber ⇒ Object
Returns the value of attribute jabber.
-
#jabber_users ⇒ Object
Returns the value of attribute jabber_users.
Instance Method Summary collapse
- #add_token(jabber_username, campfire_token) ⇒ Object
-
#initialize(params) ⇒ Connection
constructor
A new instance of Connection.
- #run ⇒ Object
Constructor Details
#initialize(params) ⇒ Connection
Returns a new instance of Connection.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/firejab/connection.rb', line 12 def initialize(params) self.jabber = Jabber::Simple.new(params[:jabber][:username], params[:jabber][:password]) self. = { :path => "/room/#{params[:room_id]}/live.json", :host => 'streaming.campfirenow.com', :auth => "#{params[:token]}:x" } self.campfire_room_id = params[:room_id] self.campfire_domain = params[:domain] self.be_noisy = false # Load known tokens for our users from a "database" self.jabber_users = {} self.campfire_uids = {} end |
Instance Attribute Details
#be_noisy ⇒ Object
Returns the value of attribute be_noisy.
8 9 10 |
# File 'lib/firejab/connection.rb', line 8 def be_noisy @be_noisy end |
#campfire ⇒ Object
Returns the value of attribute campfire.
9 10 11 |
# File 'lib/firejab/connection.rb', line 9 def campfire @campfire end |
#campfire_domain ⇒ Object
Returns the value of attribute campfire_domain.
9 10 11 |
# File 'lib/firejab/connection.rb', line 9 def campfire_domain @campfire_domain end |
#campfire_options ⇒ Object
Returns the value of attribute campfire_options.
9 10 11 |
# File 'lib/firejab/connection.rb', line 9 def @campfire_options end |
#campfire_room_id ⇒ Object
Returns the value of attribute campfire_room_id.
9 10 11 |
# File 'lib/firejab/connection.rb', line 9 def campfire_room_id @campfire_room_id end |
#campfire_uids ⇒ Object
Returns the value of attribute campfire_uids.
9 10 11 |
# File 'lib/firejab/connection.rb', line 9 def campfire_uids @campfire_uids end |
#jabber ⇒ Object
Returns the value of attribute jabber.
10 11 12 |
# File 'lib/firejab/connection.rb', line 10 def jabber @jabber end |
#jabber_users ⇒ Object
Returns the value of attribute jabber_users.
10 11 12 |
# File 'lib/firejab/connection.rb', line 10 def jabber_users @jabber_users end |
Instance Method Details
#add_token(jabber_username, campfire_token) ⇒ Object
29 30 31 32 |
# File 'lib/firejab/connection.rb', line 29 def add_token(jabber_username, campfire_token) self.jabber_users[jabber_username] ||= {} self.jabber_users[jabber_username][:campfire_token] = campfire_token end |
#run ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/firejab/connection.rb', line 34 def run EM.run do self.campfire = Twitter::JSONStream.connect(self.) # Tell us what to do when we receive a campfire message self.campfire.each_item do |item| puts "Received a new message from campfire: #{item}" status = Yajl::Parser.parse(item) # For ppl that are connected and(?) we have tokens for, send the msg case status["type"] when "TextMessage", "PasteMessage" (status['user_id'], status['body']) when "UploadMessage" #TODO: Retrieve full URL from `GET /room/#{id}/messages/#{upload_message_id}/upload.json` (status['user_id'], "Uploaded #{status['body']}") when "EnterMessage" (status['user_id'], "Has entered the room!") if self.be_noisy when "LeaveMessage", "KickMessage" (status['user_id'], "Has left the room.") if self.be_noisy end end self.campfire.on_error do || puts "Received a campfire error: #{}" end self.campfire.on_max_reconnects do |timeout, retries| puts "Fatal error with campfire, you'll need to restart" end EM::PeriodicTimer.new(1) do check_jabber_connection self.jabber. do || # Removes "/resource" jabber_username = .from.strip if is_valid_user(jabber_username) (jabber_username, .body) elsif .body.match(/^\w{40}$/) # We received an auth token add_token(jabber_username, .body) #TODO: Verify token and lookup_campfire_uid (jabber_username, "Heloooooo!") else # We don't know who this is, ask for their token (jabber_username, "Hi! I don't know who you are, please send me your auth token from: https://#{self.campfire_domain}/member/edit") end end self.jabber.presence_updates do |friend, presence, | # presence may be one of [:online, :unavailable, :away] # puts "Received presence update from #{friend.inspect}: #{presence.inspect} => #{message.inspect}" #TODO: Update room presence accordingly - POST /room/#{id}/[join,leave].json set_status(friend, presence) end self.jabber.new_subscriptions do |friend, presence| puts "New subscription request: #{friend.inspect}" # self.jabber.add(friend['jid']) end end end end |