Class: ActivityNotification::SubscriptionsApiController

Inherits:
SubscriptionsController show all
Includes:
CommonApiController, ActivityNotification::Swagger::SubscriptionsApi
Defined in:
app/controllers/activity_notification/subscriptions_api_controller.rb

Overview

Controller to manage subscriptions API.

Direct Known Subclasses

SubscriptionsApiWithDeviseController

Constant Summary

Constants included from CommonController

CommonController::DEFAULT_VIEW_DIRECTORY

Instance Method Summary collapse

Methods included from StoreController

#store_controller_for_activity_notification

Instance Method Details

#create(params) ⇒ JSON

Creates new subscription.

POST /:target_type/:target_id/subscriptions

Returns Created subscription.

Parameters:

  • params (Hash)

    Request parameters

Options Hash (params):

  • :subscription (String)

    Subscription parameters

  • :subscription[:key] (String)

    Key of the subscription

  • :subscription[:subscribing] (String) — default: nil

    Whether the target will subscribe to the notification

  • :subscription[:subscribing_to_email] (String) — default: nil

    Whether the target will subscribe to the notification email

Returns:

  • (JSON)

    Created subscription



39
40
41
42
43
44
45
46
47
48
# File 'app/controllers/activity_notification/subscriptions_api_controller.rb', line 39

def create
  render_invalid_parameter("Parameter is missing or the value is empty: subscription") and return if params[:subscription].blank?
  optional_target_names = (params[:subscription][:optional_targets] || {}).keys.select { |key| !key.to_s.start_with?("subscribing_to_") }
  optional_target_names.each do |optional_target_name|
    subscribing_param = params[:subscription][:optional_targets][optional_target_name][:subscribing]
    params[:subscription][:optional_targets]["subscribing_to_#{optional_target_name}"] = subscribing_param unless subscribing_param.nil?
  end
  super
  render status: 201, json: subscription_json if @subscription
end

#destroy(params) ⇒ JSON

Deletes a subscription.

DELETE /:target_type/:target_id/subscriptions/:id

Returns 204 No Content.

Parameters:

  • params (Hash)

    Request parameters

Returns:

  • (JSON)

    204 No Content



94
95
96
97
# File 'app/controllers/activity_notification/subscriptions_api_controller.rb', line 94

def destroy
  super
  head 204
end

#index(params) ⇒ JSON

Finds and shows a subscription from specified key.

GET /:target_type/:target_id/subscriptions/find

Returns Found single subscription.

Parameters:

  • params (Hash)

    Request parameter options for subscription index

Options Hash (params):

  • :key (required, String) — default: nil

    Key of the subscription

Returns:

  • (JSON)

    Found single subscription



57
58
59
60
# File 'app/controllers/activity_notification/subscriptions_api_controller.rb', line 57

def find
  super
  render json: subscription_json if @subscription
end

#index(params) ⇒ JSON

Returns subscription index of the target.

GET /:target_type/:target_id/subscriptions

Returns configured_count: count of subscription index, subscriptions: subscription index, unconfigured_count: count of unconfigured notification keys, unconfigured_notification_keys: unconfigured notification keys.

Parameters:

  • params (Hash)

    Request parameter options for subscription index

Options Hash (params):

  • :filter (String) — default: nil

    Filter option to load subscription index by their configuration status (Nothing as all, ‘configured’ or ‘unconfigured’)

  • :limit (String) — default: nil

    Limit to query for subscriptions

  • :reverse (String) — default: 'false'

    Whether subscription index and unconfigured notification keys will be ordered as earliest first

  • :filtered_by_key (String) — default: nil

    Key of the subscription for filter

Returns:

  • (JSON)

    configured_count: count of subscription index, subscriptions: subscription index, unconfigured_count: count of unconfigured notification keys, unconfigured_notification_keys: unconfigured notification keys



22
23
24
25
26
27
# File 'app/controllers/activity_notification/subscriptions_api_controller.rb', line 22

def index
  super
  json_response = { configured_count: @subscriptions.size, subscriptions: @subscriptions } if @subscriptions
  json_response = (json_response || {}).merge(unconfigured_count: @notification_keys.size, unconfigured_notification_keys: @notification_keys) if @notification_keys
  render json: json_response
end

#index(params) ⇒ JSON

Finds and returns configured optional_target names from specified key.

GET /:target_type/:target_id/subscriptions/optional_target_names

