Skip to content

Commit 705fdb7

Browse files
committed
transition the polling api to the jsonapi standard
fixes CNVS-13271 this commit changes the formatting of the JSON returned by the polling endpoints to conform to the adopted jsonapi standard. It also adds a 'created_at' field to the poll and poll_session endpoints Test plan - All polling endpoints should adhere to the jsonapi standard (nested under a collection, e.g. all returned JSON items will be contained under a pluralized root, polls will be under a 'polls' key, poll choices will be under a 'poll_choices' key, even for a single resource) - There are no 'meta' or 'links' attributes to worry about yet - The poll and poll_session endpoints should return a 'created_at' field. This is viewable by both students and teachers Change-Id: Ie0fd49a2c699fba5d8257ee1e8dbb062c81662cb Reviewed-on: https://gerrit.instructure.com/35388 Tested-by: Jenkins <jenkins@instructure.com> Reviewed-by: Ahmad Amireh <ahmad@instructure.com> QA-Review: Caleb Guanzon <cguanzon@instructure.com> Product-Review: Josh Simpson <jsimpson@instructure.com>
1 parent feb9644 commit 705fdb7

12 files changed

Lines changed: 255 additions & 145 deletions

app/controllers/polling/poll_choices_controller.rb

Lines changed: 47 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,17 @@ class PollChoicesController < ApplicationController
5959
#
6060
# Returns the list of PollChoices in this poll.
6161
#
62-
# @returns [PollChoice]
62+
# @example_response
63+
# {
64+
# "poll_choices": [PollChoice]
65+
# }
66+
#
6367
def index
6468
if authorized_action(@poll, @current_user, :read)
6569
@poll_choices = @poll.poll_choices
6670
@poll_choices = Api.paginate(@poll_choices, self, api_v1_poll_choices_url(@poll))
6771

68-
render json: serialize_json(@poll_choices)
72+
render json: serialize_jsonapi(@poll_choices)
6973
end
7074
end
7175

@@ -74,12 +78,16 @@ def index
7478
#
7579
# Returns the poll choice with the given id
7680
#
77-
# @returns PollChoice
81+
# @example_response
82+
# {
83+
# "poll_choices": [PollChoice]
84+
# }
85+
#
7886
def show
7987
@poll_choice = @poll.poll_choices.find(params[:id])
8088

8189
if authorized_action(@poll, @current_user, :read)
82-
render json: serialize_json(@poll_choice, true)
90+
render json: serialize_jsonapi(@poll_choice)
8391
end
8492
end
8593

@@ -88,20 +96,25 @@ def show
8896
#
8997
# Create a new poll choice for this poll
9098
#
91-
# @argument poll_choice[text] [Required, String]
99+
# @argument poll_choices[][text] [Required, String]
92100
# The descriptive text of the poll choice.
93101
#
94-
# @argument poll_choice[is_correct] [Optional, Boolean]
102+
# @argument poll_choices[][is_correct] [Optional, Boolean]
95103
# Whether this poll choice is considered correct or not. Defaults to false.
96104
#
97-
# @returns PollChoice
105+
# @example_response
106+
# {
107+
# "poll_choices": [PollChoice]
108+
# }
109+
#
98110
def create
99-
@poll_choice = @poll.poll_choices.new(params[:poll_choice])
100-
@poll_choice.is_correct = false if params[:poll_choice] && params[:poll_choice][:is_correct].blank?
111+
poll_choice_params = params[:poll_choices][0]
112+
@poll_choice = @poll.poll_choices.new(poll_choice_params)
113+
@poll_choice.is_correct = false if poll_choice_params && poll_choice_params[:is_correct].blank?
101114

102115
if authorized_action(@poll, @current_user, :update)
103116
if @poll_choice.save
104-
render json: serialize_json(@poll_choice)
117+
render json: serialize_jsonapi(@poll_choice)
105118
else
106119
render json: @poll_choice.errors, status: :bad_request
107120
end
@@ -113,23 +126,28 @@ def create
113126
#
114127
# Update an existing poll choice for this poll
115128
#
116-
# @argument poll_choice[text] [Required, String]
129+
# @argument poll_choices[][text] [Required, String]
117130
# The descriptive text of the poll choice.
118131
#
119-
# @argument poll_choice[is_correct] [Optional, Boolean]
132+
# @argument poll_choices[][is_correct] [Optional, Boolean]
120133
# Whether this poll choice is considered correct or not. Defaults to false.
121134
#
122-
# @returns Poll
135+
# @example_response
136+
# {
137+
# "poll_choices": [PollChoice]
138+
# }
139+
#
123140
def update
141+
poll_choice_params = params[:poll_choices][0]
124142
@poll_choice = @poll.poll_choices.find(params[:id])
125143

