Class: Paperboy::View

Inherits:
Object
  • Object
show all
Defined in:
lib/paperboy.rb

Overview

Paperboy::View is for templating Paperboy output.

Instance Method Summary collapse

Constructor Details

#initialize(story_pkgs, outfile) ⇒ View

Views are initialized from the ‘run` method of `PaperBoy::Collector` but can also be invoked separately, if you have an array of stories.



288
289
290
291
# File 'lib/paperboy.rb', line 288

def initialize(story_pkgs,outfile)
  @stories = story_pkgs
  @outfile = outfile
end

Instance Method Details

#erb(template) ⇒ Object

Templatize your story output with embedded ruby. This allows the greatest flexibility for presenting the data.

To use, access the ‘@stories` array, and it’s component hashes. Example:

<h1>My Popular Stories</h1>

  <% @stories.each do |story| %>
    <div class="story">
       <h2><a href="<%= story[:url] %>"><%= story[:hed] %></a></h2>
       <% if not story[:img].empty? %>
         <div class="img">
            <a href="<%= story[:url] %>">
             <img src="<%= story[:img] %>">
            </a>
         </div>
       <% end %>
       <% if not story[:blurb].empty? %>
         <div class="blurb"><%= story[:blurb] %></div>
       <% end %>
    </div>
  <% end %>


351
352
353
354
355
356
357
# File 'lib/paperboy.rb', line 351

def erb(template)
  t = File.open(template).read
  template = t.to_s      
  html = ERB.new(template).result(binding)
  
  self.write(html)
end

#htmlObject

HTML is the default output method. It will return a bare-bones page of story output including blurbs and images if available.



295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
# File 'lib/paperboy.rb', line 295

def html

  html = ''
  
  @stories.each do |pkg|
  
    html << <<DOCUMENT
    <div class="story">
      <h2><a href="#{pkg[:url]}">#{pkg[:hed]}</a></h2>
DOCUMENT
    
    if not pkg[:img].empty?
      html << <<DOCUMENT
      <div class="img"><a href="#{pkg[:url]}"><img src="#{pkg[:img]}"></a></div>
DOCUMENT
    end
    
    if not pkg[:blurb].empty?
      html << <<DOCUMENT
      <div class="blurb">#{pkg[:blurb]}</div>
DOCUMENT
    end
    
    html << <<DOCUMENT
    </div>
DOCUMENT
  
  end
  
  self.write(html)
end

#write(html) ⇒ Object

Write out flat HTML to a file from either plain html or erb templating.



360
361
362
363
364
# File 'lib/paperboy.rb', line 360

def write(html)
  f = File.new(@outfile,"w+")      
  f.write(html)
  f.close
end