Skip to content

Commit

Permalink
add github news feed parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
technoweenie committed Jan 2, 2010
1 parent 41ce429 commit d36f510
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 51 deletions.
2 changes: 1 addition & 1 deletion lib/github_twitter_server/api.rb
Expand Up @@ -29,7 +29,7 @@
if params[:auth][:password].to_s.size < 32
cacher.fetch_user_feed(params[:auth][:user])
else
[] # unimplemented news feed
cacher.fetch_news_feed(params[:auth][:user], params[:auth][:password])
end
end

Expand Down
5 changes: 5 additions & 0 deletions lib/github_twitter_server/cacher.rb
Expand Up @@ -23,5 +23,10 @@ def fetch_user_feed(user)
return [] if !user
fetch_feed("#{user}.atom")
end

def fetch_news_feed(user, token)
return [] if !user
fetch_feed("#{user}.private.atom?token=#{token}")
end
end
end
1 change: 1 addition & 0 deletions lib/github_twitter_server/cacher/feed.rb
Expand Up @@ -16,6 +16,7 @@ class << self
self.table_name = :github_feeds

def self.read(path)
path = path.gsub /\?.*/, ''
feed = first(:path => path)
if feed && Time.now - feed.created_at < @cache_threshold
feed.atom
Expand Down
40 changes: 39 additions & 1 deletion test/cacher_feed_test.rb
@@ -1,9 +1,12 @@
require File.expand_path(File.join(File.dirname(__FILE__), 'helper'))

class CacherFeedTest < FeedTestCase
before :all do
@data = feed_data(:simple)
end

describe "parsing User Feed" do
before :all do
@data = feed_data(:simple)
@expired = Cacher::Feed.create :path => 'zenhob.atom', :atom => 'expired', :created_at => (Time.now - Cacher::Feed.cache_threshold * 2)
@cached = Cacher::Feed.create :path => 'technoweenie.atom', :atom => @data, :created_at => (Time.now - Cacher::Feed.cache_threshold / 2)
end
Expand Down Expand Up @@ -36,4 +39,39 @@ class CacherFeedTest < FeedTestCase
assert_equal 1, items.size
end
end

describe "parsing News Feed" do
before :all do
@expired = Cacher::Feed.create :path => 'zenhob.private.atom', :atom => 'expired', :created_at => (Time.now - Cacher::Feed.cache_threshold * 2)
@cached = Cacher::Feed.create :path => 'technoweenie.private.atom', :atom => @data, :created_at => (Time.now - Cacher::Feed.cache_threshold / 2)
end

before do
@conn = Faraday::TestConnection.new do |stub|
stub.get('zenhob.private.atom') { [200, {}, @data] }
stub.get('towski.private.atom') { [200, {}, @data] }
end
@cacher = Cacher.new
@cacher.connection = @conn
end

it "fetches expired feed and updates atom/timestamp" do
items = @cacher.fetch_news_feed(:zenhob, 'abc')
assert_equal 1, items.size
assert_equal @data, Cacher::Feed.first(:path => 'zenhob.private.atom').atom

end

it "fetches new feed and creates cached record" do
assert_nil Cacher::Feed.first(:path => 'towski.atom')
items = @cacher.fetch_news_feed(:towski, 'abc')
assert_equal 1, items.size
assert_not_nil Cacher::Feed.first(:path => 'towski.private.atom')
end

it "retrieves cached feed" do
items = @cacher.fetch_news_feed(:technoweenie, 'abc')
assert_equal 1, items.size
end
end
end
105 changes: 56 additions & 49 deletions test/cacher_test.rb
@@ -1,56 +1,63 @@
require File.expand_path(File.join(File.dirname(__FILE__), 'helper'))

class CacherTest < FeedTestCase
describe "parsing User Feed" do
before :all do
data = feed_data(:user_feed)
conn = Faraday::TestConnection.new do |stub|
stub.get('technoweenie.atom') { [200, {}, data] }
[
[:User, :fetch_user_feed, :technoweenie, nil, 'technoweenie.atom'],
[:News, :fetch_news_feed, :technoweenie, 'abc', 'technoweenie.private.atom']
].each do |(method_title, method, user, pass, url)|
describe "parsing #{method_title} Feed" do
before :all do
data = feed_data(:user_feed)
conn = Faraday::TestConnection.new do |stub|
stub.get(url) { [200, {}, data] }
end
@cacher = Cacher.new
@cacher.connection = conn
# @cacher.fetch_user_feed(:technoweenie)
# @cacher.fetch_news_feed(:technoweenie, 'abc)
@items = @cacher.send(*[method, user, pass].compact)
end

