Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

First commit, specs passing

  • Loading branch information...
commit 42543e5b2de1e37981a75353d5e4a74956418e36 1 parent 5e2dd99
@winton authored
View
5 .travis.yml
@@ -0,0 +1,5 @@
+language: ruby
+rvm:
+ - 1.8.7
+ - 1.9.2
+ - 1.9.3
View
2  LICENSE
@@ -15,4 +15,4 @@ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
-CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
8 README.md
@@ -1,4 +1,4 @@
-GemTemplate
+Stasis-server
===========
A gem template for new projects.
@@ -16,8 +16,8 @@ Setup the template
You only have to do this once.
<pre>
-git clone git@github.com:winton/gem_template.git
-cd gem_template
+git clone git@github.com:winton/stasis-server.git
+cd stasis-server
stencil
</pre>
@@ -29,7 +29,7 @@ Do this for every new project.
<pre>
mkdir my_project
git init
-stencil gem_template
+stencil stasis-server
rake rename
</pre>
View
30 Rakefile
@@ -1,27 +1,7 @@
require 'bundler/gem_tasks'
+require 'spec/rake/spectask'
-# DELETE AFTER USING
-desc "Rename project"
-task :rename do
- name = ENV['NAME'] || File.basename(Dir.pwd)
- camelize = lambda do |str|
- str.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
- end
- dir = Dir['**/gem_template*']
- begin
- from = dir.pop
- if from
- to = from.split('/')
- to[-1].gsub!('gem_template', name)
- FileUtils.mv(from, to.join('/'))
- end
- end while dir.length > 0
- Dir["**/*"].each do |path|
- if File.file?(path)
- `sed -i '' 's/gem_template/#{name}/g' #{path}`
- `sed -i '' 's/GemTemplate/#{camelize.call(name)}/g' #{path}`
- no_space = File.read(path).gsub(/\s+\z/, '')
- File.open(path, 'w') { |f| f.write(no_space) }
- end
- end
-end
+Spec::Rake::SpecTask.new(:spec) do |t|
+ t.spec_files = FileList['spec/**/*_spec.rb']
+end
+task :default => :spec
View
3  bin/gem_template
@@ -1,3 +0,0 @@
-#!/usr/bin/env ruby
-
-require File.expand_path("../../lib/gem_template", __FILE__)
View
15 bin/stasis-server
@@ -0,0 +1,15 @@
+#!/usr/bin/env ruby
+
+require File.expand_path(File.dirname(__FILE__) + "/../lib/stasis/server")
+
+gem "slop", "~> 2.1.0"
+require 'slop'
+
+slop = Slop.parse :help => true, :optional_arguments => true do
+ on :r, :redis, "Redis URI (default: localhost:6379/0)", :default => "localhost:6379/0"
+end
+
+exit if slop.help?
+options = slop.to_hash
+
+Stasis::Server.new(Dir.pwd, options)
View
4 lib/gem_template.rb
@@ -1,4 +0,0 @@
-$:.unshift File.dirname(__FILE__)
-
-module GemTemplate
-end
View
118 lib/stasis/server.rb
@@ -0,0 +1,118 @@
+gem "redis", "~> 2.2.2"
+gem "stasis", "0.1.23"
+gem "yajl-ruby", "~> 1.0.0"
+
+require 'digest/sha1'
+require 'redis'
+require 'stasis'
+require 'yajl'
+
+class Stasis
+ class Server
+
+ def initialize(root, options={})
+ puts "\nStarting Stasis server (redis @ #{options[:server]})..."
+
+ redis = Redis.connect(:url => "redis://#{options[:server]}")
+ stasis = Stasis.new(*[ root, options[:public], options ].compact)
+ retries = 0
+
+ begin
+ while true
+ sleep(1.0 / 1000.0)
+ request = redis.lpop('stasis:requests')
+
+ if request
+ files = {}
+ request = Yajl::Parser.parse(request)
+ paths = request['paths']
+
+ unless request['force']
+ paths = request['paths'].reject do |path|
+ files[path] = redis.get("stasis:caches:#{root}:#{path}")
+ end
+ end
+
+ if paths.empty? && !request['paths'].empty?
+ new_files = {}
+ else
+ params = request['paths'] + [
+ {
+ :collect => request['return'] || request['force'],
+ :params => request['params'],
+ :write => request['write']
+ }
+ ]
+ new_files = stasis.render(*params) || {}
+ end
+
+ if request['ttl']
+ new_files.each do |path, body|
+ key = "stasis:caches:#{root}:#{path}"
+ redis.set(key, body)
+ redis.expire(key, request['ttl'])
+ end
+ end
+
+ if request['return']
+ request['wait'] = true
+ end
+
+ if request['wait']
+ response = files.merge(new_files)
+ redis.publish(self.class.response_key(request['id']), Yajl::Encoder.encode(response))
+ end
+ end
+ end
+ rescue Interrupt
+ shut_down
+ rescue Exception => e
+ puts "\nError: #{e.message}"
+ puts "\t#{e.backtrace.join("\n\t")}"
+ retries += 1
+ shut_down if retries >= 10
+ retry
+ end
+ end
+
+ def shut_down
+ puts "\nShutting down Stasis server..."
+ exit
+ end
+
+ class <<self
+ def push(options)
+ options[:id] = Digest::SHA1.hexdigest("#{options['paths']}#{rand}")
+ redis_url = "redis://#{options.delete(:redis) || "localhost:6379/0"}"
+ response = nil
+
+ redis_1 = Redis.connect(:url => redis_url)
+ redis_2 = Redis.connect(:url => redis_url)
+
+ if options[:return] || options[:wait]
+ redis_1.subscribe(response_key(options[:id])) do |on|
+ on.subscribe do |channel, subscriptions|
+ redis_2.rpush("stasis:requests", Yajl::Encoder.encode(options))
+ end
+
+ on.message do |channel, message|
+ response = Yajl::Parser.parse(message)
+ redis_1.unsubscribe
+ end
+ end
+ else
+ redis_1.rpush("stasis:requests", Yajl::Encoder.encode(options))
+ end
+
+ redis_1.quit
+ redis_2.quit
+
+ response
+ end
+
+ def response_key(id)
+ "stasis:response:#{id}"
+ end
+ end
+ end
+end
View
2  spec/fixtures/project/params.html.haml
@@ -0,0 +1,2 @@
+params
+= params.inspect
View
2  spec/fixtures/project/public/params.html
@@ -0,0 +1,2 @@
+params
+{:test=>true}
View
2  spec/fixtures/project/public/time.html
@@ -0,0 +1,2 @@
+time
+1341050857.4833658
View
2  spec/fixtures/project/time.html.haml
@@ -0,0 +1,2 @@
+time
+= Time.now.to_f
View
4 spec/gem_template_spec.rb
@@ -1,4 +0,0 @@
-require 'spec_helper'
-
-describe GemTemplate do
-end
View
22 spec/spec_helper.rb
@@ -1,8 +1,28 @@
require "pp"
require "bundler"
+Bundler.setup(:default)
Bundler.require(:development)
$root = File.expand_path('../../', __FILE__)
+require "#{$root}/lib/stasis/server"
-require "#{$root}/lib/gem_template"
+def generate(options={})
+ $files = nil if options[:reload]
+ $fixture = "#{$root}/spec/fixtures/project"
+ unless $files
+ $stasis ||= Stasis.new($fixture)
+ $stasis.render(*[ options[:only] ].flatten.compact)
+ generate_files
+ end
+end
+
+def generate_files
+ pub = "#{$fixture}/public"
+ $files = Dir.glob("#{pub}/**/*", File::FNM_DOTMATCH).inject({}) do |hash, path|
+ if File.file?(path)
+ hash[path[pub.length+1..-1]] = File.read(path)
+ end
+ hash
+ end
+end
View
65 spec/stasis/server_spec.rb
@@ -0,0 +1,65 @@
+require 'spec_helper'
+require 'stasis/server'
+
+describe Stasis::Server do
+
+ before(:all) do
+ generate
+ @thread = Thread.new do
+ Stasis::Server.new($fixture, :server => 'localhost:6379/0')
+ end
+ end
+
+ after(:all) do
+ @thread.kill
+ end
+
+ it "should change time.html" do
+ time = $files['time.html'].split("time")[1].strip
+ new_time = Stasis::Server.push(
+ :paths => [ 'time.html.haml' ],
+ :redis => 'localhost:6379/0',
+ :return => true
+ )['time.html.haml'].split("time")[1].strip
+ time.should_not == new_time
+ generate_files
+ new_time_from_file = $files['time.html'].split("time")[1].strip
+ new_time_from_file.should == new_time
+ new_time_from_file.should_not == time
+ end
+
+ it "should pass params" do
+ params = Stasis::Server.push(
+ :paths => [ 'params.html.haml' ],
+ :params => { :test => true },
+ :redis => 'localhost:6379/0',
+ :return => true
+ )['params.html.haml'].split("params")[1].strip
+ eval(params).should == { :test => true }
+ end
+
+ it "should expire after ttl" do
+ time = Stasis::Server.push(
+ :paths => [ 'time.html.haml' ],
+ :redis => 'localhost:6379/0',
+ :return => true,
+ :ttl => 1,
+ :write => false
+ )['time.html.haml'].split("time")[1].strip
+ time2 = Stasis::Server.push(
+ :paths => [ 'time.html.haml' ],
+ :redis => 'localhost:6379/0',
+ :return => true,
+ :write => false
+ )['time.html.haml'].split("time")[1].strip
+ time.should == time2
+ sleep 2
+ time3 = Stasis::Server.push(
+ :paths => [ 'time.html.haml' ],
+ :redis => 'localhost:6379/0',
+ :return => true,
+ :write => false
+ )['time.html.haml'].split("time")[1].strip
+ time2.should_not == time3
+ end
+end
View
8 gem_template.gemspec → stasis-server.gemspec
@@ -5,7 +5,7 @@ lib = "#{root}/lib"
$:.unshift lib unless $:.include?(lib)
Gem::Specification.new do |s|
- s.name = "gem_template"
+ s.name = "stasis-server"
s.version = '0.1.0'
s.platform = Gem::Platform::RUBY
s.authors = []
@@ -19,5 +19,11 @@ Gem::Specification.new do |s|
s.require_paths = %w(lib)
s.test_files = `cd #{root} && git ls-files -- {features,test,spec}/*`.split("\n")
+ s.add_development_dependency "haml"
+ s.add_development_dependency "rake"
s.add_development_dependency "rspec", "~> 1.0"
+
+ s.add_dependency "redis", "~> 2.2.2"
+ s.add_dependency "slop", "~> 2.1.0"
+ s.add_dependency "stasis", "0.1.23"
end
Please sign in to comment.
Something went wrong with that request. Please try again.