Class: Planet::Blog
- Inherits:
-
Object
- Object
- Planet::Blog
- Defined in:
- lib/planet/blog.rb
Instance Attribute Summary collapse
-
#author ⇒ Object
Returns the value of attribute author.
-
#feed ⇒ Object
Returns the value of attribute feed.
-
#image ⇒ Object
Returns the value of attribute image.
-
#name ⇒ Object
Returns the value of attribute name.
-
#planet ⇒ Object
Returns the value of attribute planet.
-
#posts ⇒ Object
Returns the value of attribute posts.
-
#rss_data ⇒ Object
Returns the value of attribute rss_data.
-
#twitter ⇒ Object
Returns the value of attribute twitter.
-
#type ⇒ Object
Returns the value of attribute type.
-
#url ⇒ Object
Returns the value of attribute url.
Instance Method Summary collapse
- #fetch ⇒ Object
-
#initialize(attributes = {}) ⇒ Blog
constructor
A new instance of Blog.
- #on_fetch_success(feed) ⇒ Object
- #sanitize_images(html) ⇒ Object
- #whitelisted?(entry) ⇒ Boolean
Constructor Details
#initialize(attributes = {}) ⇒ Blog
Returns a new instance of Blog.
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/planet/blog.rb', line 18 def initialize(attributes = {}) self.url = attributes[:url] self.feed = attributes[:feed] self.type = attributes[:type] self.name = attributes[:name] self. = attributes[:author] self.image = attributes[:image] self.twitter = attributes[:twitter] self.posts = attributes.fetch(:posts, []) self.planet = attributes[:planet] # 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 |
Instance Attribute Details
#author ⇒ Object
Returns the value of attribute author.
7 8 9 |
# File 'lib/planet/blog.rb', line 7 def @author end |
#feed ⇒ Object
Returns the value of attribute feed.
7 8 9 |
# File 'lib/planet/blog.rb', line 7 def feed @feed end |
#image ⇒ Object
Returns the value of attribute image.
7 8 9 |
# File 'lib/planet/blog.rb', line 7 def image @image end |
#name ⇒ Object
Returns the value of attribute name.
7 8 9 |
# File 'lib/planet/blog.rb', line 7 def name @name end |
#planet ⇒ Object
Returns the value of attribute planet.
7 8 9 |
# File 'lib/planet/blog.rb', line 7 def planet @planet end |
#posts ⇒ Object
Returns the value of attribute posts.
7 8 9 |
# File 'lib/planet/blog.rb', line 7 def posts @posts end |
#rss_data ⇒ Object
Returns the value of attribute rss_data.
7 8 9 |
# File 'lib/planet/blog.rb', line 7 def rss_data @rss_data end |
#twitter ⇒ Object
Returns the value of attribute twitter.
7 8 9 |
# File 'lib/planet/blog.rb', line 7 def twitter @twitter end |
#type ⇒ Object
Returns the value of attribute type.
7 8 9 |
# File 'lib/planet/blog.rb', line 7 def type @type end |
#url ⇒ Object
Returns the value of attribute url.
7 8 9 |
# File 'lib/planet/blog.rb', line 7 def url @url end |
Instance Method Details
#fetch ⇒ Object
38 39 40 41 42 43 44 45 46 |
# File 'lib/planet/blog.rb', line 38 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_code, response_header, response_body| puts "\t=> Failed to fetch #{url} with response_code: #{response_code}" }) end |
#on_fetch_success(feed) ⇒ Object
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 |
# File 'lib/planet/blog.rb', line 48 def on_fetch_success(feed) self.name ||= feed.title || 'the source' self.url ||= feed.url if self.url.nil? abort "#{ self. }'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 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. }" end end |
#sanitize_images(html) ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/planet/blog.rb', line 81 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) ⇒ Boolean
95 96 97 98 99 100 |
# File 'lib/planet/blog.rb', line 95 def whitelisted?(entry) return true if self.planet..empty? result = !(entry.categories & self.planet.).empty? puts "\t=> Ignored post titled: #{entry.title} with categories: [#{entry.categories.join(', ')}]" unless result result end |