Module: ActivityNotification::SubscriptionApi

Extended by:
ActiveSupport::Concern
Included in:
ORM::ActiveRecord::Subscription, ORM::Dynamoid::Subscription, ORM::Mongoid::Subscription
Defined in:
lib/activity_notification/apis/subscription_api.rb

Overview

Defines API for subscription included in Subscription model.

Instance Method Summary collapse

Instance Method Details

#as_json(options = {}) ⇒ Hash

Override as_json method for optional_targets representation

Parameters:

  • options (Hash) (defaults to: {})

    Options for as_json method

Returns:

  • (Hash)

    Hash representing the subscription model



107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/activity_notification/apis/subscription_api.rb', line 107

def as_json(options = {})
  json = super(options).with_indifferent_access
  optional_targets_json = {}
  optional_target_names.each do |optional_target_name|
    optional_targets_json[optional_target_name] = {
      subscribing:   json[:optional_targets][Subscription.to_optional_target_key(optional_target_name)],
      subscribed_at: json[:optional_targets][Subscription.to_optional_target_subscribed_at_key(optional_target_name)],
      unsubscribed_at: json[:optional_targets][Subscription.to_optional_target_unsubscribed_at_key(optional_target_name)]
    }
  end
  json[:optional_targets] = optional_targets_json
  json
end

#optional_target_namesArray<Symbol>

Returns optional_target names of the subscription from optional_targets field.

Returns:

  • (Array<Symbol>)

    Array of optional target names



224
225
226
# File 'lib/activity_notification/apis/subscription_api.rb', line 224

def optional_target_names
  optional_targets.keys.select { |key| key.to_s.start_with?("subscribing_to_") }.map { |key| key.slice(15..-1) }
end

#subscribe(options = {}) ⇒ Boolean

Subscribes to the notification and notification email.

Parameters:

  • options (Hash) (defaults to: {})

    Options for subscribing to the notification

Options Hash (options):

  • :subscribed_at (DateTime) — default: Time.current

    Time to set to subscribed_at and subscribed_to_email_at of the subscription record

  • :with_email_subscription (Boolean) — default: true

    If the subscriber also subscribes notification email

  • :with_optional_targets (Boolean) — default: true

    If the subscriber also subscribes optional_targets

Returns:

  • (Boolean)

    If successfully updated subscription instance



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/activity_notification/apis/subscription_api.rb', line 128

def subscribe(options = {})
  subscribed_at = options[:subscribed_at] || Time.current
  with_email_subscription = options.has_key?(:with_email_subscription) ? options[:with_email_subscription] : ActivityNotification.config.subscribe_to_email_as_default
  with_optional_targets   = options.has_key?(:with_optional_targets) ? options[:with_optional_targets] : ActivityNotification.config.subscribe_to_optional_targets_as_default
  new_attributes = { subscribing: true, subscribed_at: subscribed_at, optional_targets: optional_targets }
  new_attributes = new_attributes.merge(subscribing_to_email: true, subscribed_to_email_at: subscribed_at) if with_email_subscription
  if with_optional_targets
    optional_target_names.each do |optional_target_name|
      new_attributes[:optional_targets] = new_attributes[:optional_targets].merge(
        Subscription.to_optional_target_key(optional_target_name) => true,
        Subscription.to_optional_target_subscribed_at_key(optional_target_name) => Subscription.convert_time_as_hash(subscribed_at))
    end
  end
  update(new_attributes)
end

#subscribe_to_email(options = {}) ⇒ Boolean

Subscribes to the notification email.

Parameters:

  • options (Hash) (defaults to: {})

    Options for subscribing to the notification email

Options Hash (options):

  • :subscribed_to_email_at (DateTime) — default: Time.current

    Time to set to subscribed_to_email_at of the subscription record

Returns:

  • (Boolean)

    If successfully updated subscription instance



167
168
169
170
# File 'lib/activity_notification/apis/subscription_api.rb', line 167

def subscribe_to_email(options = {})
  subscribed_to_email_at = options[:subscribed_to_email_at] || Time.current
  update(subscribing_to_email: true, subscribed_to_email_at: subscribed_to_email_at)
end

#subscribe_to_optional_target(optional_target_name, options = {}) ⇒ Boolean

Subscribes to the specified optional target.

Parameters:

  • optional_target_name (String, Symbol)

    Symbol class name of the optional target implementation (e.g. :amazon_sns, :slack)

  • options (Hash) (defaults to: {})

    Options for unsubscribing to the specified optional target

Options Hash (options):

  • :subscribed_at (DateTime) — default: Time.current

    Time to set to subscribed__at in optional_targets hash of the subscription record

Returns:

  • (Boolean)

    If successfully updated subscription instance



