Class: Superbolt::Messenger

Inherits:
Object
  • Object
show all
Defined in:
lib/superbolt/messenger.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Messenger

Returns a new instance of Messenger.



5
6
7
8
9
10
11
# File 'lib/superbolt/messenger.rb', line 5

def initialize(options={})
  @name = options.delete(:to)
  @origin = options.delete(:from) || Superbolt.app_name
  @event = options.delete(:event) || self.class.defaultevent
  @env = Superbolt.env
  @arguments = options
end

Instance Attribute Details

#argumentsObject

Returns the value of attribute arguments.



3
4
5
# File 'lib/superbolt/messenger.rb', line 3

def arguments
  @arguments
end

#envObject

Returns the value of attribute env.



3
4
5
# File 'lib/superbolt/messenger.rb', line 3

def env
  @env
end

#eventObject

Returns the value of attribute event.



3
4
5
# File 'lib/superbolt/messenger.rb', line 3

def event
  @event
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/superbolt/messenger.rb', line 3

def name
  @name
end

#originObject

Returns the value of attribute origin.



3
4
5
# File 'lib/superbolt/messenger.rb', line 3

def origin
  @origin
end

Class Method Details

.defaulteventObject



73
74
75
# File 'lib/superbolt/messenger.rb', line 73

def self.defaultevent
  'default'
end

Instance Method Details

#attr_chainer(attr, val) ⇒ Object



39
40
41
42
43
# File 'lib/superbolt/messenger.rb', line 39

def attr_chainer(attr, val)
  return send(attr) unless val
  self.send("#{attr}=", val)
  self
end

#data(val = nil) ⇒ Object



35
36
37
# File 'lib/superbolt/messenger.rb', line 35

def data(val=nil)
  attr_chainer(:arguments, val)
end

#destination_nameObject



69
70
71
# File 'lib/superbolt/messenger.rb', line 69

def destination_name
  "#{name}_#{env}"
end

#from(val = nil) ⇒ Object



27
28
29
# File 'lib/superbolt/messenger.rb', line 27

def from(val=nil)
  attr_chainer(:origin, val)
end

#messageObject



13
14
15
16
17
18
19
# File 'lib/superbolt/messenger.rb', line 13

def message
  {
    origin: origin,
    event: event,
    arguments: arguments
  }
end

#pack_filesObject



54
55
56
57
58
59
60
# File 'lib/superbolt/messenger.rb', line 54

def pack_files
  return unless arguments.is_a?(Hash)
  file_keys = arguments.keys.find_all { |key| key.to_s.match(Superbolt.file_matcher) }
  file_keys.each do |key|
    arguments[key] = FileMarshal::Dumper.new(arguments[key]).to_hash
  end
end

#queueObject



62
63
64
65
66
67
# File 'lib/superbolt/messenger.rb', line 62

def queue
  unless name
    raise "no destination app name defined, please pass one in"
  end
  Queue.new(destination_name)
end

#re(val = nil) ⇒ Object



31
32
33
# File 'lib/superbolt/messenger.rb', line 31

def re(val=nil)
  attr_chainer(:event, val)
end

#send!(args = nil) ⇒ Object

alias




48
49
50
51
52
# File 'lib/superbolt/messenger.rb', line 48

def send!(args=nil)
  self.arguments = args if args
  pack_files
  queue.push(message)
end

#to(val = nil) ⇒ Object

chainer methods



23
24
25
# File 'lib/superbolt/messenger.rb', line 23

def to(val=nil)
  attr_chainer(:name, val)
end