126-
if params[:poll_choice] && params[:poll_choice][:is_correct].blank?
127-
params[:poll_choice][:is_correct] = @poll_choice.is_correct
144+
if poll_choice_params && poll_choice_params[:is_correct].blank?
145+
poll_choice_params[:is_correct] = @poll_choice.is_correct
128146
end
129147

130148
if authorized_action(@poll, @current_user, :update)
131-
if @poll_choice.update_attributes(params[:poll_choice])
132-
render json: serialize_json(@poll_choice)
149+
if @poll_choice.update_attributes(poll_choice_params)
150+
render json: serialize_jsonapi(@poll_choice)
133151
else
134152
render json: @poll_choice.errors, status: :bad_request
135153
end
@@ -150,18 +168,18 @@ def destroy
150168
end
151169

152170
protected
153-
def serialize_json(poll_choices, single=false)
154-
poll_choices = Array(poll_choices)
155-
156-
serialized_set = poll_choices.map do |poll_choice|
157-
Polling::PollChoiceSerializer.new(poll_choice, {
158-
controller: self,
159-
root: false,
160-
include_root: false,
161-
scope: @current_user
162-
}).as_json
163-
end
164-
single ? serialized_set.first : serialized_set
171+
def serialize_jsonapi(poll_choices)
172+
poll_choices = Array.wrap(poll_choices)
173+
174+
serialized_set = Canvas::APIArraySerializer.new(poll_choices, {
175+
each_serializer: Polling::PollChoiceSerializer,
176+
controller: self,
177+
root: false,
178+
scope: @current_user,
179+
include_root: false
180+
}).as_json
181+
182+
{ poll_choices: serialized_set }
165183
end
166184

167185
end

app/controllers/polling/poll_sessions_controller.rb

Lines changed: 66 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ module Polling
5454
# "description": "Specifies whether the results are viewable by students.",
5555
# "example": "true",
5656
# "type": "boolean"
57+
# },
58+
# "created_at": {
59+
# "description": "The time at which the poll session was created.",
60+
# "example": "2014-01-07T15:16:18Z",
61+
# "type": "string",
62+
# "format": "date-time"
5763
# }
5864
# }
5965
# }
@@ -69,13 +75,17 @@ class PollSessionsController < ApplicationController
6975
#
7076
# Returns the list of PollSessions in this poll.
7177
#
72-
# @returns [PollSession]
78+
# @example_response
79+
# {
80+
# "poll_sessions": [PollSession]
81+
# }
82+
#
7383
def index
7484
if authorized_action(@poll, @current_user, :update)
7585
@poll_sessions = @poll.poll_sessions
7686
@poll_sessions = Api.paginate(@poll_sessions, self, api_v1_poll_sessions_url(@poll))
7787

78-
render json: serialize_json(@poll_sessions)
88+
render json: serialize_jsonapi(@poll_sessions)
7989
end
8090
end
8191

@@ -84,12 +94,16 @@ def index
8494
#
8595
# Returns the poll session with the given id
8696
#
87-
# @returns PollSession
97+
# @example_response
98+
# {
99+
# "poll_sessions": [PollSession]
100+
# }
101+
#
88102
def show
89103
@poll_session = @poll.poll_sessions.find(params[:id])
90104

91105
if authorized_action(@poll_session, @current_user, :read)
92-
render json: serialize_json(@poll_session, true)
106+
render json: serialize_jsonapi(@poll_session)
93107
end
94108
end
95109

@@ -98,29 +112,35 @@ def show
98112
#
99113
# Create a new poll session for this poll
100114
#
101-
# @argument poll_session[course_id] [Required, Integer]
115+
# @argument poll_sessions[][course_id] [Required, Integer]
102116
# The id of the course this session is associated with.
103117
#
104-
# @argument poll_session[course_section_id] [Required, Integer]
118+
# @argument poll_sessions[][course_section_id] [Required, Integer]
105119
# The id of the course section this session is associated with.
106120
#
107-
# @argument poll_session[has_public_results] [Optional, Boolean]
121+
# @argument poll_sessions[][has_public_results] [Optional, Boolean]
108122
# Whether or not results are viewable by students.
109123
#
110-
# @returns PollSession
124+
# @example_response
125+
# {
126+
# "poll_sessions": [PollSession]
127+
# }
128+
#
111129
def create
112-
if params[:poll_session] && course_id = params[:poll_session].delete(:course_id)
130+
poll_session_params = params[:poll_sessions][0]
131+
132+
if course_id = poll_session_params.delete(:course_id)
113133
@course = Course.find(course_id)
114134
end
115135

