diff --git a/Gemfile b/Gemfile index cac1de1..4c7d9c0 100644 --- a/Gemfile +++ b/Gemfile @@ -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 diff --git a/Gemfile.lock b/Gemfile.lock index 9b520e3..3f9e50e 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) @@ -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) @@ -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) @@ -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) @@ -170,6 +177,7 @@ DEPENDENCIES capybara (>= 0.4.0) fabrication faker + pry rake rspec-rails ruby-debug diff --git a/app/models/whoops/filter.rb b/app/models/whoops/filter.rb index db45fad..b82da0c 100644 --- a/app/models/whoops/filter.rb +++ b/app/models/whoops/filter.rb @@ -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?} @@ -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 diff --git a/app/models/whoops/notification_subscription.rb b/app/models/whoops/notification_subscription.rb new file mode 100644 index 0000000..18fff2a --- /dev/null +++ b/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 diff --git a/app/views/notification_subscriptions/index.html.haml b/app/views/notification_subscriptions/index.html.haml index b5c0a56..88bd313 100644 --- a/app/views/notification_subscriptions/index.html.haml +++ b/app/views/notification_subscriptions/index.html.haml @@ -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) \ No newline at end of file diff --git a/spec/models/whoops/filter_spec.rb b/spec/models/whoops/filter_spec.rb index 19befbe..c540fcc 100644 --- a/spec/models/whoops/filter_spec.rb +++ b/spec/models/whoops/filter_spec.rb @@ -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 diff --git a/spec/models/whoops/notification_subscription_spec.rb b/spec/models/whoops/notification_subscription_spec.rb new file mode 100644 index 0000000..311c8e6 --- /dev/null +++ b/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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 29ba8df..c0af3d6 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -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