Class: Tinder::Room

Inherits:
Object
  • Object
show all
Defined in:
lib/vendor/tinder/lib/tinder/room.rb

Overview

A campfire room

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#idObject (readonly)

Returns the value of attribute id.



4
5
6
# File 'lib/vendor/tinder/lib/tinder/room.rb', line 4

def id
  @id
end

#nameObject

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_transcriptsObject

Get the dates for the available transcripts for this room

Raises:

  • (NotImplementedError)


149
150
151
# File 'lib/vendor/tinder/lib/tinder/room.rb', line 149

def available_transcripts
  raise NotImplementedError
end

#destroyObject

Raises:

  • (NotImplementedError)


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

Returns:

  • (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_codeObject

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_urlObject

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

#leaveObject

Leave a room



19
20
21
# File 'lib/vendor/tinder/lib/tinder/room.rb', line 19

def leave
  post 'leave'
end

#listenObject

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 timestamp

    room.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.default_options[:basic_auth]
  options = {
    :host => "streaming.#{Connection::HOST}",
    :path => room_url_for(:live),
    :auth => "#{auth[:username]}:#{auth[:password]}",
    :timeout => 2
  }
  EventMachine::run do
    stream = Twitter::JSONStream.connect(options)
    stream.each_item do |message|
      message = HashWithIndifferentAccess.new(JSON.parse(message))
      message[:user] = user(message.delete(:user_id))
      message[:created_at] = Time.parse(message[:created_at])
      yield(message)
    end
  end
end

#lockObject

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(message)
  send_message(message, 'PasteMessage')
end

#ping(force = false) ⇒ Object

Raises:

  • (NotImplementedError)


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)
  send_message(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(message, options = {})
  send_message(message)
end

#topicObject

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

#unlockObject

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

#usersObject

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