Class: Pantry::Communication::ClientFilter
- Inherits:
-
Object
- Object
- Pantry::Communication::ClientFilter
- Defined in:
- lib/pantry/communication/client_filter.rb
Overview
ClientFilter handles and manages building filters that map configuration values to 0MQ stream names. A message stream is a period-delimited string that works with 0MQ’s Subscription prefix matcher, allowing Clients to choose which messages they want to receive. Streams are built to enable tiered delivery capability.
For example, a Client with the application ‘pantry’ and roles ‘app’ and ‘db’ will subscribe to the following streams:
pantry
pantry.app
pantry.db
Similarly to differentiate between environments, the environment is added inbetween the application and the role:
pantry
pantry.production
pantry.production.app
pantry.production.db
This class is also used to when sending messages, to choose which specific stream (e.g. the deepest buildable) to send a given message down.
A client identity token can also be given via identity. If identity is provided then that stream will be chosen above all others. Use this to send a message to specific clients.
Instance Attribute Summary collapse
-
#application ⇒ Object
readonly
Returns the value of attribute application.
-
#environment ⇒ Object
readonly
Returns the value of attribute environment.
-
#identity ⇒ Object
readonly
Returns the value of attribute identity.
-
#roles ⇒ Object
readonly
Returns the value of attribute roles.
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#includes?(filter) ⇒ Boolean
A filter includes another filter if the other filter matches.
-
#initialize(application: nil, environment: nil, roles: [], identity: nil) ⇒ ClientFilter
constructor
A new instance of ClientFilter.
-
#matches?(test_stream) ⇒ Boolean
Will this filter match on the given stream?.
-
#stream ⇒ Object
Return the most specific stream that matches this ClientFilter.
-
#streams ⇒ Object
List out all communication streams this ClientFilter is configured to know about.
- #to_hash ⇒ Object
Constructor Details
#initialize(application: nil, environment: nil, roles: [], identity: nil) ⇒ ClientFilter
34 35 36 37 38 39 |
# File 'lib/pantry/communication/client_filter.rb', line 34 def initialize(application: nil, environment: nil, roles: [], identity: nil) @application = application @environment = environment @roles = roles || [] @identity = identity end |
Instance Attribute Details
#application ⇒ Object (readonly)
Returns the value of attribute application.
32 33 34 |
# File 'lib/pantry/communication/client_filter.rb', line 32 def application @application end |
#environment ⇒ Object (readonly)
Returns the value of attribute environment.
32 33 34 |
# File 'lib/pantry/communication/client_filter.rb', line 32 def environment @environment end |
#identity ⇒ Object (readonly)
Returns the value of attribute identity.
32 33 34 |
# File 'lib/pantry/communication/client_filter.rb', line 32 def identity @identity end |
#roles ⇒ Object (readonly)
Returns the value of attribute roles.
32 33 34 |
# File 'lib/pantry/communication/client_filter.rb', line 32 def roles @roles end |
Instance Method Details
#==(other) ⇒ Object
77 78 79 80 81 82 83 84 85 |
# File 'lib/pantry/communication/client_filter.rb', line 77 def ==(other) return false unless other return false unless other.is_a?(ClientFilter) self.application == other.application && self.environment == other.environment && self.roles == other.roles && self.identity == other.identity end |
#includes?(filter) ⇒ Boolean
A filter includes another filter if the other filter matches. This does not look at identities.
96 97 98 99 100 101 102 103 104 |
# File 'lib/pantry/communication/client_filter.rb', line 96 def includes?(filter) return true if self == filter return true if streams == [""] my_stream = Set.new(streams) other_stream = Set.new(filter.streams) my_stream.subset?(other_stream) end |
#matches?(test_stream) ⇒ Boolean
Will this filter match on the given stream?
88 89 90 91 92 |
# File 'lib/pantry/communication/client_filter.rb', line 88 def matches?(test_stream) self.streams.any? do |stream| stream.start_with?(test_stream) end end |
#stream ⇒ Object
Return the most specific stream that matches this ClientFilter. identity is chosen above all others.
69 70 71 72 73 74 75 |
# File 'lib/pantry/communication/client_filter.rb', line 69 def stream if @identity @identity else [@application, @environment, @roles.first].compact.join(".") end end |
#streams ⇒ Object
List out all communication streams this ClientFilter is configured to know about.
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/pantry/communication/client_filter.rb', line 42 def streams list = [] base_stream = [] if @identity list << @identity end if @application list << @application base_stream = [@application] end if @environment list << [base_stream, @environment].flatten.compact.join(".") end @roles.each do |role| list << [base_stream, @environment, role].flatten.compact.join(".") end list = list.flatten.compact list.empty? ? [""] : list end |
#to_hash ⇒ Object
106 107 108 109 110 111 112 113 |
# File 'lib/pantry/communication/client_filter.rb', line 106 def to_hash { application: @application, environment: @environment, roles: @roles, identity: @identity } end |