Class: Mwkrom::BlogReader
- Inherits:
-
Object
- Object
- Mwkrom::BlogReader
- Defined in:
- lib/mwkrom/blog_reader.rb
Overview
Copyright© MW Krom LLC, All Rights Reserved
Instance Attribute Summary collapse
-
#expires_in ⇒ Object
Returns the value of attribute expires_in.
-
#feed_url ⇒ Object
Returns the value of attribute feed_url.
-
#key ⇒ Object
Returns the value of attribute key.
Instance Method Summary collapse
- #articles_for(tags) ⇒ Object
- #internal_articles_for(tags) ⇒ Object
- #json_articles_for(tags) ⇒ Object
- #json_internal_articles_for(tags) ⇒ Object
- #latest_feed_dom ⇒ Object
- #latest_feed_xml ⇒ Object
- #use_expires_in ⇒ Object
Instance Attribute Details
#expires_in ⇒ Object
Returns the value of attribute expires_in.
6 7 8 |
# File 'lib/mwkrom/blog_reader.rb', line 6 def expires_in @expires_in end |
#feed_url ⇒ Object
Returns the value of attribute feed_url.
6 7 8 |
# File 'lib/mwkrom/blog_reader.rb', line 6 def feed_url @feed_url end |
#key ⇒ Object
Returns the value of attribute key.
6 7 8 |
# File 'lib/mwkrom/blog_reader.rb', line 6 def key @key end |
Instance Method Details
#articles_for(tags) ⇒ Object
12 13 14 |
# File 'lib/mwkrom/blog_reader.rb', line 12 def articles_for() ActiveSupport::JSON.decode(json_articles_for()) end |
#internal_articles_for(tags) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/mwkrom/blog_reader.rb', line 28 def internal_articles_for() d = latest_feed_dom if d.blank? # bad connection; empty data; etc. return [] end items = (d/:item).select do |item| .empty? || ((item/:category).any? {|x| .include?(x.inner_text)}) end items.map do |item| {:url => item.at('link').inner_html, :title => item.at('title').inner_html, :pubDate => (item.at('pubDate') && item.at('pubDate').inner_html), :creator => (item.at('dc:creator') && item.at('dc:creator').inner_html), :description => item.at('description').inner_text} end end |
#json_articles_for(tags) ⇒ Object
16 17 18 19 20 21 22 |
# File 'lib/mwkrom/blog_reader.rb', line 16 def json_articles_for() tkey = .join(":").gsub(' ', '') cache_key = "blog_reader_tags:#{tkey}:#{key}" Rails.cache.fetch(cache_key, :expires_in => use_expires_in) do json_internal_articles_for() end end |
#json_internal_articles_for(tags) ⇒ Object
24 25 26 |
# File 'lib/mwkrom/blog_reader.rb', line 24 def json_internal_articles_for() internal_articles_for().to_json end |
#latest_feed_dom ⇒ Object
57 58 59 |
# File 'lib/mwkrom/blog_reader.rb', line 57 def latest_feed_dom latest_feed_xml.present? && Hpricot::XML(latest_feed_xml) end |
#latest_feed_xml ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/mwkrom/blog_reader.rb', line 45 def latest_feed_xml cache_key = "blog_reader_all:#{key}" Rails.cache.fetch(cache_key, :expires_in => use_expires_in) do begin res = Net::HTTP.get_response(URI.parse(feed_url)) res && res.body rescue Errno::ECONNREFUSED => e "" end end end |
#use_expires_in ⇒ Object
8 9 10 |
# File 'lib/mwkrom/blog_reader.rb', line 8 def use_expires_in expires_in || 10.minutes end |