lib/flapjack/data/contact.rb in flapjack-0.8.4 vs lib/flapjack/data/contact.rb in flapjack-0.8.5

- old
+ new

@@ -18,11 +18,11 @@ class Contact attr_accessor :id, :first_name, :last_name, :email, :media, :media_intervals, :media_rollup_thresholds, :pagerduty_credentials, - :linked_entity_ids + :linked_entity_ids, :linked_media_ids TAG_PREFIX = 'contact_tag' ALL_MEDIA = ['email', 'sms', 'jabber', 'pagerduty'] def self.all(options = {}) @@ -153,10 +153,22 @@ @redis.hset("contact_media:#{self.id}", 'pagerduty', details['service_key']) @redis.hmset("contact_pagerduty:#{self.id}", *['subdomain', 'username', 'password'].collect {|f| [f, details[f]]}) end + # returns false if this contact was already in the set for the entity + def add_entity(entity) + key = "contacts_for:#{entity.id}" + @redis.sadd(key, self.id) + end + + # returns false if this contact wasn't in the set for the entity + def remove_entity(entity) + key = "contacts_for:#{entity.id}" + @redis.srem(key, self.id) + end + # NB ideally contacts_for:* keys would scope the entity and check by an # input source, for namespacing purposes def entities(options = {}) @redis.keys('contacts_for:*').inject({}) {|ret, k| if @redis.sismember(k, self.id) @@ -202,11 +214,11 @@ contact_ids = redis.sinter(k, temp_set) next if contact_ids.empty? next unless k =~ /^contacts_for:([a-zA-Z0-9][a-zA-Z0-9\.\-]*[a-zA-Z0-9])(?::(\w+))?$/ entity_id = $1 - check = $2 + check = $2 entity_data << {:id => entity_id, :name => redis.hget("entity:#{entity_id}", 'name')} contact_ids.each do |contact_id| linked_entity_ids[contact_id] ||= [] @@ -464,11 +476,24 @@ "email" => self.email, "media" => self.media, "media_intervals" => self.media_intervals, "media_rollup_thresholds" => self.media_rollup_thresholds, "timezone" => self.timezone.name, + "tags" => self.tags.to_a + }.to_json + end + + def to_jsonapi(*args) + { "id" => self.id, + "first_name" => self.first_name, + "last_name" => self.last_name, + "email" => self.email, + "timezone" => self.timezone.name, "tags" => self.tags.to_a, - "links" => {:entities => @linked_entity_ids || []} + "links" => { + :entities => @linked_entity_ids || [], + :media => @linked_media_ids || [] + } }.to_json end private