Class: Bifrost::Topic

Inherits:
Entity
  • Object
show all
Defined in:
lib/bifrost/topic.rb

Overview

Topics are central to the pub/sub system in the Bifrost. All messages must be delivered to a topic. The topic is responsible for forwarding the message to registered subscribers

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Topic

Returns a new instance of Topic.



11
12
13
14
# File 'lib/bifrost/topic.rb', line 11

def initialize(name)
  @name ||= name
  super()
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/bifrost/topic.rb', line 9

def name
  @name
end

Instance Method Details

#==(other) ⇒ Object



73
74
75
# File 'lib/bifrost/topic.rb', line 73

def ==(other)
  name == other.name && self.class == other.class
end

#add_subscriber(subscriber) ⇒ Object

A new subscriber can be added to a topic



44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/bifrost/topic.rb', line 44

def add_subscriber(subscriber)
  if exists?
    begin
      @bus.interface.create_subscription(name, subscriber.name)
    rescue Azure::Core::Http::HTTPError => e
      return false if e.status_code == 409
      raise e
    end
    true
  else
    false
  end
end

#deleteObject

If a topic is defined, we can remove the definition



27
28
29
30
31
32
33
34
# File 'lib/bifrost/topic.rb', line 27

def delete
  if exists?
    @bus.delete_topic(name)
    true
  else
    false
  end
end

#exists?Boolean

Topics are self aware, and know if they exist or not, but only with the help of the almighty bus

Returns:

  • (Boolean)


79
80
81
# File 'lib/bifrost/topic.rb', line 79

def exists?
  @bus.topic_exists?(self)
end

#remove_subscriber(subscriber) ⇒ Object

A topic subscriber can be removed from the topic if it exists



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/bifrost/topic.rb', line 59

def remove_subscriber(subscriber)
  if exists?
    begin
      @bus.interface.delete_subscription(name, subscriber.name)
    rescue Azure::Core::Http::HTTPError => e
      return false if e.status_code == 404
      raise e
    end
    true
  else
    false
  end
end

#saveObject

If a topic has not been defined we can save it, so it becomes defined



17
18
19
20
21
22
23
24
# File 'lib/bifrost/topic.rb', line 17

def save
  if exists?
    false
  else
    @bus.create_topic(name)
    true
  end
end

#subscribersObject

This method returns a list of subscribers currently defined on the topic



37
38
39
40
41
# File 'lib/bifrost/topic.rb', line 37

def subscribers
  @bus.interface.list_subscriptions(name).map do |s|
    Subscriber.new(s.name)
  end
end

#to_sObject



83
84
85
# File 'lib/bifrost/topic.rb', line 83

def to_s
  name
end