Class: Toto::Site

Inherits:
Object show all
Defined in:
lib/toto.rb

Defined Under Namespace

Classes: Context

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Site

Returns a new instance of Site.



35
36
37
# File 'lib/toto.rb', line 35

def initialize config
  @config = config
end

Instance Method Details

#/Object



76
77
78
# File 'lib/toto.rb', line 76

def /
  self[:root]
end

#[](*args) ⇒ Object



39
40
41
# File 'lib/toto.rb', line 39

def [] *args
  @config[*args]
end

#[]=(key, value) ⇒ Object



43
44
45
# File 'lib/toto.rb', line 43

def []= key, value
  @config.set key, value
end

#archives(filter = //) ⇒ Object



61
62
63
64
65
66
67
68
69
70
# File 'lib/toto.rb', line 61

def archives filter = //
  entries = ! self.articles.empty??
    self.articles.select do |a|
      File.basename(a) =~ /^#{filter}/
    end.reverse.map do |article|
      Article.new File.new(article), @config
    end : []

  return :archives => Archives.new(entries)
end

#article(route) ⇒ Object



72
73
74
# File 'lib/toto.rb', line 72

def article route
  Article.new(File.new("#{Paths[:articles]}/#{route.join('-')}.#{self[:ext]}"), @config).load
end

#go(route, type = :html) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/toto.rb', line 80

def go route, type = :html
  route << self./ if route.empty?
  type = type.to_sym

  body, status = if Context.new.respond_to?(:"to_#{type}")
    if route.first =~ /\d{4}/
      begin
        case route.size
          when 1..3
            [Context.new(archives(route * '-'), @config).render(:archives, type), 200]
          when 4
            [Context.new(article(route), @config).render(:article, type), 200]
          else http 400
        end
      rescue Errno::ENOENT => e
        $stderr.puts e
        http 404
      end
    elsif respond_to?(route = route.first.to_sym)
      [Context.new(send(route, type), @config).render(route, type), 200]
    else
      http 401
    end
  else
    http 400
  end

  return :body => body, :type => type, :status => status
end

#index(type = :html) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/toto.rb', line 47

def index type = :html
  case type
    when :html
      {:articles => self.articles.reverse.map do |article|
          Article.new File.new(article), @config
      end }.merge archives
    when :xml, :json
      return :articles => self.articles.map do |article|
        Article.new File.new(article), @config
      end
    else return {}
  end
end