Enable include_sort_info in RssAgent

Spotted in #1916.
This commit is contained in:
Akinori MUSHA 2017-03-07 18:14:50 +09:00
parent e5776f206e
commit 98b11ddb80
3 changed files with 31 additions and 26 deletions

View file

@ -70,6 +70,23 @@ module SortableEvents
boolify(interpolated['include_sort_info'])
end
def create_events(events)
if include_sort_info?
count = events.count
events.each.with_index(1) do |event, position|
event.payload[:sort_info] = {
position: position,
count: count
}
create_event(event)
end
else
events.each do |event|
create_event(event)
end
end
end
module AutomaticSorter
def check
return super unless events_order || include_sort_info?
@ -105,20 +122,7 @@ module SortableEvents
yield
ensure
events, @sortable_events = sort_events(@sortable_events), nil
if include_sort_info?
count = events.count
events.each.with_index(1) do |event, position|
event.payload[:sort_info] = {
position: position,
count: count
}
create_event(event)
end
else
events.each do |event|
create_event(event)
end
end
create_events(events)
end
end

View file

@ -169,17 +169,12 @@ module Agents
end
end
created_event_count = 0
sort_events(new_events).each.with_index do |event, index|
entry_id = event.payload[:id]
if check_and_track(entry_id)
unless max_events && max_events > 0 && index >= max_events
created_event_count += 1
create_event(event)
end
end
end
log "Fetched #{urls.to_sentence} and created #{created_event_count} event(s)."
events = sort_events(new_events).select.with_index { |event, index|
check_and_track(event.payload[:id]) &&
!(max_events && max_events > 0 && index >= max_events)
}
create_events(events)
log "Fetched #{urls.to_sentence} and created #{events.size} event(s)."
end
def check_and_track(entry_id)

View file

@ -61,6 +61,7 @@ describe Agents::RssAgent do
describe "emitting RSS events" do
it "should emit items as events for an Atom feed" do
agent.options['include_feed_info'] = true
agent.options['include_sort_info'] = true
expect {
agent.check
@ -68,7 +69,7 @@ describe Agents::RssAgent do
first, *, last = agent.events.last(20)
[first, last].each do |event|
expect(first.payload['feed']).to include({
expect(event.payload['feed']).to include({
"type" => "atom",
"title" => "Recent Commits to huginn:master",
"url" => "https://github.com/cantino/huginn/commits/master",
@ -98,6 +99,7 @@ describe Agents::RssAgent do
expect(first.payload['authors']).to eq(["cantino (https://github.com/cantino)"])
expect(first.payload['date_published']).to be_nil
expect(first.payload['last_updated']).to eq("2014-07-16T22:26:22-07:00")
expect(first.payload['sort_info']).to eq({ 'position' => 20, 'count' => 20 })
expect(last.payload['url']).to eq("https://github.com/cantino/huginn/commit/d465158f77dcd9078697e6167b50abbfdfa8b1af")
expect(last.payload['urls']).to eq(["https://github.com/cantino/huginn/commit/d465158f77dcd9078697e6167b50abbfdfa8b1af"])
expect(last.payload['links']).to eq([
@ -110,11 +112,13 @@ describe Agents::RssAgent do
expect(last.payload['authors']).to eq(["CloCkWeRX (https://github.com/CloCkWeRX)"])
expect(last.payload['date_published']).to be_nil
expect(last.payload['last_updated']).to eq("2014-07-01T16:37:47+09:30")
expect(last.payload['sort_info']).to eq({ 'position' => 1, 'count' => 20 })
end
it "should emit items as events in the order specified in the events_order option" do
expect {
agent.options['events_order'] = ['{{title | replace_regex: "^[[:space:]]+", "" }}']
agent.options['include_sort_info'] = true
agent.check
}.to change { agent.events.count }.by(20)
@ -122,9 +126,11 @@ describe Agents::RssAgent do
expect(first.payload['title'].strip).to eq('upgrade rails and gems')
expect(first.payload['url']).to eq("https://github.com/cantino/huginn/commit/87a7abda23a82305d7050ac0bb400ce36c863d01")
expect(first.payload['urls']).to eq(["https://github.com/cantino/huginn/commit/87a7abda23a82305d7050ac0bb400ce36c863d01"])
expect(first.payload['sort_info']).to eq({ 'position' => 20, 'count' => 20 })
expect(last.payload['title'].strip).to eq('Dashed line in a diagram indicates propagate_immediately being false.')
expect(last.payload['url']).to eq("https://github.com/cantino/huginn/commit/0e80f5341587aace2c023b06eb9265b776ac4535")
expect(last.payload['urls']).to eq(["https://github.com/cantino/huginn/commit/0e80f5341587aace2c023b06eb9265b776ac4535"])
expect(last.payload['sort_info']).to eq({ 'position' => 1, 'count' => 20 })
end
it "should emit items as events for a FeedBurner RSS 2.0 feed" do