Class: Postzord::Dispatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/postzord/dispatcher.rb

Direct Known Subclasses

Private, Public

Defined Under Namespace

Classes: Private, Public

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user, object, opts = {}) ⇒ Dispatcher


15
16
17
18
19
20
21
22
23
# File 'lib/postzord/dispatcher.rb', line 15

def initialize(user, object, opts={})
  @sender = user
  @object = object
  @xml = @object.to_diaspora_xml
  @opts = opts

  additional_subscribers = opts[:additional_subscribers] || []
  @subscribers = subscribers_from_object | [*additional_subscribers]
end

Instance Attribute Details

#objectObject (readonly)

Returns the value of attribute object


10
11
12
# File 'lib/postzord/dispatcher.rb', line 10

def object
  @object
end

#optsObject (readonly)

Returns the value of attribute opts


10
11
12
# File 'lib/postzord/dispatcher.rb', line 10

def opts
  @opts
end

#senderObject (readonly)

Returns the value of attribute sender


10
11
12
# File 'lib/postzord/dispatcher.rb', line 10

def sender
  @sender
end

#subscribersObject (readonly)

Returns the value of attribute subscribers


10
11
12
# File 'lib/postzord/dispatcher.rb', line 10

def subscribers
  @subscribers
end

#xmlObject (readonly)

Returns the value of attribute xml


10
11
12
# File 'lib/postzord/dispatcher.rb', line 10

def xml
  @xml
end

Class Method Details

.build(user, object, opts = {}) ⇒ Postzord::Dispatcher


26
27
28
29
30
31
32
33
34
35
36
# File 'lib/postzord/dispatcher.rb', line 26

def self.build(user, object, opts={})
  unless object.respond_to? :to_diaspora_xml
    raise 'This object does not respond_to? to_diaspora xml.  Try including Diaspora::Federated::Base into your object'
  end

  if self.object_should_be_processed_as_public?(object)
    Postzord::Dispatcher::Public.new(user, object, opts)
  else
    Postzord::Dispatcher::Private.new(user, object, opts)
  end
end

.defer_build_and_post(user, object, opts = {}) ⇒ Object


38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/postzord/dispatcher.rb', line 38

def self.defer_build_and_post(user, object, opts={})
  opts[:additional_subscribers] ||= []
  if opts[:additional_subscribers].present?
    opts[:additional_subscribers] = [*opts[:additional_subscribers]].map(&:id)
  end

  if opts[:to].present?
    opts[:to] = [*opts[:to]].map {|e| e.respond_to?(:id) ? e.id : e }
  end

  Workers::DeferredDispatch.perform_async(user.id, object.class.to_s, object.id, opts)
end

.object_should_be_processed_as_public?(object) ⇒ Boolean


53
54
55
56
57
58
59
# File 'lib/postzord/dispatcher.rb', line 53

def self.object_should_be_processed_as_public?(object)
  if object.respond_to?(:public?) && object.public?
    true
  else
    false
  end
end

Instance Method Details

#postObject


62
63
64
65
66
67
# File 'lib/postzord/dispatcher.rb', line 62

def post
  self.deliver_to_services(@opts[:url], @opts[:services] || [])
  self.post_to_subscribers if @subscribers.present?
  self.process_after_dispatch_hooks
  @object
end