Class: Heathrow::Sources::RSS

Inherits:
Base
  • Object
show all
Defined in:
lib/heathrow/sources/rss.rb

Instance Attribute Summary

Attributes inherited from Base

#config, #db, #name, #type

Instance Method Summary collapse

Methods inherited from Base

#enabled?, #last_fetch, #poll_interval, #save_config, #update_last_fetch

Constructor Details

#initialize(name, config, db) ⇒ RSS

Returns a new instance of RSS.



10
11
12
13
14
# File 'lib/heathrow/sources/rss.rb', line 10

def initialize(name, config, db)
  super
  # feeds can be simple URLs or hashes with {url:, title:, tags:}
  @feeds = config['feeds'] || []
end

Instance Method Details

#add_feed(url, title: nil, tags: []) ⇒ Object



56
57
58
59
60
61
# File 'lib/heathrow/sources/rss.rb', line 56

def add_feed(url, title: nil, tags: [])
  entry = { 'url' => url, 'title' => title, 'tags' => tags }
  @feeds << entry unless @feeds.any? { |f| feed_url(f) == url }
  @config['feeds'] = @feeds
  save_config
end

#fetchObject

Legacy interface for source_manager



47
48
49
50
51
52
53
54
# File 'lib/heathrow/sources/rss.rb', line 47

def fetch
  return [] unless enabled?
  source = @db.get_source_by_name(@name)
  return [] unless source
  sync(source['id'])
  update_last_fetch
  []
end

#list_feedsObject



69
70
71
# File 'lib/heathrow/sources/rss.rb', line 69

def list_feeds
  @feeds.map { |f| { url: feed_url(f), title: feed_title(f), tags: feed_tags(f) } }
end

#remove_feed(url) ⇒ Object



63
64
65
66
67
# File 'lib/heathrow/sources/rss.rb', line 63

def remove_feed(url)
  @feeds.reject! { |f| feed_url(f) == url }
  @config['feeds'] = @feeds
  save_config
end

#sync(source_id) ⇒ Object

Sync all feeds into the database. Called from application.rb on refresh.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/heathrow/sources/rss.rb', line 17

def sync(source_id)
  count = 0
  changed = false
  @feeds.each do |feed_entry|
    url, title, tags = parse_feed_entry(feed_entry)
    begin
      n = sync_feed(source_id, url, title, tags)
      count += n
      if feed_entry.is_a?(Hash)
        feed_entry['last_status'] = 'ok'
        feed_entry['last_sync'] = Time.now.to_i
        changed = true
      end
    rescue => e
      STDERR.puts "RSS error #{url}: #{e.message}" if ENV['DEBUG']
      if feed_entry.is_a?(Hash)
        feed_entry['last_status'] = "error: #{e.message}"[0..120]
        feed_entry['last_sync'] = Time.now.to_i
        changed = true
      end
    end
  end
  if changed
    @config['feeds'] = @feeds
    save_config
  end
  count
end

#test_connection(&progress) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/heathrow/sources/rss.rb', line 73

def test_connection(&progress)
  ok = 0
  fail_names = []
  fail_details = []
  @feeds.each_with_index do |f, i|
    url = feed_url(f)
    title = feed_title(f) || url
    progress.call("Testing #{i+1}/#{@feeds.size}: #{title}") if progress
    begin
      data = http_get(url)
      if data && !data.empty?
        ok += 1
      else
        fail_names << title
        fail_details << title
      end
    rescue => e
      fail_names << title
      fail_details << "#{title}: #{e.message[0..50]}"
    end
  end
  if fail_names.empty?
    { success: true, message: "All #{ok} feeds OK" }
  else
    { success: false, message: "#{ok}/#{@feeds.size} OK. Failed: #{fail_details.join(', ')}", failed_feeds: fail_names }
  end
end