Skip to content

Commit

Permalink
Render an HTML and Feed template
Browse files Browse the repository at this point in the history
  • Loading branch information
alloy committed Mar 4, 2009
1 parent 3dfb16c commit 941a1c9
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 10 deletions.
25 changes: 18 additions & 7 deletions lib/jewelry_portfolio.rb
Expand Up @@ -3,7 +3,7 @@
require 'jewelry_portfolio/template'

class JewelryPortfolio
attr_reader :account, :repo, :index, :template
attr_reader :account, :repo, :index, :html_template, :feed_template

# Initializes a JewelryPortfolio instance for the specified +account+.
#
Expand All @@ -15,18 +15,20 @@ def initialize(account, repo = nil)
raise ArgumentError, "No `account' given." unless account
raise ArgumentError, "No valid `repo' given." if repo && !repo.valid?

@account = account
@repo = repo
@index = ReposIndex.new(@account, (Dir.pwd unless @repo))
@template = Template::HTML.new(File.join(@index.path, 'template.html.erb'), @index.repos)
@account = account
@repo = repo
@index = ReposIndex.new(@account, (Dir.pwd unless @repo))

@html_template = template(Template::HTML, 'template.html.erb')
@feed_template = template(Template::Feed, 'feed.xml.builder')

@index.add(@repo) if @repo
end

# Renders the index.html file.
def render!
puts "Generating html"
File.open(File.join(@index.path, 'index.html'), 'w') { |f| f << @template.render }
write_out('HTML', 'index.html', @html_template.render)
write_out('feed', 'feed.xml', @feed_template.render)
end

# Renders the index.html file, then commits and pushes it to the remote.
Expand All @@ -38,6 +40,15 @@ def release!

private

def template(klass, file)
klass.new(File.join(@index.path, file), @index.repos)
end

def write_out(message, file, data)
puts "Generating #{message}"
File.open(File.join(@index.path, file), 'w') { |f| f << data }
end

def commit_message
if @repo
"Updated github pages for: #{@repo.name}-#{@repo.version}"
Expand Down
20 changes: 17 additions & 3 deletions test/jewelry_portfolio_test.rb
Expand Up @@ -42,16 +42,28 @@
index.repos.map { |r| r.name }.should == %w{ dr-nic-magic-awesome microgem }
end

it "should return the template" do
template = @portfolio.template
it "should return the html template" do
template = @portfolio.html_template
template.should.be.instance_of JewelryPortfolio::Template::HTML
template.file.should == File.join(@portfolio.index.path, 'template.html.erb')
template.repos.should == @portfolio.index.repos.to_a
end

it "should write out the template" do
it "should return the feed template" do
template = @portfolio.feed_template
template.should.be.instance_of JewelryPortfolio::Template::Feed
template.file.should == File.join(@portfolio.index.path, 'feed.xml.builder')
template.repos.should == @portfolio.index.repos.to_a
end

it "should write out the template and feed" do
time = Time.now
Time.stubs(:now).returns(time)
expected_feed = File.read(fixture('feed.xml')).gsub('TIME_NOW', time.iso8601)

@portfolio.render!
File.read(File.join(@portfolio.index.path, 'index.html')).should == File.read(fixture('template.html'))
File.read(File.join(@portfolio.index.path, 'feed.xml')).should == expected_feed
end

it "should render, commit, and push the master branch" do
Expand All @@ -67,6 +79,8 @@
before do
JewelryPortfolio::ReposIndex.any_instance.stubs(:load_pages_repo!)
JewelryPortfolio::Template::HTML.stubs(:new)
JewelryPortfolio::Template::Feed.stubs(:new)

@portfolio = JewelryPortfolio.new('alloy')
end

Expand Down

0 comments on commit 941a1c9

Please sign in to comment.