Skip to content

Commit

Permalink
✨ Sync Term data to API server by POST/PUT/DELETE method, #151
Browse files Browse the repository at this point in the history
- Request to original API with HTTP POST/PUT/DELETE method for Term resource
  • Loading branch information
kyoshizaki committed Nov 17, 2018
1 parent 97d203c commit 6517268
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 26 deletions.
22 changes: 22 additions & 0 deletions app/controllers/concerns/roster_api.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require 'rest-client'
require 'json'

module RosterApi
extend ActiveSupport::Concern

private

def request_roster_api(endpoint, method, payload={})
url = Rails.application.secrets.roster_url_prefix + endpoint
# FIXME: verify_ssl should be true!
request_hash = {
url: url,
method: method,
headers: {Authorization: 'Bearer ' + Rails.application.secrets.roster_token},
verify_ssl: false
}
request_hash.merge!({payload: payload}) if payload.present?
response = RestClient::Request.execute(request_hash)
JSON.parse(response.body) if response.body.present?
end
end
61 changes: 44 additions & 17 deletions app/controllers/terms_controller.rb
Original file line number Diff line number Diff line change
@@ -1,32 +1,48 @@
class TermsController < ApplicationController
include RosterApi
# ====================================================================
# Public Functions
# ====================================================================
def ajax_create
@term = Term.new(term_params)
if @term.save
flash.now[:message] = '学期を追加しました'
begin
# this action is not permitted when SYSTEM_ROSTER_SYNC is :suspended
raise unless %i[on off].include? SYSTEM_ROSTER_SYNC
Term.transaction do
@term.save!
if SYSTEM_ROSTER_SYNC == :on
response = request_roster_api('/academicSessions/', :post, {academicSession: @term.to_roster_hash})
@term.update_attributes!(sourced_id: response['academicSession']['sourcedId'])
end
end
flash.now[:message] = t('controllers.terms.created')
flash.now[:message_category] = 'info'
@term = Term.new
else
flash.now[:message] = '学期の追加に失敗しました'
rescue => error
flash.now[:message] = t('controllers.terms.creation_failed')
flash.now[:message_category] = 'error'
end

render_term
end

def ajax_update
term = Term.find params[:term][:id]

if term.update_attributes(term_params)
flash.now[:message] = '学期情報を更新しました'
term = Term.find params[:id]
begin
# this action is not permitted when SYSTEM_ROSTER_SYNC is :suspended
raise unless %i[on off].include? SYSTEM_ROSTER_SYNC
Term.transaction do
term.update_attributes!(term_params)
if SYSTEM_ROSTER_SYNC == :on
raise if term.sourced_id.blank?
request_roster_api("/academicSessions/#{term.sourced_id}", :put, {academicSession: term.to_roster_hash})
end
end
flash.now[:message] = t('controllers.terms.updated')
flash.now[:message_category] = 'info'
else
flash.now[:message] = '学期情報の更新に失敗しました'
rescue => error
flash.now[:message] = t('controllers.terms.update_failed')
flash.now[:message_category] = 'error'
end

render_term
end

Expand All @@ -36,10 +52,21 @@ def ajax_new

def ajax_destroy
term = Term.find params[:id]
if term.deletable? session[:id]
term.destroy
else
flash.now[:message] = '学期の削除に失敗しました。学期は登録コースがゼロの時のみ、削除可能です。'
begin
# this action is not permitted when SYSTEM_ROSTER_SYNC is :suspended
raise unless %i[on off].include? SYSTEM_ROSTER_SYNC
Term.transaction do
raise unless term.deletable? session[:id]
term.destroy!
if SYSTEM_ROSTER_SYNC == :on
raise if term.sourced_id.blank?
request_roster_api("/academicSessions/#{term.sourced_id}", :delete)
end
end
flash.now[:message] = t('controllers.terms.deleted')
flash.now[:message_category] = 'info'
rescue => error
flash.now[:message] = t('controllers.terms.delete_failed')
flash.now[:message_category] = 'error'
end
render_term
Expand All @@ -52,7 +79,7 @@ def ajax_destroy
private

def term_params
params.require(:term).permit(:id, :title, :start_at, :end_at)
params.require(:term).permit(:title, :start_at, :end_at)
end

def render_term
Expand Down
4 changes: 2 additions & 2 deletions app/jobs/roster_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def perform(*args)
@logger.formatter = ActiveSupport::Logger::Formatter.new

