Class: APDM::Feed
- Inherits:
- 
      Object
      
        - Object
- APDM::Feed
 
- Defined in:
- lib/apdm/feed.rb
Instance Attribute Summary collapse
- 
  
    
      #channel  ⇒ Object 
    
    
  
  
  
  
    
      readonly
    
    
  
  
  
  
  
  
    Returns the value of attribute channel. 
Instance Method Summary collapse
- #cache_key(options = {}) ⇒ Object
- #fetch(options = {}) ⇒ Object
- 
  
    
      #initialize(channel)  ⇒ Feed 
    
    
  
  
  
    constructor
  
  
  
  
  
  
  
    A new instance of Feed. 
- #sanitize(entry) ⇒ Object
- #sanitize_field(field) ⇒ Object
Constructor Details
#initialize(channel) ⇒ Feed
Returns a new instance of Feed.
| 20 21 22 | # File 'lib/apdm/feed.rb', line 20 def initialize(channel) @channel = channel end | 
Instance Attribute Details
#channel ⇒ Object (readonly)
Returns the value of attribute channel.
| 19 20 21 | # File 'lib/apdm/feed.rb', line 19 def channel @channel end | 
Instance Method Details
#cache_key(options = {}) ⇒ Object
| 58 59 60 61 62 | # File 'lib/apdm/feed.rb', line 58 def cache_key( = {}) pieces = ["apdm", "rss", channel.label] pieces += .sort_by { |key, value| key }.map { |key, value| "#{key}:#{value}" } pieces.join('.') end | 
#fetch(options = {}) ⇒ Object
| 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | # File 'lib/apdm/feed.rb', line 24 def fetch( = {}) feed_as_json = APDM.cache.get cache_key return JSON.parse(feed_as_json) if feed_as_json result = Feedzirra::Feed.fetch_and_parse(channel.feed_url()) # The API will return a 400 response in the case of a non-existant section id return nil unless result.is_a?(Feedzirra::Parser::RSS) feed_entries = result.entries.map do |entry| sanitize entry end.compact APDM.cache.set cache_key, feed_entries.to_json, 60*5 return feed_entries end | 
#sanitize(entry) ⇒ Object
| 42 43 44 45 46 47 48 49 50 51 52 | # File 'lib/apdm/feed.rb', line 42 def sanitize(entry) { "title" => sanitize_field(entry.title), "url" => sanitize_field(entry.url), "author" => sanitize_field(entry.), "published" => entry.published, "image" => sanitize_field(entry.image_url), "content" => sanitize_field(entry.body), "summary" => sanitize_field(entry.summary) } end | 
#sanitize_field(field) ⇒ Object
| 54 55 56 | # File 'lib/apdm/feed.rb', line 54 def sanitize_field(field) field ? field.encode(Encoding::UTF_8).sanitize : nil end |