Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added Rack server

  • Loading branch information...
commit 3b565101ff12703d263f82507b53e6b0ae9a5cef 1 parent 8685ccb
@ddemaree authored
View
17 .gitignore
@@ -0,0 +1,17 @@
+*.gem
+*.rbc
+.bundle
+.config
+.yardoc
+Gemfile.lock
+InstalledFiles
+_yardoc
+coverage
+doc/
+lib/bundler/man
+pkg
+rdoc
+spec/reports
+test/tmp
+test/version_tmp
+tmp
View
2  .rspec
@@ -0,0 +1,2 @@
+--color
+--format progress
View
8 Rakefile
@@ -1,10 +1,10 @@
#!/usr/bin/env rake
require "bundler/gem_tasks"
-# require 'rspec/core/rake_task'
-# RSpec::Core::RakeTask.new(:spec)
-
+require 'rspec/core/rake_task'
require 'annotations/rake_task'
+
+RSpec::Core::RakeTask.new(:spec)
Annotations::RakeTask.new(:notes)
-task :default => [:notes]
+task :default => [:spec, :notes]
View
4 lib/soundwave.rb
@@ -30,10 +30,10 @@ def partial(name)
@paths ||= [@page.path.dirname, site.source.join("includes"), Dir.pwd].map(&:to_s).uniq
@trail ||= Hike::Trail.new(@page.site.source).tap do |t|
t.append_extension ".mustache"
- t.append_paths *@paths
+ @paths.each { |p| t.append_path(p) }
end
- if path = @trail.find(partial_path(name))
+ if path = (@trail.find(partial_path(name)) || @trail.find(name.to_s))
File.read(path)
end
end
View
2  lib/soundwave/cli.rb
@@ -1,3 +1,5 @@
+require 'soundwave'
+
module Soundwave
module CLI
extend self
View
51 lib/soundwave/server.rb
@@ -0,0 +1,51 @@
+require "soundwave"
+require "rack"
+
+module Soundwave
+ class Server
+
+ def initialize(site)
+ @site = site
+ end
+
+ def call(env)
+ if page = find_page(env["PATH_INFO"])
+ respond_with 200, page.render
+ else
+ respond_with 404, "Not Found"
+ end
+ end
+
+ def find_template(logical_path, format_extension=".mustache")
+ template_trail.find(
+ logical_path,
+ logical_path.sub(".html", ".mustache"),
+ logical_path + format_extension,
+ File.join(logical_path, "index" + format_extension)
+ )
+ end
+
+ def find_page(logical_path, format_extension=".mustache")
+ if template = find_template(logical_path, format_extension)
+ Page.new(@site, template)
+ end
+ end
+
+ def respond_with(status, body, content_type = "text/html; charset=utf-8")
+ headers = {
+ "Content-Type" => content_type,
+ "Content-Length" => Rack::Utils.bytesize(body).to_s
+ }
+ [status, headers, [body]]
+ end
+
+ protected
+
+ def template_trail
+ @_template_trail ||= Hike::Trail.new(@site.source).tap do |t|
+ t.append_path "."
+ end
+ end
+
+ end
+end
View
1  soundwave.gemspec
@@ -21,6 +21,7 @@ Gem::Specification.new do |gem|
gem.add_runtime_dependency "mustache"
gem.add_runtime_dependency "hike"
+ gem.add_development_dependency "rack"
gem.add_development_dependency "annotations"
gem.add_development_dependency "bundler"
gem.add_development_dependency "rspec", "~> 2.8.0"
View
1  spec/fixtures/site/index.mustache
@@ -0,0 +1 @@
+Hello world!
View
30 spec/server_spec.rb
@@ -0,0 +1,30 @@
+require "spec_helper"
+require "soundwave/server"
+
+describe Soundwave::Server do
+
+ let(:root_path) { Pathname(File.expand_path("../fixtures/site", __FILE__)) }
+ let(:site) { Soundwave::Site.new(root_path) }
+ let(:server) { Soundwave::Server.new(site) }
+
+ it "returns index.html given a blank path" do
+ path = server.find_template("/")
+ path.should == root_path.join("index.mustache").to_s
+ end
+
+ it "converts html paths to mustache ones" do
+ path = server.find_template("/index.html")
+ path.should == root_path.join("index.mustache").to_s
+ end
+
+ it "renders the found template" do
+ response = server.call("PATH_INFO" => "/")
+ response.should == [200, {"Content-Type" => "text/html; charset=utf-8", "Content-Length" => "12"}, ["Hello world!"]]
+ end
+
+ it "returns 404 if the template was not found" do
+ response = server.call("PATH_INFO" => "/bogus.html")
+ response[0].should == 404
+ end
+
+end
View
11 spec/spec_helper.rb
@@ -0,0 +1,11 @@
+# This file was generated by the `rspec --init` command. Conventionally, all
+# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
+# Require this file using `require "spec_helper.rb"` to ensure that it is only
+# loaded once.
+#
+# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
+RSpec.configure do |config|
+ # config.treat_symbols_as_metadata_keys_with_true_values = true
+ # config.run_all_when_everything_filtered = true
+ # config.filter_run :focus
+end
Please sign in to comment.
Something went wrong with that request. Please try again.