Permalink
Browse files

Now stores the code in Redis using the MD5 of the code as the key.

Performing a get request to the MD5 will show the code
  • Loading branch information...
1 parent c169ce5 commit 091163cba27a78e1da06782a74ab21ea08d4626d @cc committed Mar 10, 2012
Showing with 53 additions and 2 deletions.
  1. +2 −0 Gemfile
  2. +5 −0 Gemfile.lock
  3. +14 −0 pastr_app.rb
  4. +15 −2 spec/pastr_spec.rb
  5. +5 −0 templates/index.mustache
  6. +3 −0 templates/show.mustache
  7. +9 −0 views/show.rb
View
@@ -2,6 +2,8 @@ source 'http://rubygems.org'
gem 'sinatra'
gem 'mustache'
+gem 'shotgun'
+gem 'redis'
group :test do
gem 'rspec'
View
@@ -24,6 +24,7 @@ GEM
rack
rack-test (0.6.1)
rack (>= 1.0)
+ redis (2.2.2)
rspec (2.8.0)
rspec-core (~> 2.8.0)
rspec-expectations (~> 2.8.0)
@@ -38,6 +39,8 @@ GEM
ffi (~> 1.0)
multi_json (~> 1.0)
rubyzip
+ shotgun (0.9)
+ rack (>= 1.0)
sinatra (1.3.2)
rack (~> 1.3, >= 1.3.6)
rack-protection (~> 1.2)
@@ -54,5 +57,7 @@ DEPENDENCIES
launchy
mustache
rack-test
+ redis
rspec
+ shotgun
sinatra
View
@@ -2,10 +2,13 @@
require 'sinatra/base'
require 'mustache/sinatra'
+require 'redis'
class PastrApp < Sinatra::Base
register Mustache::Sinatra
+ @@redis = Redis.new(:host => 'localhost', :port => 6379)
+
require 'views/layout'
set :mustache, {
@@ -16,4 +19,15 @@ class PastrApp < Sinatra::Base
get '/' do
mustache :index
end
+
+ post '/' do
+ id = Digest::MD5.hexdigest(params[:code])
+ @@redis.set "pastr:#{id}", params[:code]
+ redirect "/#{id}"
+ end
+
+ get '/:id' do |id|
+ @code = @@redis.get "pastr:#{id}"
+ mustache :show
+ end
end
View
@@ -1,5 +1,4 @@
require File.dirname(__FILE__) + '/spec_helper'
-set :environment, :test
require 'capybara'
require 'capybara/dsl'
@@ -16,5 +15,19 @@
it "should have the title of the application" do
page.body.should match "<h1>Pastr</h1>"
end
-
+
+ it "pastes code into the form" do
+ fill_in "code", :with => 'puts "Hello, World"'
+ click_button "Pastr"
+ end
+
+ it "should show the code" do
+ page.body.should match 'puts "Hello, World"'
+ end
+
+ it "it should still show the code if the page is reloaded" do
+ visit current_url
+ page.body.should match 'puts "Hello, World"'
+ end
+
end
View
@@ -1 +1,6 @@
<h1>{{title}}</h1>
+
+<form action="/" method="post">
+ <textarea name="code"></textarea>
+ <input type="submit" value="Pastr"/>
+</form>
View
@@ -0,0 +1,3 @@
+<pre>
+{{code}}
+</pre>
View
@@ -0,0 +1,9 @@
+class PastrApp
+ module Views
+ class Show < Layout
+ def code
+ @code
+ end
+ end
+ end
+end

0 comments on commit 091163c

Please sign in to comment.