case SYSTEM_ROSTER_SYNC
when 'on'
when :on
@logger.info 'Started RosterJob'
rterms = get_roster '/terms'
ActiveRecord::Base.transaction do
Expand All @@ -23,7 +23,7 @@ def perform(*args)
end
@logger.info 'Completed RosterJob'
end
when 'off', 'suspended'
when :off, :suspended
@logger.info "Nothing has done with RosterJob because SYSTEM_ROSTER_SYNC is #{SYSTEM_ROSTER_SYNC}"
else
@logger.warn "Incorrect value (#{SYSTEM_ROSTER_SYNC}) is set to constant SYSTEM_ROSTER_SYNC"
Expand Down
10 changes: 10 additions & 0 deletions app/models/term.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,14 @@ def status
'open'
end
end

def to_roster_hash
hash = {
title: self.title,
type: 'term',
startDate: self.start_at,
endDate: self.end_at,
schoolYear: self.end_at.year
}
end
end
4 changes: 1 addition & 3 deletions app/views/terms/_new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
<%= f.label :title, '学期名', class: 'col-md-3 col-form-label text-sm-right' %>
<div class="col-md-7">
<%= f.text_field :title, {class: 'form-control', placeholder: 'タイトル', style: 'margin-right:16px;'} %>
<div class="form-text">コーストップページに表示</div>
</div>
</div>
<div class="form-group row">
Expand All @@ -39,8 +38,7 @@
<div class="header-explanation">学期開始日の1ヶ月前から、コースを学期に登録可能</div>
</h2>
<% @terms.each do |term| %>
<%= form_for(term, url: {action: 'ajax_update'}, html: {style: 'margin-bottom:30px;', remote: true}) do |f| %>
<%= f.hidden_field :id, value: term.id %>
<%= form_for(term, url: {action: 'ajax_update', id: term.id}, html: {style: 'margin-bottom:30px;', remote: true}) do |f| %>
<%= f.text_field :title, {class: 'form-control', placeholder: 'タイトル', style: 'margin-right:2px;'} %>
<%= f.date_select :start_at, {}, {class: 'form-control', style: 'width:80px; margin-right:2px;float: left;'} %> <div style="float:left;margin:0 10px;">-</div><%= f.date_select :end_at, {}, {class: 'form-control', style: 'width:80px; margin-right:2px;float: left;'} %>
<div style="clear: both;width:100%;text-align:right;">( 登録しているコースの数: <%= term.courses.length %></div>
Expand Down
4 changes: 2 additions & 2 deletions config/initializers/constants.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
# Log file for ActiveJob
SYSTEM_JOB_LOG_FILE = 'log/active_job.log'

# Synchronization mode with IMS OneRoster API: on / suspended / off
# Synchronization mode with IMS OneRoster API: select one from [:on, :suspended, :off]
# Check schedule.rb and run "bundle exec whenever -i" when SYSTEM_ROSTER_SYNC changes
SYSTEM_ROSTER_SYNC = 'on'.freeze
SYSTEM_ROSTER_SYNC = :on.freeze

# ===== Versatile constatns =====
# time delay for autocomplete
Expand Down
7 changes: 7 additions & 0 deletions config/locales/controllers/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,10 @@ en:
re_register_button_error: Please re-register +Note button
transfer_error: Transferring note failed
upload_error: Failed to save file
terms:
created: Term created
creation_failed: Term creation failed
updated: Term updated
update_failed: Term update failed
deleted: Term deleted
delete_failed: Term delete failed
7 changes: 7 additions & 0 deletions config/locales/controllers/ja.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,10 @@ ja:
re_register_button_error: +Noteボタンを再登録してください
transfer_error: ノートの移動に失敗しました
upload_error: ファイルの保存に失敗しました
terms:
created: 学期を登録しました
creation_failed: 学期の登録に失敗しました
updated: 学期を更新しました
update_failed: 学期の更新に失敗しました
deleted: 学期を削除しました
delete_failed: 学期の削除に失敗しました
4 changes: 2 additions & 2 deletions config/schedule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@
set :output, "#{path}/log/cron.log"
set :environment, :production

# For SYSTEM_ROSTER_SYNC = 'on' only
# Required only when SYSTEM_ROSTER_SYNC is :on
every 1.day, at: ['6:00 am'] do
runner 'RosterJob.perform_now'
end

# To update course status according to term
# For all SYSTEM_ROSTER_SYNC settings
# Required for all SYSTEM_ROSTER_SYNC settings
every 1.day, at: ['0:10 am'] do
runner 'TermJob.perform_now'
end

0 comments on commit 6517268

Please sign in to comment.