Class: Tinder::Room
- Inherits:
-
Object
- Object
- Tinder::Room
- Defined in:
- lib/vendor/tinder/lib/tinder/room.rb
Overview
A campfire room
Instance Attribute Summary collapse
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#name ⇒ Object
Returns the value of attribute name.
Instance Method Summary collapse
-
#available_transcripts ⇒ Object
Get the dates for the available transcripts for this room.
- #destroy ⇒ Object
-
#files(count = 5) ⇒ Object
Get the list of latest files for this room.
- #guest_access_enabled? ⇒ Boolean
-
#guest_invite_code ⇒ Object
The invite code use for guest.
-
#guest_url ⇒ Object
Get the url for guest access.
-
#initialize(connection, attributes = {}) ⇒ Room
constructor
A new instance of Room.
-
#join(force = false) ⇒ Object
Join the room.
-
#leave ⇒ Object
Leave a room.
-
#listen ⇒ Object
Listen for new messages in the room, yielding them to the provided block as they arrive.
-
#lock ⇒ Object
Lock the room to prevent new users from entering and to disable logging.
- #paste(message) ⇒ Object
- #ping(force = false) ⇒ Object
- #play(sound) ⇒ Object
-
#speak(message, options = {}) ⇒ Object
Post a new message to the chat room.
-
#topic ⇒ Object
Get the current topic.
-
#topic=(topic) ⇒ Object
Change the topic.
-
#transcript(transcript_date) ⇒ Object
Get the transcript for the given date (Returns a hash in the same format as #listen).
-
#unlock ⇒ Object
Unlock the room.
- #upload(filename) ⇒ Object
-
#user(id) ⇒ Object
return the user with the given id; if it isn’t in our room cache, do a request to get it.
-
#users ⇒ Object
Get the list of users currently chatting for this room.
Constructor Details
#initialize(connection, attributes = {}) ⇒ Room
Returns a new instance of Room.
6 7 8 9 10 11 |
# File 'lib/vendor/tinder/lib/tinder/room.rb', line 6 def initialize(connection, attributes = {}) @connection = connection @id = attributes['id'] @name = attributes['name'] @loaded = false end |
Instance Attribute Details
#id ⇒ Object (readonly)
Returns the value of attribute id.
4 5 6 |
# File 'lib/vendor/tinder/lib/tinder/room.rb', line 4 def id @id end |
#name ⇒ Object
Returns the value of attribute name.
4 5 6 |
# File 'lib/vendor/tinder/lib/tinder/room.rb', line 4 def name @name end |
Instance Method Details
#available_transcripts ⇒ Object
Get the dates for the available transcripts for this room
149 150 151 |
# File 'lib/vendor/tinder/lib/tinder/room.rb', line 149 def available_transcripts raise NotImplementedError end |
#destroy ⇒ Object
70 71 72 |
# File 'lib/vendor/tinder/lib/tinder/room.rb', line 70 def destroy raise NotImplementedError end |
#files(count = 5) ⇒ Object
Get the list of latest files for this room
182 183 184 |
# File 'lib/vendor/tinder/lib/tinder/room.rb', line 182 def files(count = 5) get(:uploads)['uploads'].map { |u| u['full_url'] } end |
#guest_access_enabled? ⇒ Boolean
28 29 30 31 |
# File 'lib/vendor/tinder/lib/tinder/room.rb', line 28 def guest_access_enabled? load @open_to_guests ? true : false end |
#guest_invite_code ⇒ Object
The invite code use for guest
34 35 36 37 |
# File 'lib/vendor/tinder/lib/tinder/room.rb', line 34 def guest_invite_code load @active_token_value end |
#guest_url ⇒ Object
Get the url for guest access
24 25 26 |
# File 'lib/vendor/tinder/lib/tinder/room.rb', line 24 def guest_url "#{@connection.uri}/#{guest_invite_code}" if guest_access_enabled? end |
#join(force = false) ⇒ Object
Join the room. Pass true
to join even if you’ve already joined.
14 15 16 |
# File 'lib/vendor/tinder/lib/tinder/room.rb', line 14 def join(force = false) post 'join' end |
#leave ⇒ Object
Leave a room
19 20 21 |
# File 'lib/vendor/tinder/lib/tinder/room.rb', line 19 def leave post 'leave' end |
#listen ⇒ Object
Listen for new messages in the room, yielding them to the provided block as they arrive. Each message is a hash with:
-
:body
: the body of the message -
:user
: Campfire user, which is itself a hash, of:-
:id
: User id -
:name
: User name -
:email_address
: Email address -
:admin
: Boolean admin flag -
:created_at
: User creation timestamp -
:type
: User type (e.g. Member)
-
-
:id
: Campfire message id -
:type
: Campfire message type -
:room_id
: Campfire room id -
:created_at
: Message creation timestamproom.listen do |m|
room.speak "Go away!" if m[:body] =~ /Java/i
end
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/vendor/tinder/lib/tinder/room.rb', line 124 def listen raise "no block provided" unless block_given? require 'twitter/json_stream' join # you have to be in the room to listen auth = connection.[:basic_auth] = { :host => "streaming.#{Connection::HOST}", :path => room_url_for(:live), :auth => "#{auth[:username]}:#{auth[:password]}", :timeout => 2 } EventMachine::run do stream = Twitter::JSONStream.connect() stream.each_item do || = HashWithIndifferentAccess.new(JSON.parse()) [:user] = user(.delete(:user_id)) [:created_at] = Time.parse([:created_at]) yield() end end end |
#lock ⇒ Object
Lock the room to prevent new users from entering and to disable logging
57 58 59 |
# File 'lib/vendor/tinder/lib/tinder/room.rb', line 57 def lock post :lock end |
#paste(message) ⇒ Object
79 80 81 |
# File 'lib/vendor/tinder/lib/tinder/room.rb', line 79 def paste() (, 'PasteMessage') end |
#ping(force = false) ⇒ Object
66 67 68 |
# File 'lib/vendor/tinder/lib/tinder/room.rb', line 66 def ping(force = false) raise NotImplementedError end |
#play(sound) ⇒ Object
83 84 85 |
# File 'lib/vendor/tinder/lib/tinder/room.rb', line 83 def play(sound) (sound, 'SoundMessage') end |
#speak(message, options = {}) ⇒ Object
Post a new message to the chat room
75 76 77 |
# File 'lib/vendor/tinder/lib/tinder/room.rb', line 75 def speak(, = {}) () end |
#topic ⇒ Object
Get the current topic
51 52 53 54 |
# File 'lib/vendor/tinder/lib/tinder/room.rb', line 51 def topic load @topic end |
#topic=(topic) ⇒ Object
Change the topic
46 47 48 |
# File 'lib/vendor/tinder/lib/tinder/room.rb', line 46 def topic=(topic) connection.put("/room/#{@id}.json", :body => {:room => {:topic => topic}}.to_json) end |
#transcript(transcript_date) ⇒ Object
Get the transcript for the given date (Returns a hash in the same format as #listen)
room.transcript(room.available_transcripts.first)
#=> [{:message=>"foobar!",
:user_id=>"99999",
:person=>"Brandon",
:id=>"18659245",
:timestamp=>=>Tue May 05 07:15:00 -0700 2009}]
The timestamp slot will typically have a granularity of five minutes.
164 165 166 167 168 169 170 171 172 |
# File 'lib/vendor/tinder/lib/tinder/room.rb', line 164 def transcript(transcript_date) url = "/room/#{@id}/transcript/#{transcript_date.to_date.strftime('%Y/%m/%d')}.json" connection.get(url)['messages'].map do |room| { :id => room['id'], :user_id => room['user_id'], :message => room['body'], :timestamp => Time.parse(room['created_at']) } end end |
#unlock ⇒ Object
Unlock the room
62 63 64 |
# File 'lib/vendor/tinder/lib/tinder/room.rb', line 62 def unlock post :unlock end |
#upload(filename) ⇒ Object
174 175 176 177 178 179 |
# File 'lib/vendor/tinder/lib/tinder/room.rb', line 174 def upload(filename) File.open(filename, "rb") do |file| params = Multipart::MultipartPost.new('upload' => file) post(:uploads, :body => params.query) end end |
#user(id) ⇒ Object
return the user with the given id; if it isn’t in our room cache, do a request to get it
94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/vendor/tinder/lib/tinder/room.rb', line 94 def user(id) if id user = users.detect {|u| u[:id] == id } unless user user_data = connection.get("/users/#{id}.json") user = user_data && user_data[:user] end user[:created_at] = Time.parse(user[:created_at]) user end end |
#users ⇒ Object
Get the list of users currently chatting for this room
88 89 90 91 |
# File 'lib/vendor/tinder/lib/tinder/room.rb', line 88 def users reload! @users end |