From 2468492c6656944f20aa34907429bd4d5720fdc9 Mon Sep 17 00:00:00 2001 From: rick Date: Tue, 22 Dec 2009 16:53:52 -0800 Subject: [PATCH] add feed/entry/title parsing, and project parsing based on the entry event type --- lib/github_twitter_server/events.rb | 73 +++++++++++++++++++++++++++++ lib/github_twitter_server/feed.rb | 63 +++++++------------------ test/feeds/feed_test.rb | 18 ++++++- 3 files changed, 108 insertions(+), 46 deletions(-) create mode 100644 lib/github_twitter_server/events.rb diff --git a/lib/github_twitter_server/events.rb b/lib/github_twitter_server/events.rb new file mode 100644 index 0000000..db6c5b3 --- /dev/null +++ b/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 \ No newline at end of file diff --git a/lib/github_twitter_server/feed.rb b/lib/github_twitter_server/feed.rb index 1e7688a..0cd5c52 100644 --- a/lib/github_twitter_server/feed.rb +++ b/lib/github_twitter_server/feed.rb @@ -1,5 +1,6 @@ require 'time' require 'sax-machine' +require 'github_twitter_server/events' module GithubTwitterServer class Feed @@ -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'} @@ -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 @@ -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 diff --git a/test/feeds/feed_test.rb b/test/feeds/feed_test.rb index 659d96e..e68c780 100644 --- a/test/feeds/feed_test.rb +++ b/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| @@ -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