Class: Tinder::Room
- Inherits:
-
Object
- Object
- Tinder::Room
- Defined in:
- 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
-
#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(options = {}) ⇒ Object
Listen for new messages in the room, yielding them to the provided block as they arrive.
- #listening? ⇒ Boolean
-
#lock ⇒ Object
Lock the room to prevent new users from entering and to disable logging.
- #paste(message) ⇒ Object
- #play(sound) ⇒ Object
-
#speak(message, options = {}) ⇒ Object
Post a new message to the chat room.
- #stop_listening ⇒ Object
-
#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.
- #update(attrs) ⇒ Object
- #upload(file, content_type = nil, filename = nil) ⇒ 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/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/tinder/room.rb', line 4 def id @id end |
#name ⇒ Object
Returns the value of attribute name.
4 5 6 |
# File 'lib/tinder/room.rb', line 4 def name @name end |
Instance Method Details
#files(count = 5) ⇒ Object
Get the list of latest files for this room
187 188 189 |
# File 'lib/tinder/room.rb', line 187 def files(count = 5) get(:uploads)['uploads'].map { |u| u['full_url'] } end |
#guest_access_enabled? ⇒ Boolean
29 30 31 32 |
# File 'lib/tinder/room.rb', line 29 def guest_access_enabled? load @open_to_guests ? true : false end |
#guest_invite_code ⇒ Object
The invite code use for guest
35 36 37 38 |
# File 'lib/tinder/room.rb', line 35 def guest_invite_code load @active_token_value end |
#guest_url ⇒ Object
Get the url for guest access
25 26 27 |
# File 'lib/tinder/room.rb', line 25 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/tinder/room.rb', line 14 def join(force = false) post 'join' end |
#leave ⇒ Object
Leave a room
19 20 21 22 |
# File 'lib/tinder/room.rb', line 19 def leave post 'leave' stop_listening end |
#listen(options = {}) ⇒ 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
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/tinder/room.rb', line 121 def listen( = {}) raise ArgumentError, "no block provided" unless block_given? join # you have to be in the room to listen require 'twitter/json_stream' auth = connection.basic_auth_settings = { :host => "streaming.#{Connection::HOST}", :path => room_url_for(:live), :auth => "#{auth[:username]}:#{auth[:password]}", :timeout => 6, :ssl => connection.[:ssl] }.merge() 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 # if we really get disconnected raise ListenFailed.new("got disconnected from #{@name}!") if !EventMachine.reactor_running? end end |
#listening? ⇒ Boolean
149 150 151 |
# File 'lib/tinder/room.rb', line 149 def listening? @stream != nil end |
#lock ⇒ Object
Lock the room to prevent new users from entering and to disable logging
62 63 64 |
# File 'lib/tinder/room.rb', line 62 def lock post :lock end |
#paste(message) ⇒ Object
76 77 78 |
# File 'lib/tinder/room.rb', line 76 def paste() (, 'PasteMessage') end |
#play(sound) ⇒ Object
80 81 82 |
# File 'lib/tinder/room.rb', line 80 def play(sound) (sound, 'SoundMessage') end |
#speak(message, options = {}) ⇒ Object
Post a new message to the chat room
72 73 74 |
# File 'lib/tinder/room.rb', line 72 def speak(, = {}) () end |
#stop_listening ⇒ Object
153 154 155 156 157 158 |
# File 'lib/tinder/room.rb', line 153 def stop_listening return unless listening? @stream.stop @stream = nil end |
#topic ⇒ Object
Get the current topic
56 57 58 59 |
# File 'lib/tinder/room.rb', line 56 def topic load @topic end |
#topic=(topic) ⇒ Object
Change the topic
47 48 49 |
# File 'lib/tinder/room.rb', line 47 def topic=(topic) update :topic => topic 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.
171 172 173 174 175 176 177 178 179 |
# File 'lib/tinder/room.rb', line 171 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
67 68 69 |
# File 'lib/tinder/room.rb', line 67 def unlock post :unlock end |
#update(attrs) ⇒ Object
51 52 53 |
# File 'lib/tinder/room.rb', line 51 def update(attrs) connection.put("/room/#{@id}.json", {:room => attrs}) end |
#upload(file, content_type = nil, filename = nil) ⇒ Object
181 182 183 184 |
# File 'lib/tinder/room.rb', line 181 def upload(file, content_type = nil, filename = nil) content_type ||= MIME::Types.type_for(filename || file) raw_post(:uploads, { :upload => Faraday::UploadIO.new(file, content_type, filename) }) 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
91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/tinder/room.rb', line 91 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
85 86 87 88 |
# File 'lib/tinder/room.rb', line 85 def users reload! @users end |