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.
-
#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.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/planet/blog.rb', line 9 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] # 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 |
#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
24 25 26 27 28 29 30 31 32 |
# File 'lib/planet/blog.rb', line 24 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
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 |
# File 'lib/planet/blog.rb', line 34 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 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 ) puts "=> Found post titled #{ @post.title } - by #{ @post.blog. }" end end |
#sanitize_images(html) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/planet/blog.rb', line 64 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
78 79 80 81 82 83 |
# File 'lib/planet/blog.rb', line 78 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 |