Module: Sinatra::HacketySling

Defined in:
lib/sinatra/hackety_sling.rb,
lib/sinatra/hackety_sling/post.rb,
lib/sinatra/hackety_sling/version.rb

Defined Under Namespace

Classes: Post

Constant Summary collapse

VERSION =
'0.1.3'

Class Method Summary collapse

Class Method Details

.registered(app) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/sinatra/hackety_sling.rb', line 8

def self.registered(app)
  app.set :hackety_sling_posts_on_index, 2
  app.set :hackety_sling_title, nil
  app.set :hackety_sling_author, nil

  app.get '/' do
    limit = app.hackety_sling_posts_on_index
    @posts = Post.order_by(date: :desc).limit(limit).all
    erb :index
  end

  app.get '/archive/?' do
    @posts = Post.order_by(date: :desc).all
    erb :post_list
  end

  %w(tags author).each do |attribute|
    app.get "/#{attribute}/:value/?" do |value|
      @posts = Post.order_by(date: :desc)
      @posts = @posts.where(attribute.to_sym.include => value).all
      erb :posts
    end
  end

  Post.all.each do |post|
    app.get "#{post.permalink}?" do
      @post = post
      erb :post
    end
    base_name = post.file_name_without_extension.sub(/\d{4}-\d{2}-\d{2}-/, '')
    app.get "/#{base_name}/?" do
      redirect post.permalink, 301
    end
  end

  ymd = [:year, :month, :day]
  app.get %r{/(\d{4}/?)(\d{2}/?)?(\d{2}/?)?} do
    selector_hash = {}
    params[:captures].each_with_index do |date_part, index|
      selector_hash[ymd[index]] = date_part.to_i unless date_part.nil?
    end
    @posts = Post.order_by(date: :desc).where(selector_hash).all
    if @posts.length == 1
      redirect @posts.first.permalink, 301
    else
      erb :posts
    end
  end

  app.get '/atom.xml' do
    content_type 'application/atom+xml'
    feed = Atom::Feed.new do |f|
      f.title = app.hackety_sling_title ||= 'HacketySling Blog'
      blog_url = request.url.sub(request.fullpath, '/')
      f.links << Atom::Link.new(href: blog_url)
      f.links << Atom::Link.new(rel: 'self', href: request.url)
      f.updated = Post.last.date.xmlschema + 'T00:00:00+00:00'
      author = app.hackety_sling_author ||= app.hackety_sling_title
      f.authors << Atom::Person.new(name: author)
      f.id = blog_url
      Post.order_by(date: :desc).limit(10).all.each do |post|
        f.entries << Atom::Entry.new do |e|
          e.title = post.title
          e.links << Atom::Link.new(href: blog_url + post.permalink)
          e.id = blog_url + post.permalink
          e.content = Atom::Content::Html.new post.to_html
          e.updated = post.date.xmlschema + 'T00:00:00+00:00'
        end
      end
    end
    feed.to_xml.to_s
  end
end