Skip to content

Commit

Permalink
Booyah, rewrote the old version now using only rack
Browse files Browse the repository at this point in the history
  • Loading branch information
stas committed Jan 5, 2012
1 parent e36adb1 commit 24b5b00
Show file tree
Hide file tree
Showing 19 changed files with 278 additions and 242 deletions.
18 changes: 9 additions & 9 deletions Gemfile.lock
@@ -1,24 +1,24 @@
PATH
remote: .
specs:
pagina (0.1)
pagina (0.2)
dalli
kramdown
sinatra
rack

GEM
remote: http://rubygems.org/
specs:
dalli (1.0.3)
kramdown (0.13.3)
rack (1.2.2)
sinatra (1.2.6)
rack (~> 1.1)
tilt (< 2.0, >= 1.2.2)
tilt (1.3)
bacon (1.1.0)
dalli (1.1.4)
kramdown (0.13.4)
rack (1.4.0)
rake (0.9.2.2)

PLATFORMS
ruby

DEPENDENCIES
bacon
pagina!
rake
13 changes: 13 additions & 0 deletions Rakefile
@@ -0,0 +1,13 @@
require 'rake'
require 'rake/clean'

CLEAN.include %w[
pagina.log
]

desc "Run all the tests"
task :test do
sh "bacon test/init.rb"
end

task :default => 'test'
8 changes: 0 additions & 8 deletions config.ru

This file was deleted.

14 changes: 0 additions & 14 deletions config/pagina.yaml

This file was deleted.

68 changes: 68 additions & 0 deletions lib/pagina.rb
@@ -0,0 +1,68 @@
##
# Our own namespace
module Pagina

require 'erb'
require 'open-uri'
require 'logger'
require 'kramdown'
require 'dalli'

VERSION = '0.2'
ROOT = File.dirname __FILE__

$LOAD_PATH.unshift(Pagina::ROOT)
require 'pagina/app'
require 'pagina/page'


class << self
attr_accessor :options
end
@options = {
'title' => 'Default Pagina Title',
'description' => 'Default Pagina Description',
'dropbox_url' => 'http://dl.dropbox.com/u/',
'dropbox_id' => 'Your DropBox ID comes here',
'dropbox_folder' => 'Your DropBox folder to be used for app',
'page_extension' => '.txt',
'cache' => false,
'memcache_servers' => ['An array of server IP:PORTs'],
'memcache_user' => 'Your memcache user',
'memcache_password' => 'Your memcache password',
'layout' => 'Full path to your layout file',
'content_type' => 'text/html',
'e404_message' => 'Sorry, not found!',
'logger' => Logger.new(STDOUT)
}


##
# If caching was set to true, load the memcache client
if !@options['cache'].nil? && @options['cache'] == true
Pagina.memcache_servers = ENV['MEMCACHE_SERVERS'].split(' ') if !ENV['MEMCACHE_SERVERS'].nil?
Pagina.memcache_user = ENV['MEMCACHE_USERNAME'] if !ENV['MEMCACHE_USERNAME'].nil?
Pagina.memcache_password = ENV['MEMCACHE_PASSWORD'] if !ENV['MEMCACHE_PASSWORD'].nil?
@options['cache'] = Dalli::Client.new(
Pagina.memcache_servers,
:username => Pagina.memcache_user,
:password => Pagina.memcache_password
)
Pagina.logger.info('Caching client started')
end

##
# Simple wrapper to ease access to our @options
# If an argument is passed, the old value will be re-written
def self.method_missing(method, *args)
key = method.to_s
new_value = args.first
if @options.include? key
@options[key] = new_value if !new_value.nil?
return @options[key]
else
super
end
end

end #Pagina
70 changes: 24 additions & 46 deletions lib/pagina/app.rb
@@ -1,49 +1,27 @@
require 'rubygems'
require 'sinatra/base'
module Pagina
##
# Default App to be started
class App

require 'pagina/config'
require 'pagina/sitemap'
require 'pagina/page'
def call(env)
request = Rack::Request.new(env)
name = request.path_info[1..-1] # Get rid of first slash
name = 'index' if name.empty?
Pagina.logger.info("New page request: #{name}")

module Pagina
class App < Sinatra::Base
set :root, File.expand_path( '../../', File.dirname(__FILE__) )
set :config, File.join(root, 'config')
set :views, File.join(root, 'views')
set :public, File.join(root, 'public')
set :environment, :production

helpers do
def load_config
config_data = Pagina::Config.new
if !config_data.nil?
@site_name = config_data.name
@site_description = config_data.description
end
end
end

get '/' do
redirect '/index'
end

get '/:name' do
load_config
@page = Pagina::Page.new(params[:name])

raise Sinatra::NotFound if @page.title.nil?
erb :page
end

not_found do
load_config
erb :'404'
end

error do
load_config
erb :'500'
p = Pagina::Page.new(name) || Pagina.e404_message
page_layout = ERB.new File.open(Pagina.layout).read

@site_name = Pagina.title
@site_description = Pagina.description
@content = p.content || Pagina.e404_message
@title = p.title || Pagina.e404_message
[
p.title.nil? ? 404 : 200,
{"Content-Type" => Pagina.content_type},
[page_layout.result(binding)]
]
end
end
end

end #App
end #Pagina
23 changes: 0 additions & 23 deletions lib/pagina/cache.rb

This file was deleted.

34 changes: 0 additions & 34 deletions lib/pagina/config.rb

This file was deleted.

82 changes: 63 additions & 19 deletions lib/pagina/page.rb
@@ -1,26 +1,70 @@
require 'kramdown'

module Pagina

##
# Page Handler class
class Page
attr_reader :title, :content


attr_accessor :content, :title

##
# Page Constructor
#
# @param [String] name, the requested page
def initialize(name)
sitemap = Pagina::Sitemap.new
if sitemap.nil?
return nil
end
page_name = name.to_s + '.txt'
page = sitemap.find(page_name)
@name = name
@content = nil
@title = nil
@page_path = Pagina.dropbox_url +
Pagina.dropbox_id.to_s + '/' +
Pagina.dropbox_folder + '/' +
name +
(!Pagina.page_extension.empty? ? Pagina.page_extension : '')

if !page.nil?
@title = page[:content].split("\n")[0]
@content = page[:content]
raw_page = build
@title = raw_page.split("\n")[0] if !raw_page.nil?
@content = Kramdown::Document.new(raw_page).to_html if !raw_page.nil?
end

##
# Try to build the page response
# If caching is set, it will be asked from cache
#
# @return [String], the raw page content
def build
result = nil
if Pagina.cache != false
result = Pagina.cache.get(@name)
Pagina.logger.info("Page loaded from cache: #{@name}")
end

!result.nil? ? result : request_page
end

def body_html
Kramdown::Document.new(@content).to_html

##
# Try to request page data and format it to HTML
# If caching is set, the page content cache will be updated
#
# @return [String], the raw page content
def request_page
page_data = nil
begin
page_request = open(@page_path)
if page_request.status[0].to_i == 200
page_data = page_request.string
end
rescue OpenURI::HTTPError
Pagina.logger.info("Error trying to fetch: #{@name}")
return nil
end

if Pagina.cache != false and !page_data.nil?
Pagina.cache.add(@name, page_data)
Pagina.logger.info("Page cached: #{@name}")
else
Pagina.logger.info("Page served uncached: #{@name}")
end
page_data
end
end
end

end #Page
end # Pagina

0 comments on commit 24b5b00

Please sign in to comment.