Skip to content

Commit

Permalink
putting together notification subscription model
Browse files Browse the repository at this point in the history
  • Loading branch information
flyingmachine committed Nov 28, 2012
1 parent 5515319 commit a8230be
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 7 deletions.
1 change: 1 addition & 0 deletions Gemfile
Expand Up @@ -10,6 +10,7 @@ group :development do
gem 'ruby-debug-base19', '0.11.23' if RUBY_VERSION.include? '1.9.1'
gem 'ruby-debug19', :platforms => :ruby_19
gem 'ruby-debug', :platforms => :mri_18
gem 'pry'
end
end

Expand Down
8 changes: 8 additions & 0 deletions Gemfile.lock
Expand Up @@ -54,6 +54,7 @@ GEM
xpath (~> 0.1.4)
childprocess (0.3.0)
ffi (~> 1.0.6)
coderay (1.0.8)
columnize (0.3.6)
diff-lcs (1.1.3)
erubis (2.7.0)
Expand All @@ -78,6 +79,7 @@ GEM
i18n (>= 0.4.0)
mime-types (~> 1.16)
treetop (~> 1.4.8)
method_source (0.8.1)
mime-types (1.18)
mongo (1.6.1)
bson (~> 1.6.1)
Expand All @@ -88,6 +90,10 @@ GEM
multi_json (1.0.4)
nokogiri (1.5.0)
polyglot (0.3.3)
pry (0.9.10)
coderay (~> 1.0.5)
method_source (~> 0.8)
slop (~> 3.3.1)
rack (1.4.1)
rack-cache (1.2)
rack (>= 0.4)
Expand Down Expand Up @@ -149,6 +155,7 @@ GEM
ffi (~> 1.0.9)
multi_json (~> 1.0.4)
rubyzip
slop (3.3.3)
sprockets (2.1.2)
hike (~> 1.2)
rack (~> 1.0)
Expand All @@ -170,6 +177,7 @@ DEPENDENCIES
capybara (>= 0.4.0)
fabrication
faker
pry
rake
rspec-rails
ruby-debug
Expand Down
12 changes: 12 additions & 0 deletions app/models/whoops/filter.rb
Expand Up @@ -7,6 +7,8 @@ class Whoops::Filter
FILTERED_FIELDS.each do |document_field|
field document_field, :type => Array
end

belongs_to :filterable, :polymorphic => true

def to_query_document
doc = attributes.except(:_id, "_id").delete_if{|k, v| v.blank?}
Expand All @@ -18,6 +20,16 @@ def to_query_document
end
end

def matches_event_group?(event_group)
FILTERED_FIELDS.all? do |field|
if self.send(field).blank?
true
else
/^(#{self.send(field).join("|")})$/ =~ event_group.send(field)
end
end
end

class << self
def new_from_params(params)
if params
Expand Down
27 changes: 27 additions & 0 deletions app/models/whoops/notification_subscription.rb
@@ -0,0 +1,27 @@
class Whoops::NotificationSubscription
include Mongoid::Document

has_one :filter, :as => :filterable, :class_name => "Whoops::Filter"
validates_presence_of :email

field :email, :type => String

before_save :downcase_email

def downcase_email
self.email.downcase!
end

class Matcher
attr_accessor :event_group

# @param [ Whoops::EventGroup ]
def initialize(event_group)
self.event_group = event_group
end

def matching_emails
Whoops::NotificationSubscription.all.select{ |ns| ns.filter.matches_event_group?(self.event_group) }.collect(&:email)
end
end
end
7 changes: 0 additions & 7 deletions app/views/notification_subscriptions/index.html.haml
Expand Up @@ -4,18 +4,11 @@
.space
= render :partial => "form"



%table
%tr
%th Email Address
%th Services to Monitor
%th

- @notification_rules.each do |nr|
%tr
%td= nr.email
%td= nr.matchers.sort.join(", ")
%td= link_to "update", edit_whoops_notification_rule_path(nr)


18 changes: 18 additions & 0 deletions spec/models/whoops/filter_spec.rb
Expand Up @@ -8,4 +8,22 @@
keys.should_not include("_id")
end
end

describe "#matches_event_group?" do
let(:filter) { Whoops::Filter.new(:service => ["app.web"]) }
let(:event_group) { Whoops::EventGroup.new(:service => "app.web", :event_type => "info") }
it "should match an event group when the filters match the event group fields" do
filter.matches_event_group?(event_group).should be_true
end

it "should match .* filters" do
filter.service = ["app.*"]
filter.matches_event_group?(event_group).should be_true
end

it "should not match an event group when the filters are not equal to the event group fields" do
filter.service = ["app.queue"]
filter.matches_event_group?(event_group).should be_false
end
end
end
33 changes: 33 additions & 0 deletions spec/models/whoops/notification_subscription_spec.rb
@@ -0,0 +1,33 @@
require 'spec_helper'

describe Whoops::NotificationSubscription do
let(:subscription) {
Whoops::NotificationSubscription.create(
:email => "Daniel@Higginbotham.com",
:filter => Whoops::Filter.new(:service => ["test.*"])
)
}
let(:event_params){ Whoops::Spec::ATTRIBUTES[:event_params] }
let(:event){ Whoops::Event.record(event_params) }
let(:event_group){ event.event_group }

it "downcases the email on save" do
subscription.email.should == "daniel@higginbotham.com"
end

describe Whoops::NotificationSubscription::Matcher do
let(:matcher){ Whoops::NotificationSubscription::Matcher.new(event_group) }

it "should return the email addresses of subscriptions whose filters match an event group" do
subscription.filter.save

nomatch = Whoops::NotificationSubscription.create(
:email => "does_not@match.com",
:filter => Whoops::Filter.new(:service => ["not_test.*"])
)
nomatch.filter.save

matcher.matching_emails.should == ["daniel@higginbotham.com"]
end
end
end
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Expand Up @@ -5,6 +5,7 @@
require "rails/test_help"
require "rspec/rails"
require "fabrication"
require "pry"

ActionMailer::Base.delivery_method = :test
ActionMailer::Base.perform_deliveries = true
Expand Down

0 comments on commit a8230be

Please sign in to comment.