Class: Moogle::Handlers::AcceptNotification

Inherits:
Object
  • Object
show all
Includes:
Serf::Command, Serf::Util::ErrorHandling
Defined in:
lib/moogle/handlers/accept_notification.rb

Overview

Handler that accepts Moogle::Messges::Notifications and creates actual push requests to relay the notification to each found target.

Constant Summary collapse

DEFAULT_PUSH_OPTIONS =
{
  'from' => '[email protected]'
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAcceptNotification

Returns a new instance of AcceptNotification.



34
35
36
37
# File 'lib/moogle/handlers/accept_notification.rb', line 34

def initialize
  @pusher_queue = opts! :pusher_queue
  @default_options = opts :default_push_options, DEFAULT_PUSH_OPTIONS
end

Instance Attribute Details

#default_optionsObject (readonly)

Returns the value of attribute default_options.



28
29
30
# File 'lib/moogle/handlers/accept_notification.rb', line 28

def default_options
  @default_options
end

#pusher_queueObject (readonly)

Returns the value of attribute pusher_queue.



27
28
29
# File 'lib/moogle/handlers/accept_notification.rb', line 27

def pusher_queue
  @pusher_queue
end

Instance Method Details

#callObject



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/moogle/handlers/accept_notification.rb', line 39

def call
  return nil if request.receiver_refs.blank? || request.message_kind.blank?

  # Get all targets whose links' kinds + receivers match.
  model = opts :model, Moogle::Target
  targets = model.all(
    model.links.message_kind => request.message_kind,
    model.links.receiver_ref => request.receiver_refs)

  # Create a new push request for each target found.
  # Push each request to the pusher queue, and errors are caught
  # and pushed to the error channel.
  targets.each do |target|
    with_error_handling(
        kind: 'moogle/handlers/accept_notification',
        request: request.to_hash,
        target_id: target.id) do
      request_factory = request_factory_for target.type
      push_data = [
        request.attributes,
        default_options,
        target.options,
        {
          target_id: target.id,
          message_origin: "#{request.message_kind}:#{request.uuid}:"
        }
      ].reduce(&:merge)
      push_request = request_factory.build push_data
      pusher_queue.push push_request.to_hash
    end
  end

  return nil
rescue => e
  e.extend Moogle::Error
  raise e
end

#request_factory_for(target_type) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/moogle/handlers/accept_notification.rb', line 77

def request_factory_for(target_type)
  case target_type.to_s
  when 'Moogle::BlogTarget'
    Moogle::Requests::PushBlogEntry
  when 'Moogle::EmailTarget'
    Moogle::Requests::PushEmail
  #when 'Moogle::FacebookTarget'
  #  Moogle::Requests::PushFacebookAction
  #when 'Moogle::TwitterTarget'
  #  Moogle::Requests::PushTweet
  when 'Moogle::WebhookTarget'
    Moogle::Requests::PushWebhookPing
  else
    raise ArgumentError, "Unsupported Target #{target_type}"
  end
end