Class: Mwkrom::BlogReader

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

Overview

Copyright© MW Krom LLC, All Rights Reserved

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#expires_inObject

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_urlObject

Returns the value of attribute feed_url.



6
7
8
# File 'lib/mwkrom/blog_reader.rb', line 6

def feed_url
  @feed_url
end

#keyObject

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(tags)
  ActiveSupport::JSON.decode(json_articles_for(tags))
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(tags)
  d = latest_feed_dom
  if d.blank?  # bad connection; empty data; etc.
    return []
  end
  items = (d/:item).select do |item|
    tags.empty? || ((item/:category).any? {|x| tags.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(tags)
  tkey = tags.join(":").gsub(' ', '')
  cache_key = "blog_reader_tags:#{tkey}:#{key}"
  Rails.cache.fetch(cache_key, :expires_in => use_expires_in) do
    json_internal_articles_for(tags)
  end
end

#json_internal_articles_for(tags) ⇒ Object



24
25
26
# File 'lib/mwkrom/blog_reader.rb', line 24

def json_internal_articles_for(tags)
  internal_articles_for(tags).to_json
end

#latest_feed_domObject



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_xmlObject



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_inObject



8
9
10
# File 'lib/mwkrom/blog_reader.rb', line 8

def use_expires_in
  expires_in || 10.minutes
end