Skip to content

Commit

Permalink
add feed/entry/title parsing, and project parsing based on the entry …
Browse files Browse the repository at this point in the history
…event type
  • Loading branch information
technoweenie committed Dec 23, 2009
1 parent cb44da9 commit 2468492
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 46 deletions.
73 changes: 73 additions & 0 deletions lib/github_twitter_server/events.rb
@@ -0,0 +1,73 @@
module GithubTwitterServer
# these modules extend Feed instances for custom parsing
module Events
module GenericEvent
def project
end

def content
@content ||= parsed_content
end
end

module CommitCommentEvent
def project
if title =~ /\w+ commented on (.*)$/
$1
else
nil
end
end

# Comment in a18f575: this mess is gonna get raw, like sushi =>
# @c_a18f575 this mess is gonna get raw, like sushi
def content
@content ||= begin
# pull out commit hash and comment
if parsed_content =~ /(\w+)\: (.+)$/
"@c_#{$1} #{$2}"
else
parsed_content
end
end
end
end

module PushEvent
def project
if title =~ /\w+ pushed to \w+ at (.*)$/
$1
else
nil
end
end

# put each commit on a line
# @link users
# @link commits
def content
@content ||= begin
parsed_content.dup.tap do |text|
# parse out "HEAD IS (sha)"
text.gsub! /^HEAD is \w+ /, ''
# [['technoweenie', 'sha1'], ['technoweenie', 'sha2']]
commits = text.scan(/(\w+) committed (\w+):/)
msgs = text.split(/\w+ committed \w+: /)
msgs.shift
s = []
commits.each_with_index do |(user, sha), idx|
s << "#{"@#{user} " if user != author}#{sha} #{msgs[idx]}".strip
end
s = s * "\n"
text.replace \
case commits.size
when 1 then s
when 0 then ''
else "#{commits.size} commits: #{s}"
end
end
end
end
end
end
end
63 changes: 18 additions & 45 deletions lib/github_twitter_server/feed.rb
@@ -1,5 +1,6 @@
require 'time'
require 'sax-machine'
require 'github_twitter_server/events'

module GithubTwitterServer
class Feed
Expand Down Expand Up @@ -28,6 +29,7 @@ class AtomEntry
include SAXMachine
element :id, :as => :guid
element :updated
element :title
element :author, :as => :author_name
element :content, :as => :raw_content
element :link, :value => :href, :with => {:type => "text/html", :rel => 'alternate'}
Expand All @@ -40,24 +42,14 @@ def twitter_user
{:screen_name => author}
end

def parsed_content
@parsed_content ||= begin
raw_content.gsub! /<(.|\n)+?>/, ''
raw_content.gsub! /\s+/, ' '
raw_content.strip!
raw_content
end
def content
extend_for_event_type
content
end

def content
@content ||= case event_type
when 'CommitCommentEvent'
parse_comment_event(parsed_content)
when 'PushEvent'
parse_push_event(parsed_content)
else
parsed_content
end
def project
extend_for_event_type
project
end

def author
Expand All @@ -79,38 +71,19 @@ def updated_at
@updated_at ||= Time.parse(updated)
end

# Comment in a18f575: this mess is gonna get raw, like sushi =>
# @c_a18f575 this mess is gonna get raw, like sushi
def parse_comment_event(text)
# pull out commit hash and comment
if text =~ /(\w+)\: (.+)$/
"@c_#{$1} #{$2}"
else
text
def parsed_content
@parsed_content ||= begin
raw_content.gsub! /<(.|\n)+?>/, ''
raw_content.gsub! /\s+/, ' '
raw_content.strip!
raw_content
end
end

# put each commit on a line
# @link users
# @link commits
def parse_push_event(text)
text = text.dup
# parse out "HEAD IS (sha)"
text.gsub! /^HEAD is \w+ /, ''
# [['technoweenie', 'sha1'], ['technoweenie', 'sha2']]
commits = text.scan(/(\w+) committed (\w+):/)
msgs = text.split(/\w+ committed \w+: /)
msgs.shift
s = []
commits.each_with_index do |(user, sha), idx|
s << "#{"@#{user} " if user != author}#{sha} #{msgs[idx]}".strip
end
s = s * "\n"
case commits.size
when 1 then s
when 0 then ''
else "#{commits.size} commits: #{s}"
end
def extend_for_event_type
extend Events.const_defined?(event_type) ?
Events.const_get(event_type) :
Events::GenericEvent
end
end

Expand Down
18 changes: 17 additions & 1 deletion test/feeds/feed_test.rb
@@ -1,7 +1,7 @@
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'helper'))

class FeedTest < FeedTestCase
describe "atom parsing" do
describe "parsing User Feed" do
before :all do
@data = feed_data(:user_feed)
@conn = Faraday::TestConnection.new do |stub|
Expand Down Expand Up @@ -35,6 +35,22 @@ class FeedTest < FeedTestCase
assert_equal "2 commits: #{commit}\n@bob #{commit}", @feed.entries[3].content
end

it "parses feed/entry/title" do
assert_equal 'technoweenie commented on technoweenie/faraday', @feed.entries[0].title
end

it "parses project name from feed/entry/title for CommitCommentEvent" do
assert_equal 'technoweenie/faraday', @feed.entries[0].project
end

it "parses project name from feed/entry/title for CreateEvent" do
assert_nil @feed.entries[1].project
end

it "parses project name from feed/entry/title for PushEvent" do
assert_equal 'technoweenie/faraday', @feed.entries[3].project
end

it "parses event_types" do
assert_equal 'CommitCommentEvent', @feed.entries[0].event_type
assert_equal 'CreateEvent', @feed.entries[1].event_type
Expand Down

0 comments on commit 2468492

Please sign in to comment.