Class: Whisper::Blog

Inherits:
Object
  • Object
show all
Includes:
Loggy
Defined in:
lib/whisper/blog.rb

Constant Summary collapse

MAX_RECENT_COMMENTS =
30

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, entryset, commentset, router, authors) ⇒ Blog

Returns a new instance of Blog.



13
14
15
16
17
18
19
20
21
22
# File 'lib/whisper/blog.rb', line 13

def initialize config, entryset, commentset, router, authors
  @config = config
  @entryset = entryset
  @commentset = commentset
  @router = router
  @authors = authors
  @helper = CachedFile.new File.join(config.helper_dir, "helper.rb")
  @templates = {} # we'll create these lazily
  @paginations = {}
end

Instance Attribute Details

#entrysetObject

Returns the value of attribute entryset.



11
12
13
# File 'lib/whisper/blog.rb', line 11

def entryset
  @entryset
end

#helperObject

Returns the value of attribute helper.



11
12
13
# File 'lib/whisper/blog.rb', line 11

def helper
  @helper
end

Instance Method Details

#comments_for_entries(entries) ⇒ Object



151
152
153
# File 'lib/whisper/blog.rb', line 151

def comments_for_entries entries
  @commentset.comments_by_entry_id.values_at(*entries.map { |e| e.id }).map { |x| x || [] }
end

#get_content_for(path, params, request_method) ⇒ Object



27
# File 'lib/whisper/blog.rb', line 27

def get_content_for path, params, request_method; @router.get_content_for path, params, request_method end

#install_default_routes!(emailer = nil) ⇒ Object



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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/whisper/blog.rb', line 46

def install_default_routes! emailer=nil
  common_opts = { :config => @config, :router => @router, :helper => @helper, :extra_deps => [@entryset, @commentset], :entryset => @entryset }

  @router.add_handler :index, :get, "/index/:page:format" do |route, page, format, params|
    debug "index handler called for page #{page.inspect} format #{format.inspect}"

    Page.new common_opts.merge({ :format => format, :url_vars => { :route => route },
      :master_template => template_for("master", format),
      :template => template_for("list", format) }) do

      pages = paginate :entries_by_page, @entryset.timestamp, @entryset.entries
      entries = pages[page] or raise InvalidPageError
      comments = comments_for_entries entries
      { :entries => entries.zip(comments), :pages => pages.size, :page => page,
        :recent_comments => recent_comments_and_entries, :route => route
      }
    end
  end

  @router.add_handler :label, :get, "/label/+label/:page:format" do |route, label, page, format, params|
    debug "label handler called for label #{label.inspect} page #{page.inspect} format #{format.inspect}"

    Page.new common_opts.merge({:format => format, :url_vars => { :route => route, :label => label },
      :master_template => template_for("master", format),
      :template => template_for("list", format) }) do

      raw_entries = @entryset.entries_by_label[label] or raise InvalidPageError
      pages = paginate [:entries_by_label, label], @entryset.timestamp, raw_entries
      entries = pages[page] or raise InvalidPageError
      comments = comments_for_entries entries
      { :entries => entries.zip(comments), :pages => pages.size, :page => page,
        :recent_comments => recent_comments_and_entries, :route => route, :label => label
      }
    end
  end

  @router.add_handler :author, :get, "/by/+author/:page:format" do |route, author, page, format, params|
    debug "author handler called for author #{author.inspect} page #{page.inspect} format #{format.inspect}"

    Page.new common_opts.merge({:format => format, :url_vars => { :route => route, :author => author },
      :master_template => template_for("master", format),
      :template => template_for("list", format) }) do

      pages = paginate [:entries_by_author, author], @entryset.timestamp, @entryset.entries_by_author[author]
      entries = pages[page] or raise InvalidPageError
      comments = comments_for_entries entries
      { :entries => entries.zip(comments), :pages => pages.size, :page => page,
        :recent_comments => recent_comments_and_entries, :route => route, :author => author
      }
    end
  end

  ## allow formats on /. why not.
  @router.add_handler :root, :get, "/:format" do |route, format, params|
    debug "root handler called with format #{format}"
    Page.new common_opts.merge({ :format => format, :url_vars => { :route => :index },
      :template => template_for("list", format),
      :master_template => template_for("master", format) }) do

      pages = paginate :entries_by_page, @entryset.timestamp, @entryset.entries
      entries = pages[0]
      comments = comments_for_entries entries
      { :entries => entries.zip(comments), :pages => pages.size, :page => 0,
        :recent_comments => recent_comments_and_entries, :route => route
      }
    end
  end

  if emailer
    @router.add_handler :comment, :post, "/comment/+id" do |route, id, params|
      debug "request comment handler called for id #{id} with params #{params.inspect}"
      begin
        emailer.handle_send_request id, params if emailer
        ["email sent!", "text/plain"]
      rescue EmailSender::Error => e
        ["error: #{e.message}", "text/plain"]
      end
    end
  end

  ## individual entry handler is the most generic (entry ids can be close to
  ## anything), so we install it last.
  @router.add_handler :entry, :get, "/+id:format" do |route, id, format, params|
    debug "entry handler called for id #{id.inspect} format #{format.inspect}"

    Page.new common_opts.merge({ :format => format, :url_vars => { :route => route, :id => id },
      :template => template_for("entry", format),
      :master_template => template_for("master", format)
      }) do

      raise InvalidPageError unless (entry = @entryset.entries_by_id[id])
      { :entry => entry, :comments => (@commentset.comment_tree_by_entry_id[entry.id] || {}),
        :recent_comments => recent_comments_and_entries, :route => route,
        :author_info => @authors
      }
    end
  end
end

#paginate(id, timestamp, things) ⇒ Object

we’re basically doing this by hand, but we could also make a separate paginator object and fit it into the dependency model.



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/whisper/blog.rb', line 34

def paginate id, timestamp, things
  time, pages = @paginations[id]
  if time.nil? || time < timestamp
    pages = things.paginate @config.page_size
    pages[0] ||= []
    debug "repaginated #{id.inspect}: turned #{things.size} things => #{pages.size} pages"
    time = Time.now
    @paginations[id] = [time, pages]
  end
  pages
end

#recent_comments_and_entriesObject



145
146
147
148
149
# File 'lib/whisper/blog.rb', line 145

def recent_comments_and_entries
  comments = @commentset.comments[0 ... MAX_RECENT_COMMENTS]
  entries_per_comment = @entryset.entries_by_id.values_at(*comments.map { |c| c.entry_id })
  comments.zip entries_per_comment
end

#taglineObject



25
# File 'lib/whisper/blog.rb', line 25

def tagline; @config.tagline end

#template_for(name, format) ⇒ Object



28
29
30
# File 'lib/whisper/blog.rb', line 28

def template_for name, format
  @templates[[name, format]] ||= CachedFile.new File.join(@config.template_dir, "#{name}.r#{format}")
end

#titleObject



24
# File 'lib/whisper/blog.rb', line 24

def title; @config.title end