Skip to content

Commit

Permalink
Red, green, refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
qrush committed May 20, 2009
1 parent 9d7c905 commit e296308
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 52 deletions.
6 changes: 3 additions & 3 deletions app/app.rb
Expand Up @@ -19,17 +19,17 @@ class App < Sinatra::Default
end

get '/gems' do
@gems = Cutter.list_gems
@gems = Cutter.find_all
haml :gems
end

get '/gems/:gem' do
@gem = Cutter.find_gem(params[:gem])
@gem = Cutter.find(params[:gem])
haml :gem
end

post '/gems' do
spec, exists = Cutter.new(request.body).save_gem
spec, exists = Cutter.new(request.body).process
Cutter.indexer.update_index

content_type "text/plain"
Expand Down
43 changes: 27 additions & 16 deletions app/cutter.rb
@@ -1,6 +1,6 @@
module Gem
class Cutter
attr_accessor :data, :error
attr_accessor :data, :error, :spec, :exists

def initialize(data)
self.data = data
Expand All @@ -16,63 +16,74 @@ def indexer.say(message) end
indexer
end

def self.list_gems
def self.find_all
cache_path = Cutter.server_path('cache', "*.gem")
Dir[cache_path].map do |gem|
gem = File.basename(gem).split("-")
"#{gem[0..-2]} (#{gem.last.chomp(".gem")})"
end
end

def self.find_gem(gem)
def self.find(gem)
path = Cutter.server_path('specifications', gem + "*")
Specification.load Dir[path].first
end

def save_gem
def validate
temp = Tempfile.new("gem")

File.open(temp.path, 'wb') do |f|
f.write data.read
f.write self.data.read
end

if File.size(temp.path).zero?
self.error = "Empty gem cannot be processed."
return
nil
else
temp
end
end

def save(temp)
begin
spec = Format.from_file_by_path(temp.path).spec
ruby_spec = spec.to_ruby
self.spec = Format.from_file_by_path(temp.path).spec
ruby_spec = self.spec.to_ruby
rescue Exception => e
puts e
return
end

name = "#{spec.name}-#{spec.version}.gem"
name = "#{self.spec.name}-#{self.spec.version}.gem"

cache_path = Cutter.server_path('cache', name)
spec_path = Cutter.server_path('specifications', name + "spec")

exists = File.exists?(spec_path)
self.exists = File.exists?(spec_path)

FileUtils.cp temp.path, cache_path
File.open(spec_path, "w") do |f|
f.write ruby_spec
end
end

# Do the indexer's work for it.
Cutter.indexer.abbreviate spec
Cutter.indexer.sanitize spec
def index
Cutter.indexer.abbreviate self.spec
Cutter.indexer.sanitize self.spec

quick_path = Cutter.server_path("quick", "Marshal.#{Gem.marshal_version}", "#{spec.name}-#{spec.version}.gemspec.rz")
quick_path = Cutter.server_path("quick", "Marshal.#{Gem.marshal_version}", "#{self.spec.name}-#{self.spec.version}.gemspec.rz")

zipped = Gem.deflate(Marshal.dump(spec))
zipped = Gem.deflate(Marshal.dump(self.spec))
File.open(quick_path, "wb") do |f|
f.write zipped
end
end

[spec, exists]
def process
temp = validate
unless temp.nil?
save(temp)
index
end
end
end
end
51 changes: 23 additions & 28 deletions spec/api_spec.rb
Expand Up @@ -18,7 +18,7 @@

describe "on POST to /gems" do
before do
stub(Gem::Cutter).new.stub!.save_gem { @gem }
stub(Gem::Cutter).new.stub!.process { @gem }
mock(Gem::Cutter).indexer.stub!.update_index
post '/gems', {}, {'rack.input' => gem_file("test-0.0.0.gem") }
end
Expand All @@ -29,41 +29,36 @@
end
end

describe "with a saved gem" do
it "should list installed gems" do
mock(Gem::Cutter).find_all { ["test (0.0.0)"] }
get "/gems"
last_response.status.should == 200
last_response.body.should =~ /test \(0.0.0\)/
end

describe "On GET to /gems/test" do
before do
stub(Gem::Cutter).find_gem("test") { @gem }
stub(Gem::Cutter).list_gems { ["test (0.0.0)"] }
mock(Gem::Cutter).find("test") { @gem }
get "/gems/test"
end

it "should list installed gems" do
get "/gems"
it "should return information about the gem" do
last_response.body.should contain("test")
last_response.body.should contain("0.0.0")
last_response.status.should == 200
last_response.body.should =~ /test \(0.0.0\)/
end
end

describe "On GET to /gems/test" do
before do
get "/gems/test"
end

it "should return information about the gem" do
last_response.body.should contain("test")
last_response.body.should contain("0.0.0")
last_response.status.should == 200
end
describe "on POST to /gems with existing gem" do
before do
stub(Gem::Cutter).new.stub!.process { [@gem, true] }
mock(Gem::Cutter).indexer.stub!.update_index
post '/gems', {}, {'rack.input' => gem_file("test-0.0.0.gem_up") }
end

describe "on POST to /gems with existing gem" do
before do
stub(Gem::Cutter).new.stub!.save_gem { [@gem, true] }
mock(Gem::Cutter).indexer.stub!.update_index
post '/gems', {}, {'rack.input' => gem_file("test-0.0.0.gem_up") }
end

it "should alert user that gem was updated" do
last_response.body.should == "Gem 'test' version 0.0.0 updated."
last_response.status.should == 200
end
it "should alert user that gem was updated" do
last_response.body.should == "Gem 'test' version 0.0.0 updated."
last_response.status.should == 200
end
end
end
Expand Down
9 changes: 4 additions & 5 deletions spec/cutter_spec.rb
Expand Up @@ -27,7 +27,7 @@
it "should not save an empty gem" do
mock(File).size(@temp_path) { 0 }

@cutter.save_gem
@cutter.process

File.exists?(@cache_path).should be_false
File.exists?(@spec_path).should be_false
Expand All @@ -50,11 +50,10 @@
marshal = "marshal"
quick_path = Gem::Cutter.server_path("quick", "Marshal.#{Gem.marshal_version}", "#{spec.name}-#{spec.version}.gemspec.rz")

mock(Marshal).dump(spec) { marshal }
mock(Gem).deflate(marshal)
mock(Marshal).dump(spec) { mock(Gem).deflate(stub!) }
mock(File).open(quick_path, 'wb')

@cutter.save_gem
@cutter.process
end

it "should save gem and update index" do
Expand All @@ -77,7 +76,7 @@
end

it "should save gem and update index" do
@cutter.save_gem
@cutter.process
File.exists?(@cache_path).should be_true
File.exists?(@spec_path).should be_true
FileUtils.compare_file(@gem_up_file.path, @cache_path).should be_true
Expand Down

0 comments on commit e296308

Please sign in to comment.