Skip to content
This repository has been archived by the owner on Jul 18, 2023. It is now read-only.

Commit

Permalink
Parse all messages the same way as #listen
Browse files Browse the repository at this point in the history
  • Loading branch information
Ravi Gadad committed Apr 6, 2013
1 parent 3a2463f commit 9a4b4e8
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 47 deletions.
60 changes: 24 additions & 36 deletions lib/tinder/room.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# encoding: UTF-8
require 'time'

module Tinder
# A campfire room
class Room
Expand Down Expand Up @@ -109,8 +111,10 @@ def user(id)
end
end

# Listen for new messages in the room, yielding them to the provided block as they arrive.
# Each message is a hash with:
# Modifies a hash representation of a Campfire message. Expands +:user_id+
# to a full hash at +:user+, generates Timestamp from +:created_at+.
#
# Full returned hash:
# * +:body+: the body of the message
# * +:user+: Campfire user, which is itself a hash, of:
# * +:id+: User id
Expand All @@ -123,6 +127,14 @@ def user(id)
# * +:type+: Campfire message type
# * +:room_id+: Campfire room id
# * +:created_at+: Message creation timestamp
def parse_message(message)
message[:user] = user(message.delete(:user_id))
message[:created_at] = Time.parse(message[:created_at])
message
end

# Listen for new messages in the room, parsing them with #parse_message
# and then yielding them to the provided block as they arrive.
#
# room.listen do |m|
# room.speak "Go away!" if m[:body] =~ /Java/i
Expand All @@ -137,7 +149,6 @@ def listen(options = {})
require 'hashie'
require 'multi_json'
require 'twitter/json_stream'
require 'time'

auth = connection.basic_auth_settings
options = {
Expand All @@ -154,8 +165,7 @@ def listen(options = {})
Tinder.logger.info "Listening to #{@name}…"
@stream.each_item do |message|
message = Hashie::Mash.new(MultiJson.decode(message))
message[:user] = user(message.delete(:user_id))
message[:created_at] = Time.parse(message[:created_at])
message = parse_message(message)
yield(message)
end

Expand Down Expand Up @@ -184,35 +194,18 @@ def stop_listening
@stream = nil
end

# 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.
# Get the transcript for the given date (returns an array of messages parsed
# via #parse_message, see #parse_message for format of returned message)
#
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']) }
connection.get(url)['messages'].map do |message|
parse_message(message)
end
end

# Search transcripts for a specific term
#
# room.search("bobloblaw")
# #=> [{:message=>"foo!",
# :user_id=>"99999",
# :person=>"Brandon",
# :id=>"18659245",
# :timestamp=>=>Tue May 05 07:15:00 -0700 2009}]
# Search transcripts for the given term (returns an array of messages parsed
# via #parse_message, see #parse_message for format of returned message)
#
def search(term)
encoded_term = URI.encode(term)
Expand All @@ -221,11 +214,8 @@ def search(term)
message[:room_id] == id
end

room_messages.map do |room|
{ :id => room['id'],
:user_id => room['user_id'],
:message => room['body'],
:timestamp => Time.parse(room['created_at']) }
room_messages.map do |message|
parse_message(message)
end
end

Expand All @@ -250,9 +240,7 @@ def recent(options = {})
url = "#{room_url_for(:recent)}?limit=#{options[:limit]}&since_message_id=#{options[:since_message_id]}"

connection.get(url)['messages'].map do |msg|
msg[:created_at] = Time.parse(msg[:created_at])
msg[:user] = user(msg[:user_id])
msg
parse_message(msg)
end
end

Expand Down
51 changes: 40 additions & 11 deletions spec/tinder/room_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,26 @@ module EventMachine; def self.run; yield end end
end
end

it "should GET the search endpoint with the search term" do
it "should GET the search endpoint with the search term and filter by room" do
@room.stub(:id).and_return(490096)
@room.should_receive(:parse_message).exactly(2).times
@room.search("foo")
end

it "should return empty array if no messages in room" do
@room.should_receive(:parse_message).never
@room.search("foo").should be_empty
end
end

describe "transcript" do
it "should GET the transcript endpoint with the provided date" do
stub_connection(@connection) do |stub|
stub.get('/room/80749/transcript/2012/10/15.json') {[200, {}, fixture("rooms/recent.json")]}
end
@room.should_receive(:parse_message).exactly(2).times
@room.transcript(Date.parse('2012-10-15'))
end
end

describe "unlock" do
Expand Down Expand Up @@ -210,21 +227,33 @@ module EventMachine; def self.run; yield end end
stub.get('/room/80749/recent.json') {[
200, {}, fixture('rooms/recent.json')
]}
stub.get('/users/1158839.json') {[
200, {}, fixture('users/me.json')
]}
stub.get('/users/1158837.json') {[
200, {}, fixture('users/me.json')
]}
end
end

it "should get a list of parsed recent messages" do
messages = @room.recent({:limit => 1})
@room.should_receive(:parse_message).exactly(2).times
messages = @room.recent
end
end

messages.size.should equal(2)
messages.first.size.should equal(8)
messages.first[:user].size.should equal(7)
describe "parse_message" do
it "expands user and parses created_at" do
unparsed_message = {
:user_id => 123,
:body => 'An aunt is worth two nieces',
:created_at => '2012/02/14 16:21:00 +0000'
}
expected = {
:user => {
:name => 'Dr. Noodles'
},
:body => 'An aunt is worth two nieces',
:created_at => Time.parse('2012/02/14 16:21:00 +0000')
}
@room.stub(:user).with(123).and_return({ :name => 'Dr. Noodles' })

actual = @room.parse_message(unparsed_message)
actual.should == expected
end
end
end

0 comments on commit 9a4b4e8

Please sign in to comment.