Skip to content

Commit

Permalink
Basic working git-to-rss + sinatra for ruby
Browse files Browse the repository at this point in the history
  • Loading branch information
scotchi committed May 20, 2010
1 parent d8010be commit 54e1a34
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 0 deletions.
34 changes: 34 additions & 0 deletions bin/gitfeed
@@ -0,0 +1,34 @@
#!/usr/bin/env ruby

require 'rubygems'
require 'gitfeed'

unless ARGV.size >= 1
warn <<END
Usage: gitfeed [options] /path/to/git/repository
Options:
--title="RSS Feed Title"
--url=http://base.url.com/
--description="RSS Feed Descrption"
--template=/path/to/haml/template/for/entries
--include-diffs=false
END
exit! 1
end

options = {}

until ARGV.size <= 1 do
kv = ARGV.shift.split('=')
key = kv.first.sub(/^--/, '').gsub('-', '_').to_sym
value = kv.last
value = false if value == 'false'
value = true if value == 'true'
options[key] = value
end

repository = ARGV.shift

Gitfeed::Server.start(repository, options)

19 changes: 19 additions & 0 deletions lib/entry.haml
@@ -0,0 +1,19 @@
%p
%strong Author:
= entry.author.name
&lt;
%a{ :href => "mailto:#{entry.author.email}" }
= entry.author.email
&gt;
%p
%strong Date:
= entry.author.date
%p
%strong Message:
= entry.message
%p
%strong SHA:
= entry.objectish
- if @include_diffs && !entry.parents.empty?
%pre
= @git.diff(entry.parents.first.objectish, entry.objectish)
52 changes: 52 additions & 0 deletions lib/gitfeed.rb
@@ -0,0 +1,52 @@
require 'rubygems'
require 'git'
require 'haml'
require 'rss/maker'
require 'sinatra'

class Gitfeed
RSS_VERSION = '2.0'

def initialize(repository_path, options = {})
@git = Git.open(repository_path)
@title = options[:title] || "Gitfeed for #{repository_path.sub(/.*\//, '')}"
@url = options[:url] || 'http://github.com/scotchi/gitfeed'
@description = options[:description] || repository_path
@template = options[:template] || File.expand_path('../entry.haml', __FILE__)
@include_diffs = options[:include_diffs].nil? ? true : options[:include_diffs]
@haml = Haml::Engine.new(File.read(@template))
end

def feed
RSS::Maker.make(RSS_VERSION) do |maker|
maker.channel.title = @title
maker.channel.link = @url
maker.channel.description = @description
maker.items.do_sort = true
@git.log(20).each do |entry|
item = maker.items.new_item
item.title = entry.message.sub(/\n.*/m, '')
item.link = @url
item.date = entry.date
item.description = description(entry)
end
end
end

class Server < Sinatra::Base
def self.start(*args)
@@gitfeed = Gitfeed.new(*args)
get '/' do
content_type('text/xml')
@@gitfeed.feed.to_s
end
self.run!
end
end

private

def description(entry)
@haml.render(self, :entry => entry)
end
end

0 comments on commit 54e1a34

Please sign in to comment.