Class: Heathrow::Sources::Webpage

Inherits:
Base
  • Object
show all
Defined in:
lib/heathrow/sources/webpage.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) ⇒ Webpage

Returns a new instance of Webpage.



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

def initialize(name, config, db)
  super
  @pages = config['pages'] || []
  @snapshots_dir = File.join(Dir.home, '.heathrow', 'webwatch')
  Dir.mkdir(@snapshots_dir) unless Dir.exist?(@snapshots_dir)
end

Instance Method Details

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



38
39
40
41
42
43
# File 'lib/heathrow/sources/webpage.rb', line 38

def add_page(url, title: nil, selector: nil, tags: [])
  entry = { 'url' => url, 'title' => title, 'selector' => selector, 'tags' => tags }
  @pages << entry unless @pages.any? { |p| p['url'] == url }
  @config['pages'] = @pages
  save_config
end

#fetchObject



29
30
31
32
33
34
35
36
# File 'lib/heathrow/sources/webpage.rb', line 29

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

#list_pagesObject



51
52
53
# File 'lib/heathrow/sources/webpage.rb', line 51

def list_pages
  @pages.map { |p| { url: p['url'], title: p['title'], selector: p['selector'], tags: p['tags'] || [] } }
end

#remove_page(url) ⇒ Object



45
46
47
48
49
# File 'lib/heathrow/sources/webpage.rb', line 45

def remove_page(url)
  @pages.reject! { |p| p['url'] == url }
  @config['pages'] = @pages
  save_config
end

#sync(source_id) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/heathrow/sources/webpage.rb', line 17

def sync(source_id)
  count = 0
  @pages.each do |page|
    begin
      count += check_page(source_id, page)
    rescue => e
      STDERR.puts "Webwatch error #{page['url']}: #{e.message}" if ENV['DEBUG']
    end
  end
  count
end