-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilter.rb
134 lines (96 loc) · 2.87 KB
/
filter.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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
require 'ruby-debug'
require 'nokogiri'
module RSS
module Filters
def Filters.spawn(config)
type = config[:type].capitalize
klass = RSS::Filters::Basic
begin
klass = RSS::Filters.const_get(type)
rescue Exception
# 'S cool
end
klass.new(config)
end
class Basic
def initialize(config)
@config = config
end
def apply(raw_data)
filter = @config[:filter]
result = nil
data = extract(raw_data)
# Apply this filter
r = data.match(filter)
return if r.nil?
result = {:raw => data, :captures => r.captures, :filter => filter}
# Apply sub-filters
return result if @config[:sub_filters].nil?
sub_result = nil
sub_result_data = []
@config[:sub_filters].each do |sub_filter_config|
sub_filter = RSS::Filters.spawn(sub_filter_config)
this_result = sub_filter.apply(raw_data)
sub_result_data << this_result
if sub_result.nil?
sub_result = !this_result.nil?
else
sub_result = (!sub_result.nil? || !this_result.nil?)
end
end
unless sub_result.nil?
puts "this result=#{result}"
puts "sub result=#{sub_result}"
return nil unless sub_result
result[:sub_data] = sub_result_data
end
result
end
private
def extract(data)
new_data = ""
puts "Searching: #{@config[:type]}"
data.search(@config[:type]).each do |data|
new_data << data
end
new_data
end
def preprocess(data)
end
end
class Price < Basic
def apply(raw_data)
title = raw_data.at("title").inner_text
title =~ /.*?\$(\d*)$/
price = $1
return nil unless price
price = price.to_i
puts "Price = #{price}"
puts "Price below threshold: #{(price > @config[:threshold]) ? nil : true}"
pass = (price <= @config[:threshold])
return nil unless pass
{:price => price, :threshold => @config[:threshold]}
end
end
class Main < Basic
def apply(raw_data)
sub_result = nil
sub_result_data = []
@config[:filters].each do |sub_filter_config|
sub_filter = RSS::Filters.spawn(sub_filter_config)
this_result = sub_filter.apply(raw_data)
sub_result_data << this_result
if sub_result.nil?
sub_result = !this_result.nil?
else
sub_result = (!sub_result.nil? || !this_result.nil?)
end
end
sub_result_data.compact!
return nil if sub_result_data.size == 0
return nil unless sub_result # could be false
sub_result_data
end
end
end
end