it "fetches all feed items" do
assert_equal 7, @items.size
end

it "sets status ID" do
assert_equal '113924968', @items[0][:id]
assert_equal '113908814', @items[1][:id]
assert_equal '113907363', @items[2][:id]
assert_equal '113902146', @items[3][:id]
assert_equal '114814260', @items[4][:id]
assert_equal '114807560', @items[5][:id]
assert_equal '114806136', @items[6][:id]
end

it "sets screen name" do
assert_equal 'technoweenie', @items[0][:user][:screen_name]
assert_equal 'technoweenie', @items[1][:user][:screen_name]
assert_equal 'technoweenie', @items[2][:user][:screen_name]
assert_equal 'technoweenie', @items[3][:user][:screen_name]
assert_equal 'traviscline', @items[4][:user][:screen_name]
assert_equal 'mxcl', @items[5][:user][:screen_name]
assert_equal 'qrush', @items[6][:user][:screen_name]
end

it "sets text" do
assert_equal '@technoweenie/faraday @c_a18f575 this mess is gonna get raw, like sushi',
@items[0][:text]
assert_equal 'New branch is at technoweenie/github_twitter_server/tree/master',
@items[1][:text]
assert_equal 'New repository is at technoweenie/github_twitter_server',
@items[2][:text]
assert_equal "@technoweenie/faraday 2 commits: a18f5754 add faraday gemspec\n@bob a18f5754 add faraday gemspec",
@items[3][:text]
assert_equal '@mxcl/homebrew Updated Fourma: sip',
@items[4][:text]
assert_equal '&quot;Git Guidelines&quot; is at mxcl/homebrew/wikis/git-guidelines',
@items[5][:text]
assert_equal 'Deleted branch was at qrush/gemcutter/tree/add_gravatars',
@items[6][:text]
end
@cacher = Cacher.new
@cacher.connection = conn
@items = @cacher.fetch_user_feed(:technoweenie)
end

it "fetches all feed items" do
assert_equal 7, @items.size
end

it "sets status ID" do
assert_equal '113924968', @items[0][:id]
assert_equal '113908814', @items[1][:id]
assert_equal '113907363', @items[2][:id]
assert_equal '113902146', @items[3][:id]
assert_equal '114814260', @items[4][:id]
assert_equal '114807560', @items[5][:id]
assert_equal '114806136', @items[6][:id]
end

it "sets screen name" do
assert_equal 'technoweenie', @items[0][:user][:screen_name]
assert_equal 'technoweenie', @items[1][:user][:screen_name]
assert_equal 'technoweenie', @items[2][:user][:screen_name]
assert_equal 'technoweenie', @items[3][:user][:screen_name]
assert_equal 'traviscline', @items[4][:user][:screen_name]
assert_equal 'mxcl', @items[5][:user][:screen_name]
assert_equal 'qrush', @items[6][:user][:screen_name]
end

it "sets text" do
assert_equal '@technoweenie/faraday @c_a18f575 this mess is gonna get raw, like sushi',
@items[0][:text]
assert_equal 'New branch is at technoweenie/github_twitter_server/tree/master',
@items[1][:text]
assert_equal 'New repository is at technoweenie/github_twitter_server',
@items[2][:text]
assert_equal "@technoweenie/faraday 2 commits: a18f5754 add faraday gemspec\n@bob a18f5754 add faraday gemspec",
@items[3][:text]
assert_equal '@mxcl/homebrew Updated Fourma: sip',
@items[4][:text]
assert_equal '&quot;Git Guidelines&quot; is at mxcl/homebrew/wikis/git-guidelines',
@items[5][:text]
assert_equal 'Deleted branch was at qrush/gemcutter/tree/add_gravatars',
@items[6][:text]
end
end
end
4 changes: 4 additions & 0 deletions test/helper.rb
@@ -1,6 +1,10 @@
require 'rubygems'
require 'context'

if ENV['LEFTRIGHT']
require 'leftright'
end

$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
$LOAD_PATH.unshift(File.dirname(__FILE__))

Expand Down

0 comments on commit d36f510

Please sign in to comment.