• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

simukappu / activity_notification / 12735624472

12 Jan 2025 05:24PM UTC coverage: 100.0%. Remained the same
12735624472

push

travis-ci

simukappu
Update Ruby versions for test

3432 of 3432 relevant lines covered (100.0%)

2130.83 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/app/controllers/activity_notification/subscriptions_api_controller.rb
1
module ActivityNotification
24✔
2
  # Controller to manage subscriptions API.
3
  class SubscriptionsApiController < SubscriptionsController
24✔
4
    # Include Swagger API reference
5
    include Swagger::SubscriptionsApi
24✔
6
    # Include CommonApiController to select target and define common methods
7
    include CommonApiController
24✔
8
    protect_from_forgery except: [:create]
24✔
9
    before_action :set_subscription, except: [:index, :create, :find, :optional_target_names]
24✔
10
    before_action ->{ validate_param(:key) }, only: [:find, :optional_target_names]
360✔
11

12
    # Returns subscription index of the target.
13
    #
14
    # GET /:target_type/:target_id/subscriptions
15
    # @overload index(params)
16
    #   @param [Hash] params Request parameter options for subscription index
17
    #   @option params [String] :filter          (nil)     Filter option to load subscription index by their configuration status (Nothing as all, 'configured' or 'unconfigured')
18
    #   @option params [String] :limit           (nil)     Limit to query for subscriptions
19
    #   @option params [String] :reverse         ('false') Whether subscription index and unconfigured notification keys will be ordered as earliest first
20
    #   @option params [String] :filtered_by_key (nil)     Key of the subscription for filter
21
    #   @return [JSON] configured_count: count of subscription index, subscriptions: subscription index, unconfigured_count: count of unconfigured notification keys, unconfigured_notification_keys: unconfigured notification keys
22
    def index
24✔
23
      super
432✔
24
      json_response = { configured_count: @subscriptions.size, subscriptions: @subscriptions } if @subscriptions
432✔
25
      json_response = (json_response || {}).merge(unconfigured_count: @notification_keys.size, unconfigured_notification_keys: @notification_keys) if @notification_keys
432✔
26
      render json: json_response
432✔
27
    end
28

29
    # Creates new subscription.
30
    #
31
    # POST /:target_type/:target_id/subscriptions
32
    # @overload create(params)
33
    #   @param [Hash] params Request parameters
34
    #   @option params [String] :subscription                              Subscription parameters
35
    #   @option params [String] :subscription[:key]                        Key of the subscription
36
    #   @option params [String] :subscription[:subscribing]          (nil) Whether the target will subscribe to the notification
37
    #   @option params [String] :subscription[:subscribing_to_email] (nil) Whether the target will subscribe to the notification email
38
    #   @return [JSON] Created subscription
39
    def create
24✔
40
      render_invalid_parameter("Parameter is missing or the value is empty: subscription") and return if params[:subscription].blank?
336✔
41
      optional_target_names = (params[:subscription][:optional_targets] || {}).keys.select { |key| !key.to_s.start_with?("subscribing_to_") }
528✔
42
      optional_target_names.each do |optional_target_name|
288✔
43
        subscribing_param = params[:subscription][:optional_targets][optional_target_name][:subscribing]
96✔
44
        params[:subscription][:optional_targets]["subscribing_to_#{optional_target_name}"] = subscribing_param unless subscribing_param.nil?
96✔
45
      end
46
      super
288✔
47
      render status: 201, json: subscription_json if @subscription
192✔
48
    end
49

50
    # Finds and shows a subscription from specified key.
51
    #
52
    # GET /:target_type/:target_id/subscriptions/find
53
    # @overload index(params)
54
    #   @param [Hash] params Request parameter options for subscription index
55
    #   @option params [required, String] :key (nil) Key of the subscription
56
    #   @return [JSON] Found single subscription
57
    def find
24✔
58
      super
192✔
59
      render json: subscription_json if @subscription
192✔
60
    end
61

62
    # Finds and returns configured optional_target names from specified key.
63
    #
64
    # GET /:target_type/:target_id/subscriptions/optional_target_names
65
    # @overload index(params)
66
    #   @param [Hash] params Request parameter options for subscription index
67
    #   @option params [required, String] :key (nil) Key of the subscription
68
    #   @return [JSON] Configured optional_target names
69
    def optional_target_names
24✔
70
      latest_notification = @target.notifications.filtered_by_key(params[:key]).latest
144✔
71
      latest_notification ?
144✔
72
        render(json: { configured_count: latest_notification.optional_target_names.length, optional_target_names: latest_notification.optional_target_names }) :
73
        render_resource_not_found("Couldn't find notification with this target and 'key'=#{params[:key]}")
