class Planet::Blog

Attributes

author[RW]
categories[RW]
feed[RW]
image[RW]
name[RW]
planet[RW]
posts[RW]
rss_data[RW]
tags[RW]
twitter[RW]
type[RW]
url[RW]

Public Class Methods

new(attributes = {}) click to toggle source
# File lib/planet/blog.rb, line 21
def initialize(attributes = {})
  self.url        = attributes[:url]
  self.feed       = attributes[:feed]
  self.type       = attributes[:type]
  self.name       = attributes[:name]
  self.author     = attributes[:author]
  self.image      = attributes[:image]
  self.twitter    = attributes[:twitter]
  self.posts      = attributes.fetch(:posts, [])
  self.planet     = attributes[:planet]
  self.categories = attributes.fetch(:categories, '')
  self.tags       = attributes.fetch(:tags, '')

  # Feedzirra parsed data is  made available for when the information
  # provides is not enough. Transparency should help use cases we're
  # not considering.
  self.rss_data = nil

  # get parser-manager instance
  @parsers = Parsers.new
end

Public Instance Methods

fetch() click to toggle source
# File lib/planet/blog.rb, line 43
def fetch
  # given parser can be set arbitrarily with :type or inferred from the domain
  parser = self.type ? @parsers.get_parser(self.type) : @parsers.get_parser_for(self.feed)

  # parser instances should mimick Feedzirra interface
  parser.fetch_and_parse(self.feed,
                        :on_success => lambda { |url, feed| on_fetch_success(feed) },
                        :on_failure => lambda { |url, response| puts "\t=> Failed to fetch #{url.inspect} the server returned: #{response}" })
end
on_fetch_success(feed) click to toggle source
# File lib/planet/blog.rb, line 53
def on_fetch_success(feed)
  self.name ||= feed.title || 'the source'
  self.url  ||= feed.url

  if self.url.nil?
    abort "#{ self.author }'s blog does not have a url field on it's feed, you will need to specify it on planet.yml"
  end

  self.rss_data = feed

  feed.entries.each do |entry|
    next unless whitelisted?(entry)
    content = if entry.content
                self.sanitize_images(entry.content.strip)
              elsif entry.summary
                self.sanitize_images(entry.summary.strip)
              else
                abort "=> No content found on entry"
              end

    if self.planet.config.fetch('sanitize_html', false)
        content = Sanitize.fragment(content, Sanitize::Config::RELAXED)
    end

    self.posts << @post = Post.new(
      title: entry.title.nil? ? self.name : entry.title,
      content: content,
      date: entry.published,
      url: entry.url,
      blog: self,
      rss_data: entry
    )

    puts "=> Found post titled #{ @post.title } - by #{ @post.blog.author }"
  end
end
sanitize_images(html) click to toggle source
# File lib/planet/blog.rb, line 90
def sanitize_images(html)
  ## We take all images with src not matching http refs and append
  ## the original blog to them.
  html.scan(/<img src="([^h"]+)"/).flatten.each do |img|
    if img[0] == '/'
      html.gsub!(img, "#{ self.url }#{ img }")
    else
      html.gsub!(img, "#{ self.url }/#{ img }")
    end
  end

  html
end
whitelisted?(entry) click to toggle source
# File lib/planet/blog.rb, line 104
def whitelisted?(entry)
  return true if self.planet.whitelisted_tags.empty?
  result = !(entry.categories & self.planet.whitelisted_tags).empty?
  puts "\t=> Ignored post titled: #{entry.title} with categories: [#{entry.categories.join(', ')}]" unless result
  result
end