Returns Configured optional_target names.

Parameters:

  • params (Hash)

    Request parameter options for subscription index

Options Hash (params):

  • :key (required, String) — default: nil

    Key of the subscription

Returns:

  • (JSON)

    Configured optional_target names



69
70
71
72
73
74
# File 'app/controllers/activity_notification/subscriptions_api_controller.rb', line 69

def optional_target_names
  latest_notification = @target.notifications.filtered_by_key(params[:key]).latest
  latest_notification ?
    render(json: { configured_count: latest_notification.optional_target_names.length, optional_target_names: latest_notification.optional_target_names }) :
    render_resource_not_found("Couldn't find notification with this target and 'key'=#{params[:key]}")
end

#show(params) ⇒ JSON

Shows a subscription.

GET /:target_type/:target_id/subscriptions/:id

Returns Found single subscription.

Parameters:

  • params (Hash)

    Request parameters

Returns:

  • (JSON)

    Found single subscription



82
83
84
85
# File 'app/controllers/activity_notification/subscriptions_api_controller.rb', line 82

def show
  super
  render json: subscription_json
end

#open(params) ⇒ JSON

Updates a subscription to subscribe to the notifications.

PUT /:target_type/:target_id/subscriptions/:id/subscribe

Returns Updated subscription.

Parameters:

  • params (Hash)

    Request parameters

Options Hash (params):

  • :with_email_subscription (String) — default: 'true'

    Whether the subscriber also subscribes notification email

  • :with_optional_targets (String) — default: 'true'

    Whether the subscriber also subscribes optional targets

Returns:

  • (JSON)

    Updated subscription



107
108
109
110
# File 'app/controllers/activity_notification/subscriptions_api_controller.rb', line 107

def subscribe
  super
  validate_and_render_subscription
end

#open(params) ⇒ JSON

Updates a subscription to subscribe to the notification email.

PUT /:target_type/:target_id/subscriptions/:id/subscribe_email

Returns Updated subscription.

Parameters:

  • params (Hash)

    Request parameters

Returns:

  • (JSON)

    Updated subscription



129
130
131
132
# File 'app/controllers/activity_notification/subscriptions_api_controller.rb', line 129

def subscribe_to_email
  super
  validate_and_render_subscription
end

#open(params) ⇒ JSON

Updates a subscription to subscribe to the specified optional target.

PUT /:target_type/:target_id/subscriptions/:id/subscribe_to_optional_target

Returns Updated subscription.

Parameters:

  • params (Hash)

    Request parameters

Options Hash (params):

  • :optional_target_name (required, String) — default: nil

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

Returns:

  • (JSON)

    Updated subscription



152
153
154
155
# File 'app/controllers/activity_notification/subscriptions_api_controller.rb', line 152

def subscribe_to_optional_target
  super
  validate_and_render_subscription
end

#open(params) ⇒ JSON

Updates a subscription to unsubscribe to the notifications.

PUT /:target_type/:target_id/subscriptions/:id/unsubscribe

Returns Updated subscription.

Parameters:

  • params (Hash)

    Request parameters

Returns:

  • (JSON)

    Updated subscription



118
119
120
121
# File 'app/controllers/activity_notification/subscriptions_api_controller.rb', line 118

def unsubscribe
  super
  validate_and_render_subscription
end

#open(params) ⇒ JSON

Updates a subscription to unsubscribe to the notification email.

PUT /:target_type/:target_id/subscriptions/:id/unsubscribe_email

Returns Updated subscription.

Parameters:

  • params (Hash)

    Request parameters

Returns:

  • (JSON)

    Updated subscription



140
141
142
143
# File 'app/controllers/activity_notification/subscriptions_api_controller.rb', line 140

def unsubscribe_to_email
  super
  validate_and_render_subscription
end

#open(params) ⇒ JSON

Updates a subscription to unsubscribe to the specified optional target.

PUT /:target_type/:target_id/subscriptions/:id/unsubscribe_to_optional_target

Returns Updated subscription.

Parameters:

  • params (Hash)

    Request parameters

Options Hash (params):

  • :optional_target_name (required, String) — default: nil

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

Returns:

  • (JSON)

    Updated subscription



164
165
166
167
# File 'app/controllers/activity_notification/subscriptions_api_controller.rb', line 164

def unsubscribe_to_optional_target
  super
  validate_and_render_subscription
end