48✔
74
    end
75

76
    # Shows a subscription.
77
    #
78
    # GET /:target_type/:target_id/subscriptions/:id
79
    # @overload show(params)
80
    #   @param [Hash] params Request parameters
81
    #   @return [JSON] Found single subscription
82
    def show
24✔
83
      super
96✔
84
      render json: subscription_json
96✔
85
    end
86
  
87
    # Deletes a subscription.
88
    #
89
    # DELETE /:target_type/:target_id/subscriptions/:id
90
    #
91
    # @overload destroy(params)
92
    #   @param [Hash] params Request parameters
93
    #   @return [JSON] 204 No Content
94
    def destroy
24✔
95
      super
96✔
96
      head 204
96✔
97
    end
98

99
    # Updates a subscription to subscribe to the notifications.
100
    #
101
    # PUT /:target_type/:target_id/subscriptions/:id/subscribe
102
    # @overload open(params)
103
    #   @param [Hash] params Request parameters
104
    #   @option params [String] :with_email_subscription ('true') Whether the subscriber also subscribes notification email
105
    #   @option params [String] :with_optional_targets   ('true') Whether the subscriber also subscribes optional targets
106
    #   @return [JSON] Updated subscription
107
    def subscribe
24✔
108
      super
120✔
109
      validate_and_render_subscription
120✔
110
    end
111

112
    # Updates a subscription to unsubscribe to the notifications.
113
    #
114
    # PUT /:target_type/:target_id/subscriptions/:id/unsubscribe
115
    # @overload open(params)
116
    #   @param [Hash] params Request parameters
117
    #   @return [JSON] Updated subscription
118
    def unsubscribe
24✔
119
      super
120✔
120
      validate_and_render_subscription
120✔
121
    end
122

123
    # Updates a subscription to subscribe to the notification email.
124
    #
125
    # PUT /:target_type/:target_id/subscriptions/:id/subscribe_email
126
    # @overload open(params)
127
    #   @param [Hash] params Request parameters
128
    #   @return [JSON] Updated subscription
129
    def subscribe_to_email
24✔
130
      super
192✔
131
      validate_and_render_subscription
192✔
132
    end
133

134
    # Updates a subscription to unsubscribe to the notification email.
135
    #
136
    # PUT /:target_type/:target_id/subscriptions/:id/unsubscribe_email
137
    # @overload open(params)
138
    #   @param [Hash] params Request parameters
139
    #   @return [JSON] Updated subscription
140
    def unsubscribe_to_email
24✔
141
      super
120✔
142
      validate_and_render_subscription
120✔
143
    end
144

145
    # Updates a subscription to subscribe to the specified optional target.
146
    #
147
    # PUT /:target_type/:target_id/subscriptions/:id/subscribe_to_optional_target
148
    # @overload open(params)
149
    #   @param [Hash] params Request parameters
150
    #   @option params [required, String] :optional_target_name (nil) Class name of the optional target implementation (e.g. 'amazon_sns', 'slack')
151
    #   @return [JSON] Updated subscription
152
    def subscribe_to_optional_target
24✔
153
      super
192✔
154
      validate_and_render_subscription
192✔
155
    end
156

157
    # Updates a subscription to unsubscribe to the specified optional target.
158
    #
159
    # PUT /:target_type/:target_id/subscriptions/:id/unsubscribe_to_optional_target
160
    # @overload open(params)
161
    #   @param [Hash] params Request parameters
162
    #   @option params [required, String] :optional_target_name (nil) Class name of the optional target implementation (e.g. 'amazon_sns', 'slack')
163
    #   @return [JSON] Updated subscription
164
    def unsubscribe_to_optional_target
24✔
165
      super
120✔
166
      validate_and_render_subscription
120✔
167
    end
168

169
    protected
24✔
170

171
      # Returns include option for subscription JSON
172
      # @api protected
173
      def subscription_json_include_option
24✔
174
        [:target].freeze
1,104✔
175
      end
176

177
      # Returns methods option for subscription JSON
178
      # @api protected
179
      def subscription_json_methods_option
24✔
180
        [].freeze
1,104✔
181
      end
182

183
      # Returns JSON of @subscription
184
      # @api protected
185
      def subscription_json
24✔
186
        @subscription.as_json(include: subscription_json_include_option, methods: subscription_json_methods_option)
1,104✔
187
      end
188

189
      # Validate @subscription and render JSON of @subscription
190
      # @api protected
191
      def validate_and_render_subscription
24✔
192
        raise RecordInvalidError, @subscription.errors.full_messages.first if @subscription.invalid?
864✔
193
        render json: subscription_json
720✔
194
      end
195

196
  end
197
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc