Module: Vnews::Config

Defined in:
lib/vnews/config.rb

Constant Summary collapse

CONFIGPATH =
"#{ENV['HOME']}/.vnewsrc"

Class Method Summary collapse

Class Method Details

.generate_configObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/vnews/config.rb', line 12

def self.generate_config
  config = Vnews.sql_client.config.inject({}) do |memo, (k, v)|
    memo[k.to_s] = v
    memo
  end
  out = [config.to_yaml.sub(/---\s+/, ''), '']
  Vnews.sql_client.configured_folders.map do |x| 
    folder = x["folder"]
    out << folder
    Vnews.sql_client.feeds_in_folder(folder).each do |feed|
      out <<  feed
    end
    out << ""
  end
  out.join("\n")
end

.load_configObject



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

def self.load_config
  if ! File.exists?(File.expand_path(CONFIGPATH))
    return false
  end
  return if $sql_client
  f = File.read(File.expand_path(CONFIGPATH))
  top, bottom = f.split(/^\s*$/,2)
  dbconfig = YAML::load top
  Sql.new(dbconfig)
end

.rewrite_configObject



29
30
31
32
# File 'lib/vnews/config.rb', line 29

def self.rewrite_config
  out = generate_config
  f = File.open(File.expand_path(CONFIGPATH), 'w') {|f| f.write(out)}
end

.stub_configObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/vnews/config.rb', line 34

def self.stub_config
  stub = <<-END.gsub(/^ */, '')
    host: localhost 
    database: vnews 
    username: root 
    password: 

    General News 
    http://feedproxy.google.com/economist/full_print_edition
    http://feeds.feedburner.com/TheAtlanticWire

    Humor
    http://feed.dilbert.com/dilbert/blog

    Tech 
    http://rss.slashdot.org/Slashdot/slashdot
    http://feeds2.feedburner.com/readwriteweb
    http://feedproxy.google.com/programmableweb
    http://news.ycombinator.com/rss
    http://daringfireball.net/index.xml
    http://dailyvim.blogspot.com/feeds/posts/default
  END
end

.update_foldersObject



60
61
62
63
64
65
66
67
68
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/vnews/config.rb', line 60

def self.update_folders
  f = File.read(File.expand_path(CONFIGPATH))
  db, list = f.split(/^\s*$/,2)
  current_folder = nil
  ff = []
  list.split("\n").each do |line|
    line = line.strip
    if line =~ /^\s*http/ # feed
      ff << [line, current_folder]
    elsif line =~ /^\s*\w+/ # folder
      current_folder = line
    end
  end
  puts "Using feeds and folders: #{ff.inspect}"

  old_feeds = Vnews.sql_client.feeds(0).map {|x| x["feed_url"]}
  new_feeds = ff.map {|feed,folder| feed}
  rm_feeds = old_feeds - new_feeds
  puts "Removing feeds: #{rm_feeds.inspect}"
  rm_feeds.each {|x| Vnews.sql_client.delete_feed(x)}

  # ff is an association between a feed and a folder
  old_ff = Vnews.sql_client.configured_feeds_folders 
  rm_ff = old_ff - ff
  puts "Removing feed-folder associations: #{rm_ff.inspect}"
  rm_ff.each {|feed,folder| Vnews.sql_client.delete_feed_folder(feed,folder)}

  puts "Adding feeds: #{(new_feeds - old_feeds).inspect}"
  puts "Adding folder-feed associations: #{(ff - old_ff).inspect}"
  ff.each do |feed_url, folder|
    begin
      Timeout::timeout(Vnews::TIMEOUT) do
        feed_url, f, folder = *Vnews::Feed.fetch_feed(feed_url, folder)
        folder ||= "Misc"
        if f.nil?
          $stderr.print "\nNo feed found for #{feed_url}\n"
        else
          Vnews::Feed.save_feed(feed_url, f, folder)
        end
      end
    rescue Timeout::Error
      puts "TIMEOUT ERROR: #{feed_url}"
    end
  end
  $stderr.puts "\nDone."
end