Skip to content

Commit

Permalink
Added merge mode
Browse files Browse the repository at this point in the history
  • Loading branch information
chriseidhof committed Nov 29, 2014
1 parent 4f5ace5 commit b2218df
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
12 changes: 7 additions & 5 deletions app/models/agents/website_agent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def validate_options

# Check for optional fields
if options['mode'].present?
errors.add(:base, "mode must be set to on_change or all") unless %w[on_change all].include?(options['mode'])
errors.add(:base, "mode must be set to on_change, all or merge") unless %w[on_change all merge].include?(options['mode'])
end

if options['expected_update_period_in_days'].present?
Expand Down Expand Up @@ -226,10 +226,12 @@ def receive(incoming_events)
incoming_events.each do |event|
interpolate_with(event) do
url_to_scrape = event.payload['url']
valid_url = url_to_scrape =~ /^https?:\/\//i
docs = valid_url ? check_url(url_to_scrape) : []
docs = []
docs = check_url(url_to_scrape) if url_to_scrape =~ /^https?:\/\//i
docs.each do |doc|
create_event payload: doc
new_payload = interpolated['mode'].to_s == "merge" ? event.payload.dup : {}
new_payload.merge! doc
create_event payload: new_payload
end
end
end
Expand All @@ -252,7 +254,7 @@ def store_payload!(old_events, result)
end
end
true
when 'all', ''
when 'all', 'merge', ''
true
else
raise "Illegal options[mode]: #{interpolated['mode']}"
Expand Down
10 changes: 10 additions & 0 deletions spec/models/agents/website_agent_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,16 @@

expect(Event.last.payload['response_info']).to eq('The reponse from XKCD was 200 OK.')
end

it "should support merging of events" do
expect {
@checker.options = @valid_options
@checker.options[:mode] = "merge"
@checker.receive([@event])
}.to change { Event.count }.by(1)
last_payload = Event.last.payload
expect(last_payload['link']).to eq('Random')
end
end
end

Expand Down

0 comments on commit b2218df

Please sign in to comment.