Skip to content

Commit

Permalink
Merge pull request #14 from alex-hofsteede/master
Browse files Browse the repository at this point in the history
Fix issue with parsing of Tweets
  • Loading branch information
precipice committed Mar 9, 2013
2 parents 9e4ba07 + 9adfe55 commit f246b91
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 18 deletions.
34 changes: 17 additions & 17 deletions lib/campfire_export.rb
Expand Up @@ -144,13 +144,13 @@ def initialize(subdomain, api_token)

def find_timezone
settings = Nokogiri::XML get('/account.xml').body
selected_zone = settings.css('time-zone')
selected_zone = settings.xpath('/account/time-zone')
Account.timezone = find_tzinfo(selected_zone.text)
end

def rooms
doc = Nokogiri::XML get('/rooms.xml').body
doc.css('room').map {|room_xml| Room.new(room_xml) }
doc.xpath('/rooms/room').map {|room_xml| Room.new(room_xml) }
end
end

Expand All @@ -159,9 +159,9 @@ class Room
attr_accessor :id, :name, :created_at, :last_update

def initialize(room_xml)
@id = room_xml.css('id').text
@name = room_xml.css('name').text
created_utc = DateTime.parse(room_xml.css('created-at').text)
@id = room_xml.xpath('id').text
@name = room_xml.xpath('name').text
created_utc = DateTime.parse(room_xml.xpath('created-at').text)
@created_at = Account.timezone.utc_to_local(created_utc)
end

Expand All @@ -186,7 +186,7 @@ def export(start_date=nil, end_date=nil)
def find_last_update
begin
last_message = Nokogiri::XML get("/room/#{id}/recent.xml?limit=1").body
update_utc = DateTime.parse(last_message.css('created-at').text)
update_utc = DateTime.parse(last_message.xpath('/messages/message[1]/created-at').text)
@last_update = Account.timezone.utc_to_local(update_utc)
rescue Exception => e
log(:error,
Expand Down Expand Up @@ -217,7 +217,7 @@ def export
rescue Exception => e
log(:error, "transcript export for #{export_dir} failed", e)
else
@messages = xml.css('messages > message').map do |message|
@messages = xml.xpath('/messages/message').map do |message|
CampfireExport::Message.new(message, room, date)
end

Expand Down Expand Up @@ -301,19 +301,19 @@ class Message
attr_accessor :id, :room, :body, :type, :user, :date, :timestamp, :upload

def initialize(message, room, date)
@id = message.css('id').text
@id = message.xpath('id').text
@room = room
@date = date
@body = message.css('body').text
@type = message.css('type').text
@body = message.xpath('body').text
@type = message.xpath('type').text

time = Time.parse message.css('created-at').text
time = Time.parse message.xpath('created-at').text
localtime = CampfireExport::Account.timezone.utc_to_local(time)
@timestamp = localtime.strftime '%I:%M %p'

no_user = ['TimestampMessage', 'SystemMessage', 'AdvertisementMessage']
unless no_user.include?(@type)
@user = username(message.css('user-id').text)
@user = username(message.xpath('user-id').text)
end

@upload = CampfireExport::Upload.new(self) if is_upload?
Expand All @@ -327,7 +327,7 @@ def username(user_id)
"[unknown user]"
else
# Take the first name and last initial, if there is more than one name.
name_parts = doc.css('name').text.split
name_parts = doc.xpath('/user/name').text.split
if name_parts.length > 1
name_parts[-1] = "#{name_parts.last[0,1]}."
name_parts.join(" ")
Expand Down Expand Up @@ -427,10 +427,10 @@ def export
upload = Nokogiri::XML get(upload_path).body

# Get the upload itself and export it.
@id = upload.css('id').text
@byte_size = upload.css('byte-size').text.to_i
@content_type = upload.css('content-type').text
@filename = upload.css('name').text
@id = upload.xpath('id').text
@byte_size = upload.xpath('byte-size').text.to_i
@content_type = upload.xpath('content-type').text
@filename = upload.xpath('name').text

export_content(upload_dir)
export_content(thumb_dir, path_component="thumb/#{id}", verify=false) if is_image?
Expand Down
63 changes: 63 additions & 0 deletions spec/campfire_export/message_spec.rb
@@ -0,0 +1,63 @@
require 'campfire_export'
require 'campfire_export/timezone'

require 'nokogiri'

module CampfireExport
describe Message do
include TimeZone

before :each do
@messages = Nokogiri::XML <<XML
<messages>
<message>
<created-at type="datetime">2012-05-11T17:45:00Z</created-at>
<id type="integer">111</id>
<room-id type="integer">222</room-id>
<user-id type="integer" nil="true"/>
<body nil="true"/>
<type>TimestampMessage</type>
</message>
<message>
<created-at type="datetime">2012-05-11T17:47:20Z</created-at>
<id type="integer">333</id>
<room-id type="integer">222</room-id>
<user-id type="integer">555</user-id>
<body>This is a tweet</body>
<type>TweetMessage</type>
<tweet>
<id>20100487385931234</id>
<message>This is a tweet</message>
<author_username>twitter_user</author_username>
<author_avatar_url>avatar.jpg</author_avatar_url>
</tweet>
</message>
<message>
<created-at type="datetime">2012-05-11T17:47:23Z</created-at>
<id type="integer">666</id>
<room-id type="integer">222</room-id>
<user-id type="integer">555</user-id>
<body>Regular message</body>
<type>TextMessage</type>
</message>
</messages>
XML
Account.timezone = find_tzinfo("America/Los_Angeles")
end

context "when it is created" do
it "sets up basic properties" do
message = Message.new(@messages.xpath('/messages/message[3]')[0], nil, nil)
message.body.should == "Regular message"
message.id.should == "666"
message.timestamp.should == "10:47 AM"
end

it "handles tweets correctly" do
message = Message.new(@messages.xpath('/messages/message[2]'), nil, nil)
message.body.should == "This is a tweet"
message.id.should == "333"
end
end
end
end
3 changes: 2 additions & 1 deletion spec/campfire_export/room_spec.rb
Expand Up @@ -8,8 +8,9 @@ module CampfireExport
include TimeZone

before :each do
@room_xml = Nokogiri::XML "<room><name>Test Room</name><id>666</id>" +
doc = Nokogiri::XML "<room><name>Test Room</name><id>666</id>" +
"<created-at>2009-11-17T19:41:38Z</created-at></room>"
@room_xml = doc.xpath('/room')
Account.timezone = find_tzinfo("America/Los_Angeles")
end

Expand Down

0 comments on commit f246b91

Please sign in to comment.