class Flapjack::Data::Contact

Public Class Methods

jsonapi_associations() click to toggle source
# File lib/flapjack/data/contact.rb, line 258
def self.jsonapi_associations
  unless instance_variable_defined?('@jsonapi_associations')
    @jsonapi_associations = {
      :checks => Flapjack::Gateways::JSONAPI::Data::JoinDescriptor.new(
        :get => true,
        :number => :multiple, :link => true, :includable => true,
        :type => 'check',
        :klass => Flapjack::Data::Check,
        :descriptions => {
          :get => "Returns checks which this contact's notification " \
                  "rules allow it to receive notifications."
        }
      ),
      :media => Flapjack::Gateways::JSONAPI::Data::JoinDescriptor.new(
        :get => true,
        :number => :multiple, :link => true, :includable => true,
        :descriptions => {
          :get => "Returns media belonging to the contact."
        }
      ),
      :rules => Flapjack::Gateways::JSONAPI::Data::JoinDescriptor.new(
        :get => true,
        :number => :multiple, :link => true, :includable => true,
        :descriptions => {
          :get => "Returns rules belonging to the contact."
        }
      ),
      :tags => Flapjack::Gateways::JSONAPI::Data::JoinDescriptor.new(
        :post => true, :get => true, :patch => true, :delete => true,
        :number => :multiple, :link => true, :includable => true,
        :descriptions => {
          :post => "Associate tags with this contact.",
          :get => "Returns all tags linked to this contact.",
          :patch => "Update the tags associated with this contact.",
          :delete => "Delete associations between tags and this contact."
        }
      )
    }
    populate_association_data(@jsonapi_associations)
  end
  @jsonapi_associations
end
jsonapi_methods() click to toggle source
# File lib/flapjack/data/contact.rb, line 226
def self.jsonapi_methods
  @jsonapi_methods ||= {
    :post => Flapjack::Gateways::JSONAPI::Data::MethodDescriptor.new(
      :attributes => [:name, :timezone],
      :descriptions => {
        :singular => "Create a contact.",
        :multiple => "Create contacts."
      }
    ),
    :get => Flapjack::Gateways::JSONAPI::Data::MethodDescriptor.new(
      :attributes => [:name, :timezone],
      :descriptions => {
        :singular => "Get data for a contact.",
        :multiple => "Get data for multiple contacts."
      }
    ),
    :patch => Flapjack::Gateways::JSONAPI::Data::MethodDescriptor.new(
      :attributes => [:name, :timezone],
      :descriptions => {
        :singular => "Update a contact record.",
        :multiple => "Update contact records."
      }
    ),
    :delete => Flapjack::Gateways::JSONAPI::Data::MethodDescriptor.new(
      :descriptions => {
        :singular => "Delete a contact.",
        :multiple => "Delete contacts."
      }
    ),
  }
end
swagger_included_classes() click to toggle source
# File lib/flapjack/data/contact.rb, line 212
def self.swagger_included_classes
  # hack -- hardcoding for now
  [
    Flapjack::Data::Check,
    Flapjack::Data::Contact,
    Flapjack::Data::Medium,
    Flapjack::Data::Rule,
    Flapjack::Data::ScheduledMaintenance,
    Flapjack::Data::State,
    Flapjack::Data::Tag,
    Flapjack::Data::UnscheduledMaintenance
  ]
end

Public Instance Methods

checks() click to toggle source
# File lib/flapjack/data/contact.rb, line 63
def checks
  time = Time.now

  global_acceptors = self.rules.intersect(:enabled => true,
    :blackhole => false, :strategy => 'global')

  global_rejector_ids = self.rules.intersect(:enabled => true,
    :blackhole => true, :strategy => 'global').select {|rejector|

    rejector.is_occurring_at?(time, timezone)
  }.map(&:id)

  # global blackhole
  return Flapjack::Data::Check.empty unless global_rejector_ids.empty?

  tag_rejector_ids = self.rules.intersect(:enabled => true,
    :blackhole => true, :strategy => ['any_tag', 'all_tags', 'no_tag']).select {|rejector|

    rejector.is_occurring_at?(time, timezone)
  }.map(&:id)

  tag_acceptors = self.rules.intersect(:enabled => true, :blackhole => false,
    :strategy => ['any_tag', 'all_tags', 'no_tag']).select {|acceptor|

    acceptor.is_occurring_at?(time, timezone)
  }

  # no positives
  return Flapjack::Data::Check.empty if tag_acceptors.empty?


  # initial scope is all enabled
  linked_checks = Flapjack::Data::Check.intersect(:enabled => true)

  if global_acceptors.empty?
    # if no global acceptor, scope by matching tags
    tag_acceptor_checks = Flapjack::Data::Rule.matching_checks(tag_acceptors.map(&:id))
    linked_checks = linked_checks.intersect(:id => tag_acceptor_checks)
  end

  # then exclude by checks with tags matching rejector, if any
  tag_rejector_checks = Flapjack::Data::Rule.matching_checks(tag_rejector_ids)
  unless tag_rejector_checks.empty?
    linked_checks = linked_checks.diff(:id => tag_rejector_checks)
  end

  linked_checks
end
remove_child_records() click to toggle source
# File lib/flapjack/data/contact.rb, line 53
def remove_child_records
  self.media.each  {|medium| medium.destroy }
  self.rules.each  {|rule|   rule.destroy   }
end
time_zone() click to toggle source
# File lib/flapjack/data/contact.rb, line 58
def time_zone
  return nil if self.timezone.nil?
  ActiveSupport::TimeZone[self.timezone]
end