116136
raise ActiveRecord::RecordNotFound.new(I18n.t("polling.poll_sessions.errors.course_required", "Course is required.")) unless @course
117137

118-
@poll_session = @poll.poll_sessions.new(params[:poll_session])
138+
@poll_session = @poll.poll_sessions.new(poll_session_params)
119139
@poll_session.course = @course
120140

121141
if authorized_action(@poll, @current_user, :create) && authorized_action(@course, @current_user, :update)
122142
if @poll_session.save
123-
render json: serialize_json(@poll_session)
143+
render json: serialize_jsonapi(@poll_session)
124144
else
125145
render json: @poll_session.errors, status: :bad_request
126146
end
@@ -132,22 +152,26 @@ def create
132152
#
133153
# Update an existing poll session for this poll
134154
#
135-
# @argument poll_session[course_id] [Required, Integer]
155+
# @argument poll_sessions[][course_id] [Required, Integer]
136156
# The id of the course this session is associated with.
137157
#
138-
# @argument poll_session[course_section_id] [Required, Integer]
158+
# @argument poll_sessions[][course_section_id] [Required, Integer]
139159
# The id of the course section this session is associated with.
140160
#
141-
# @argument poll_session[has_public_results] [Optional, Boolean]
161+
# @argument poll_sessions[][has_public_results] [Optional, Boolean]
142162
# Whether or not results are viewable by students.
143163
#
144-
# @returns PollSession
164+
# @example_response
165+
# {
166+
# "poll_sessions": [PollSession]
167+
# }
168+
#
145169
def update
146170
@poll_session = @poll.poll_sessions.find(params[:id])
147-
171+
poll_session_params = params[:poll_sessions][0]
148172
if authorized_action(@poll, @current_user, :update)
149-
if @poll_session.update_attributes(params[:poll_session])
150-
render json: serialize_json(@poll_session)
173+
if @poll_session.update_attributes(poll_session_params)
174+
render json: serialize_jsonapi(@poll_session)
151175
else
152176
render json: @poll_session.errors, status: :bad_request
153177
end
@@ -170,42 +194,50 @@ def destroy
170194
# @API Publish a poll session
171195
# @beta
172196
#
173-
# @returns PollSession
197+
# @example_response
198+
# {
199+
# "poll_sessions": [PollSession]
200+
# }
201+
#
174202
def publish
175203
@poll_session = @poll.poll_sessions.find(params[:id])
176204

177205
if authorized_action(@poll_session, @current_user, :publish)
178206
@poll_session.publish!
179-
render json: serialize_json(@poll_session)
207+
render json: serialize_jsonapi(@poll_session)
180208
end
181209
end
182210

183211
# @API Close a published poll session
184212
# @beta
185213
#
186-
# @returns PollSession
214+
# @example_response
215+
# {
216+
# "poll_sessions": [PollSession]
217+
# }
218+
#
187219
def close
188220
@poll_session = @poll.poll_sessions.find(params[:id])
189221

190222
if authorized_action(@poll_session, @current_user, :publish)
191223
@poll_session.close!
192-
render json: serialize_json(@poll_session)
224+
render json: serialize_jsonapi(@poll_session)
193225
end
194226
end
195227

196228
protected
197-
def serialize_json(poll_sessions, single=false)
198-
poll_sessions = Array(poll_sessions)
199-
200-
serialized_set = poll_sessions.map do |poll_session|
201-
Polling::PollSessionSerializer.new(poll_session, {
202-
controller: self,
203-
root: false,
204-
include_root: false,
205-
scope: @current_user
206-
}).as_json
207-
end
208-
single ? serialized_set.first : serialized_set
229+
def serialize_jsonapi(poll_sessions)
230+
poll_sessions = Array.wrap(poll_sessions)
231+
232+
serialized_set = Canvas::APIArraySerializer.new(poll_sessions, {
233+
each_serializer: Polling::PollSessionSerializer,
234+
controller: self,
235+
root: false,
236+
scope: @current_user,
237+
include_root: false
238+
}).as_json
239+
240+
{ poll_sessions: serialized_set }
209241
end
210242

211243
end

0 commit comments

Comments
 (0)