Static site generation with a familiar API
Quick and easy static site generation with a familiar API.
class App < Sharpie::Base
get "/" do
"Hello, world."
end
# get posts from ActiveRecord or elsewhere
def self.posts
Post.all
end
posts.each do |post|
get "/posts/#{post.id}" do
erb :post, :locals => {:post => post}
end
get "/posts/#{post.id}.json" do
post.to_json
end
end
end
App.build!("_site")
Or use with an existing Sinatra application:
class App < Sinatra::Base
register Sinatra::AdvancedRoutes # this is required
def self.posts
Post.all
end
posts.each do |post|
get "/posts/#{post['id']}.json" do
post.to_json
end
end
get "/" do
erb :index
end
end
builder = Sharpie::Builder.new(App)
builder.build!("_site")
Sharpie::Base
subclasses from Sinatra::Base
.
Add this line to your application’s Gemfile:
gem 'sharpie'
And then execute:
$ bundle
Or install it yourself as:
$ gem install sharpie
git checkout -b my-new-feature
)git commit -am 'Add some feature'
)git push origin my-new-feature
)Sinatra routes make web app and API creation dead-simple. The syntax is straightforward and allows for your site to feel more like Ruby and less like a bloated framework. And there’s also no reason why this powerful API should be limited to dynamic web applications. Sinatra routes simply describe a path and a response. Whether the response is evaluated ahead of time or on fly should not matter.
Jekyll is great for blogs and other types of sites where content is “hand created.” That is, a human creates a file in a folder and writes some Markdown. Sharpie is more geared towards use-cases where the original content is already in a machine-readable format. For example, if you have a database whose contents you want to expose via REST API. You also don’t want to maintain a server and you don’t want to worry about scaling. With Sharpie, you can write such an API with very few (yet straightfoward) lines of code.
This software is mostly a fork of the sinatra-static gem by Paul Asmuth. Paul’s code is very well-written. I’d rather copy and attribute it than re-invent a well-made wheel. I’m focusing on building an interface that suits my needs. More specifically, I want Sharpie to be part of a toolchain for rapidly developing static file-backed REST APIs.
This software, which is a fork of MIT-licensed software, is also MIT-licensed. See LICENSE.txt
.