-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
46 changed files
with
3,379 additions
and
263 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
app/controllers/activity_notification/apidocs_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
module ActivityNotification | ||
# Controller to manage Swagger API references. | ||
# @See https://github.com/fotinakis/swagger-blocks/blob/master/spec/lib/swagger_v3_blocks_spec.rb | ||
class ApidocsController < ActivityNotification.config.parent_controller.constantize | ||
include ::Swagger::Blocks | ||
|
||
swagger_root do | ||
key :openapi, '3.0.0' | ||
info version: ActivityNotification::VERSION do | ||
key :description, 'A default REST API created by activity_notification which provides integrated user activity notifications for Ruby on Rails' | ||
key :title, 'ActivityNotification' | ||
key :termsOfService, 'https://github.com/simukappu/activity_notification' | ||
contact do | ||
key :name, 'activity_notification community' | ||
key :url, 'https://github.com/simukappu/activity_notification#help' | ||
end | ||
license do | ||
key :name, 'MIT' | ||
key :url, 'https://github.com/simukappu/activity_notification/blob/master/MIT-LICENSE' | ||
end | ||
end | ||
|
||
server do | ||
key :url, 'https://activity-notification-example.herokuapp.com/api/{version}' | ||
key :description, 'ActivityNotification online demo including REST API' | ||
|
||
variable :version do | ||
key :enum, ['v2'] | ||
key :default, :"v#{ActivityNotification::GEM_VERSION::MAJOR}" | ||
end | ||
end | ||
server do | ||
key :url, 'http://localhost:3000/api/{version}' | ||
key :description, 'Example Rails application at localhost including REST API' | ||
|
||
variable :version do | ||
key :enum, ['v2'] | ||
key :default, :"v#{ActivityNotification::GEM_VERSION::MAJOR}" | ||
end | ||
end | ||
|
||
tag do | ||
key :name, 'notifications' | ||
key :description, 'Operations about user activity notifications' | ||
externalDocs do | ||
key :description, 'Find out more' | ||
key :url, 'https://github.com/simukappu/activity_notification#creating-notifications' | ||
end | ||
end | ||
|
||
tag do | ||
key :name, 'subscriptions' | ||
key :description, 'Operations about subscription management' | ||
externalDocs do | ||
key :description, 'Find out more' | ||
key :url, 'https://github.com/simukappu/activity_notification#subscription-management' | ||
end | ||
end | ||
end | ||
|
||
SWAGGERED_CLASSES = [ | ||
Notification, | ||
NotificationsApiController, | ||
Subscription, | ||
SubscriptionsApiController, | ||
self | ||
].freeze | ||
|
||
# Returns root JSON of Swagger API references. | ||
# GET /apidocs | ||
def index | ||
render json: ::Swagger::Blocks.build_root_json(SWAGGERED_CLASSES) | ||
end | ||
end | ||
end |
136 changes: 136 additions & 0 deletions
136
app/controllers/activity_notification/notifications_api_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
module ActivityNotification | ||
# Controller to manage notifications API. | ||
class NotificationsApiController < NotificationsController | ||
# Include Swagger API reference | ||
include Swagger::NotificationsApi | ||
# Include CommonApiController to select target and define common methods | ||
include CommonApiController | ||
protect_from_forgery except: [:open_all] | ||
rescue_from ActivityNotification::NotifiableNotFoundError, with: :render_notifiable_not_found | ||
|
||
# Returns notification index of the target. | ||
# | ||
# GET /:target_type/:target_id/notifications | ||
# @overload index(params) | ||
# @param [Hash] params Request parameter options for notification index | ||
# @option params [String] :filter (nil) Filter option to load notification index by their status (Nothing as auto, 'opened' or 'unopened') | ||
# @option params [String] :limit (nil) Maximum number of notifications to return | ||
# @option params [String] :reverse ('false') Whether notification index will be ordered as earliest first | ||
# @option params [String] :without_grouping ('false') Whether notification index will include group members | ||
# @option params [String] :with_group_members ('false') Whether notification index will include group members | ||
# @option params [String] :filtered_by_type (nil) Notifiable type to filter notification index | ||
# @option params [String] :filtered_by_group_type (nil) Group type to filter notification index, valid with :filtered_by_group_id | ||
# @option params [String] :filtered_by_group_id (nil) Group instance ID to filter notification index, valid with :filtered_by_group_type | ||
# @option params [String] :filtered_by_key (nil) Key of notifications to filter notification index | ||
# @return [JSON] count: number of notification index records, notifications: notification index | ||
def index | ||
super | ||
render json: { | ||
count: @notifications.size, | ||
notifications: @notifications.as_json(include: notification_json_include_option, methods: notification_json_methods_option) | ||
} | ||
end | ||
|
||
# Opens all notifications of the target. | ||
# | ||
# POST /:target_type/:target_id/notifications/open_all | ||
# @overload open_all(params) | ||
# @param [Hash] params Request parameters | ||
# @option params [String] :filtered_by_type (nil) Notifiable type to filter notification index | ||
# @option params [String] :filtered_by_group_type (nil) Group type to filter notification index, valid with :filtered_by_group_id | ||
# @option params [String] :filtered_by_group_id (nil) Group instance ID to filter notification index, valid with :filtered_by_group_type | ||
# @option params [String] :filtered_by_key (nil) Key of notifications to filter notification index | ||
# @return [JSON] count: number of opened notification records, notifications: opened notifications | ||
def open_all | ||
super | ||
render json: { | ||
count: @opened_notifications.size, | ||
notifications: @opened_notifications.as_json(include: notification_json_include_option, methods: notification_json_methods_option) | ||
} | ||
end | ||
|
||
# Returns a single notification. | ||
# | ||
# GET /:target_type/:target_id/notifications/:id | ||
# @overload show(params) | ||
# @param [Hash] params Request parameters | ||
# @return [JSON] Found single notification | ||
def show | ||
super | ||
render json: notification_json | ||
end | ||
|
||
# Deletes a notification. | ||
# | ||
# DELETE /:target_type/:target_id/notifications/:id | ||
# @overload destroy(params) | ||
# @param [Hash] params Request parameters | ||
# @return [JSON] 204 No Content | ||
def destroy | ||
super | ||
head 204 | ||
end | ||
|
||
# Opens a notification. | ||
# | ||
# PUT /:target_type/:target_id/notifications/:id/open | ||
# @overload open(params) | ||
# @param [Hash] params Request parameters | ||
# @option params [String] :move ('false') Whether it redirects to notifiable_path after the notification is opened | ||
# @return [JSON] count: number of opened notification records, notification: opened notification | ||
def open | ||
super | ||
unless params[:move].to_s.to_boolean(false) | ||
render json: { | ||
count: @opened_notifications_count, | ||
notification: notification_json | ||
} | ||
end | ||
end | ||
|
||
# Moves to notifiable_path of the notification. | ||
# | ||
# GET /:target_type/:target_id/notifications/:id/move | ||
# @overload open(params) | ||
# @param [Hash] params Request parameters | ||
# @option params [String] :open ('false') Whether the notification will be opened | ||
# @return [JSON] location: notifiable path, count: number of opened notification records, notification: specified notification | ||
def move | ||
super | ||
render status: 302, location: @notification.notifiable_path, json: { | ||
location: @notification.notifiable_path, | ||
count: (@opened_notifications_count || 0), | ||
notification: notification_json | ||
} | ||
end | ||
|
||
protected | ||
|
||
# Returns include option for notification JSON | ||
# @api protected | ||
def notification_json_include_option | ||
[:target, :notifiable, :group, :notifier, :group_members].freeze | ||
end | ||
|
||
# Returns methods option for notification JSON | ||
# @api protected | ||
def notification_json_methods_option | ||
[:notifiable_path].freeze | ||
end | ||
|
||
# Returns JSON of @notification | ||
# @api protected | ||
def notification_json | ||
@notification.as_json(include: notification_json_include_option, methods: notification_json_methods_option) | ||
end | ||
|
||
# Render associated notifiable record not found error with 500 status | ||
# @api protected | ||
# @param [Error] error Error object | ||
# @return [void] | ||
def render_notifiable_not_found(error) | ||
render status: 500, json: error_response(code: 500, message: "Associated record not found", type: error.message) | ||
end | ||
|
||
end | ||
end |
Oops, something went wrong.