Class: RssNotifier::App

Inherits:
Object
  • Object
show all
Defined in:
lib/rss_notifier/app.rb

Constant Summary collapse

CONFIG_DIR =
Pathname.new('.')
CONFIG_FILE =
CONFIG_DIR.join('config.yml')
DB_FILE =
CONFIG_DIR.join('db.yml')

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(notify: false) ⇒ App

Returns a new instance of App.



38
39
40
41
42
43
44
45
46
47
# File 'lib/rss_notifier/app.rb', line 38

def initialize(notify: false)
  @config = RssNotifier::Config.load(CONFIG_FILE)
  @db = RssNotifier::Db.load(DB_FILE)
  @notify = []
  @options = {
    notify: notify
  }

  setup_notify(@config.notify)
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



18
19
20
# File 'lib/rss_notifier/app.rb', line 18

def options
  @options
end

Class Method Details

.init(force: false) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/rss_notifier/app.rb', line 21

def self.init(force: false)
  if !Dir.exists?(CONFIG_DIR.to_s) || force
    RssNotifier.logger.info("Creating #{CONFIG_DIR}")
    FileUtils.mkdir_p(CONFIG_DIR.to_s)
  else
    RssNotifier.logger.info("Directory #{CONFIG_DIR} already exists, skipping")
  end

  if !File.exists?(CONFIG_FILE) || force
    RssNotifier.logger.info("Creating #{CONFIG_FILE}")
    config = RssNotifier::Config.default
    config.save_to(CONFIG_FILE)
  else
    RssNotifier.logger.info("File #{CONFIG_FILE} already exists, skipping")
  end
end

Instance Method Details

#check_rss!Object



69
70
71
72
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
100
101
102
103
104
105
# File 'lib/rss_notifier/app.rb', line 69

def check_rss!
  RssNotifier.logger.info "Checking #{@config.rss_urls.size} urls"

  @config.rss_urls.each do |url_object|
    title, url = url_object[:title], url_object[:url]
    raw = HTTP.get(url).to_s
    feed = RSS::Parser.parse(raw)
    items = []
    feed.items.each do |item|
      items << Item.new({
        rss_url: url,
        rss_title: title,
        link: item.link,
        title: item.title,
        description: item.description,
        date: item.date
      })
    end
    @db.update(items)
  end
  changed_items = @db.changed_items
  is_changed = @db.changed?
  @db.save

  unless is_changed
    RssNotifier.logger.info 'No changes'
    return
  end

  if options[:notify]
    RssNotifier.logger.info "#{changed_items.size} items changed, notifing..."

    changed_items.each do |item|
      notify!(item)
    end
  end
end

#notify!(item) ⇒ Object



107
108
109
110
111
112
113
114
115
116
# File 'lib/rss_notifier/app.rb', line 107

def notify!(item)
  @notify.each do |notify|
    begin
      notify.notify(item)
    rescue => e
      puts "#{e}: #{item.link} | #{notify}"
      puts e.backtrace
    end
  end
end

#runObject



62
63
64
65
66
67
# File 'lib/rss_notifier/app.rb', line 62

def run
  loop do
    check_rss!
    sleep @config.period_in_minutes * 60
  end
end

#setup_notify(notify) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/rss_notifier/app.rb', line 49

def setup_notify(notify)
  notify.each do |d|
    next unless d[:enabled]
    if d[:adapter] == 'email'
      @notify << Adapter::Email.new(d[:email])
    elsif d[:adapter] == 'pushbullet'
      @notify << Adapter::Pushbullet.new(d[:name], d[:access_token])
    else
      raise "Unknown adapter #{d[:adapter]}"
    end
  end
end