Class: Pantry::Communication::ClientFilter

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#applicationObject (readonly)

Returns the value of attribute application.



32
33
34
# File 'lib/pantry/communication/client_filter.rb', line 32

def application
  @application
end

#environmentObject (readonly)

Returns the value of attribute environment.



32
33
34
# File 'lib/pantry/communication/client_filter.rb', line 32

def environment
  @environment
end

#identityObject (readonly)

Returns the value of attribute identity.



32
33
34
# File 'lib/pantry/communication/client_filter.rb', line 32

def identity
  @identity
end

#rolesObject (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

#streamObject

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

#streamsObject

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_hashObject



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