Class: TotoBongo::Site

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

Overview

Site Is responsible for handling the site It has handles the

Defined Under Namespace

Classes: Context

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Site

Returns a new instance of Site.



122
123
124
125
# File 'lib/toto-bongo.rb', line 122

def initialize config
  TotoBongo::logger.debug("Called Site::initialize")
  @config = config
end

Instance Method Details

#/Object

called when the user requests a / Returns whatever the site index config is default is “index”



171
172
173
174
# File 'lib/toto-bongo.rb', line 171

def /
  TotoBongo::logger.debug("Called Site::/ ")
  self[:root]
end

#[](*args) ⇒ Object



127
128
129
130
# File 'lib/toto-bongo.rb', line 127

def [] *args
  TotoBongo::logger.debug("Called Site::[]: args = #{args}")
  @config[*args]
end

#[]=(key, value) ⇒ Object



132
133
134
135
# File 'lib/toto-bongo.rb', line 132

def []= key, value
  TotoBongo::logger.debug("Called Site::[]=: key = #{key} value=#{value}")
  @config.set key, value
end

#archives(filter = "") ⇒ Object

Makes the list’s of all articles pages/archives.html.haml with access to :archives to generate the full list of posts



152
153
154
155
156
157
158
159
160
161
# File 'lib/toto-bongo.rb', line 152

def archives filter = ""
  TotoBongo::logger.debug("Called Site::archive ")
  entries = ! self.articles.empty??
    self.articles.select do |a|
      filter !~ /^\d{4}/ || File.basename(a) =~ /^#{filter}/
    end.reverse.map do |article|
      Article.new article, @config
    end : []
  return :archives => Archives.new(entries, @config)
end

#article(route) ⇒ Object



163
164
165
166
# File 'lib/toto-bongo.rb', line 163

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

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

Called by the server after the route and the mime type are taken from the request,

This is the first function that is called from the server. It should return the html to render



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/toto-bongo.rb', line 184

def go route, env = {}, type = :html
  TotoBongo::logger.debug("Called Site::go ")
  #check if the request includes an specific route
  #else call / to get the index 
  route << self./ if route.empty?

  type, path = type =~ /html|xml|json/ ? type.to_sym : :html, route.join('/')
  context = lambda do |data, page|
    Context.new(data, @config, path, env).render(page, type)
  end

  body, status = if Context.new.respond_to?(:"to_#{type}")

    if route.first =~ /\d{4}/
      case route.size
        when 1..3
          context[archives(route * '-'), :archives]
        when 4
          context[article(route), :article]
        else 
          puts "400"
          http 400
      end #end case


    # Responde to a path, when the request is for example index  
    elsif respond_to?(path)
      #call the path, it will return the HTML of the path
      context[send(path, type), path.to_sym]
    else
      context[{}, path.to_sym]
    end
  else
    http 400
  end #end context new respond

return body, status

rescue Errno::ENOENT => e
   TotoBongo::logger.info("Errno:ENOENT: #{e.message} ")
   return :body => http(404).first, :type => :html, :status => 404
else
  TotoBongo::logger.debug("Status set 200 OK")
  return :body => body || "", :type => type, :status => status || 200
end

#index(type = :html) ⇒ Object

Called when index is requested



139
140
141
142
143
144
145
146
# File 'lib/toto-bongo.rb', line 139

def index type = :html
  TotoBongo::logger.debug("Called Site::index")
  articles = type == :html ? self.articles.reverse : self.articles
  #know initialize the articles
  {:articles => articles.map do |article|
    Article.new article, @config
  end}.merge archives
end