200
201
202
203
204
205
206
# File 'lib/activity_notification/apis/subscription_api.rb', line 200

def subscribe_to_optional_target(optional_target_name, options = {})
  subscribed_at = options[:subscribed_at] || Time.current
  update(optional_targets: optional_targets.merge(
    Subscription.to_optional_target_key(optional_target_name) => true,
    Subscription.to_optional_target_subscribed_at_key(optional_target_name) => Subscription.convert_time_as_hash(subscribed_at))
  )
end

#subscribing_to_optional_target?(optional_target_name, subscribe_as_default = ActivityNotification.config.subscribe_to_optional_targets_as_default) ⇒ Boolean

Returns if the target subscribes to the specified optional target.

Parameters:

  • optional_target_name (Symbol)

    Symbol class name of the optional target implementation (e.g. :amazon_sns, :slack)

  • subscribe_as_default (Boolean) (defaults to: ActivityNotification.config.subscribe_to_optional_targets_as_default)

    Default subscription value to use when the subscription record does not configured

Returns:

  • (Boolean)

    If the target subscribes to the specified optional target



187
188
189
190
191
192
# File 'lib/activity_notification/apis/subscription_api.rb', line 187

def subscribing_to_optional_target?(optional_target_name, subscribe_as_default = ActivityNotification.config.subscribe_to_optional_targets_as_default)
  optional_target_key = Subscription.to_optional_target_key(optional_target_name)
  subscribe_as_default ?
    !optional_targets.has_key?(optional_target_key) || optional_targets[optional_target_key] :
     optional_targets.has_key?(optional_target_key) && optional_targets[optional_target_key]
end

#unsubscribe(options = {}) ⇒ Boolean

Unsubscribes to the notification and notification email.

Parameters:

  • options (Hash) (defaults to: {})

    Options for unsubscribing to the notification

Options Hash (options):

  • :unsubscribed_at (DateTime) — default: Time.current

    Time to set to unsubscribed_at and unsubscribed_to_email_at of the subscription record

Returns:

  • (Boolean)

    If successfully updated subscription instance



149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/activity_notification/apis/subscription_api.rb', line 149

def unsubscribe(options = {})
  unsubscribed_at = options[:unsubscribed_at] || Time.current
  new_attributes = { subscribing:          false, unsubscribed_at:          unsubscribed_at,
                     subscribing_to_email: false, unsubscribed_to_email_at: unsubscribed_at,
                     optional_targets: optional_targets }
  optional_target_names.each do |optional_target_name|
    new_attributes[:optional_targets] = new_attributes[:optional_targets].merge(
      Subscription.to_optional_target_key(optional_target_name) => false,
      Subscription.to_optional_target_unsubscribed_at_key(optional_target_name) => Subscription.convert_time_as_hash(subscribed_at))
  end
  update(new_attributes)
end

#unsubscribe_to_email(options = {}) ⇒ Boolean

Unsubscribes to the notification email.

Parameters:

  • options (Hash) (defaults to: {})

    Options for unsubscribing the notification email

Options Hash (options):

  • :subscribed_to_email_at (DateTime) — default: Time.current

    Time to set to subscribed_to_email_at of the subscription record

Returns:

  • (Boolean)

    If successfully updated subscription instance



177
178
179
180
# File 'lib/activity_notification/apis/subscription_api.rb', line 177

def unsubscribe_to_email(options = {})
  unsubscribed_to_email_at = options[:unsubscribed_to_email_at] || Time.current
  update(subscribing_to_email: false, unsubscribed_to_email_at: unsubscribed_to_email_at)
end

#unsubscribe_to_optional_target(optional_target_name, options = {}) ⇒ Boolean

Unsubscribes to the specified optional target.

Parameters:

  • optional_target_name (String, Symbol)

    Class name of the optional target implementation (e.g. :amazon_sns, :slack)

  • options (Hash) (defaults to: {})

    Options for unsubscribing to the specified optional target

Options Hash (options):

  • :unsubscribed_at (DateTime) — default: Time.current

    Time to set to unsubscribed__at in optional_targets hash of the subscription record

Returns:

  • (Boolean)

    If successfully updated subscription instance



214
215
216
217
218
219
220
# File 'lib/activity_notification/apis/subscription_api.rb', line 214

def unsubscribe_to_optional_target(optional_target_name, options = {})
  unsubscribed_at = options[:unsubscribed_at] || Time.current
  update(optional_targets: optional_targets.merge(
    Subscription.to_optional_target_key(optional_target_name) => false,
    Subscription.to_optional_target_unsubscribed_at_key(optional_target_name) => Subscription.convert_time_as_hash(unsubscribed_at))
  )
end