-
Notifications
You must be signed in to change notification settings - Fork 0
/
choppa.rb
68 lines (54 loc) · 1.62 KB
/
choppa.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# coding: utf-8
require 'rubygems'
require 'nokogiri'
class ChoppaProcessor
attr_reader :doc
def initialize(doc)
@doc = doc
end
def process!
@doc.xpath('//*[@text="[daily]"]/*[@type="rss"]').each do |feed|
daily_groups.each {|node| node.add_child(feed.clone)}
end
offset = 0
@doc.xpath('//*[@text="[twice weekly]"]/*[@type="rss"]').each do |feed|
remove_from_all_groups(feed)
twice_weekly_groups(offset).each {|node| node.add_child(feed.clone)}
offset = (offset + 1) % 7
end
@doc.xpath('//*[@text="[every other day]"]/*[@type="rss"]').each do |feed|
remove_from_all_groups(feed)
every_other_day_groups(offset).each {|node| node.add_child(feed.clone)}
offset = (offset + 1) % 7
end
end
private
def daily_groups
days.map do |name|
find_or_create_group(name)
end
end
def twice_weekly_groups(offset)
days.values_at(offset, (3 + offset) % 7).map do |name|
find_or_create_group(name)
end
end
def remove_from_all_groups(feed)
daily_groups.each do |node|
node.xpath("//outline[@htmlUrl='#{feed['htmlUrl']}']").remove
end
end
def every_other_day_groups(offset)
days.values_at(*((0..3).map {|i| (i * 2 + offset) % 7})).map do |name|
find_or_create_group(name)
end
end
def find_or_create_group(name)
@doc.at(%{//*[@text="#{name}"]}) || @doc.at('body').add_child(
%{<outline text="#{name}" title="#{name}" />}).first
end
def days(*pick)
@days ||= (1..7).zip(%w(Monday Tuesday Wednesday Thursday Friday
Saturday Sunday)).map {|d| d.join(' ')}
end
end