Commit 6a48f1e8 authored by Zac Wood's avatar Zac Wood

Finished API cleanup and added Dockerile for API

parent 5836be61
FROM ruby:2.5
RUN mkdir /api
WORKDIR /api
ADD . /api
RUN bundle install --without development
ENV RAILS_ENV=production
...@@ -12,11 +12,6 @@ gem 'sqlite3' ...@@ -12,11 +12,6 @@ gem 'sqlite3'
# Use Puma as the app server # Use Puma as the app server
gem 'puma', '~> 3.7' gem 'puma', '~> 3.7'
# Use SCSS for stylesheets # Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks # Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5' gem 'turbolinks', '~> 5'
......
require 'icalendar' require 'icalendar'
require 'time' require 'time'
# Contains functionality for generating schedules.
class CalendarGeneratorController < ApplicationController class CalendarGeneratorController < ApplicationController
def generate # Render an iCal file containing the schedules of all the
# course sections with the given CRNs.
def new
cal = Icalendar::Calendar.new cal = Icalendar::Calendar.new
# the intended format for the json is a list of CRNs
params[:_json].each do |crn| # for each CRN sent by the post request params[:_json].each do |crn| # for each CRN sent by the post request
section = CourseSection.find_by_crn(crn) section = CourseSection.find_by_crn(crn)
event = generate_event_from_section(section) event = generate_event_from_section(section)
......
...@@ -4,7 +4,6 @@ class CourseSectionsController < ApplicationController ...@@ -4,7 +4,6 @@ class CourseSectionsController < ApplicationController
# Render JSON of all Sections belonging to a given Course. # Render JSON of all Sections belonging to a given Course.
def index def index
@sections = CourseSection.where(course_id: params[:course_id]) @sections = CourseSection.where(course_id: params[:course_id])
render json: @sections render json: @sections
end end
end end
...@@ -4,9 +4,10 @@ class CoursesController < ApplicationController ...@@ -4,9 +4,10 @@ class CoursesController < ApplicationController
def index def index
@courses = Course.all @courses = Course.all
# filter by subject + course number if the params are included
@courses = @courses.where(subject: params[:subject].upcase) if params.key?(:subject) @courses = @courses.where(subject: params[:subject].upcase) if params.key?(:subject)
@courses = @courses.where(course_number: params[:course_number]) if params.key?(:course_number) @courses = @courses.where(course_number: params[:course_number]) if params.key?(:course_number)
render json: @courses render json: @courses
end end
......
class SearchController < ApplicationController
def index
if params.key?(:crn)
crn = params[:crn]
@sections = CourseSection.find_by_crn(crn)
render json: @sections
else
render status: 404
end
end
end
...@@ -17,7 +17,7 @@ Rails.application.configure do ...@@ -17,7 +17,7 @@ Rails.application.configure do
# Attempt to read encrypted secrets from `config/secrets.yml.enc`. # Attempt to read encrypted secrets from `config/secrets.yml.enc`.
# Requires an encryption key in `ENV["RAILS_MASTER_KEY"]` or # Requires an encryption key in `ENV["RAILS_MASTER_KEY"]` or
# `config/secrets.yml.key`. # `config/secrets.yml.key`.
config.read_encrypted_secrets = true # config.read_encrypted_secrets = true
# Disable serving static files from the `/public` folder by default since # Disable serving static files from the `/public` folder by default since
# Apache or NGINX already handles this. # Apache or NGINX already handles this.
......
...@@ -4,7 +4,7 @@ Rails.application.routes.draw do ...@@ -4,7 +4,7 @@ Rails.application.routes.draw do
resources :courses, only: [:index, :show] resources :courses, only: [:index, :show]
resources :course_sections, only: [:index] resources :course_sections, only: [:index]
post 'generate', controller: 'calendar_generator', action: 'generate' post 'generate', controller: 'calendar_generator', action: 'new'
end end
root 'courses#index' # Set the root to be the courses API endpoint root 'courses#index' # Set the root to be the courses API endpoint
......
...@@ -14,8 +14,8 @@ parser = PatriotWeb::Parser.new ...@@ -14,8 +14,8 @@ parser = PatriotWeb::Parser.new
# get the first semester only # get the first semester only
semester = parser.parse_semesters.first semester = parser.parse_semesters.first
puts "DDOSing Patriot Web, buckle up kids puts "DDOSing Patriot Web, buckle up kids"
"
# parse all subjects and their courses in the semester # parse all subjects and their courses in the semester
parser.parse_subjects(semester).each do |subject| parser.parse_subjects(semester).each do |subject|
puts "Getting courses for #{subject}" puts "Getting courses for #{subject}"
......
#!/bin/sh
export SECRET_KEY_BASE=$(rails secret)
cp db/development.sqlite3 db/production.sqlite3
rails s
...@@ -2,7 +2,16 @@ require 'test_helper' ...@@ -2,7 +2,16 @@ require 'test_helper'
class CalendarGeneratorControllerTest < ActionDispatch::IntegrationTest class CalendarGeneratorControllerTest < ActionDispatch::IntegrationTest
test "should get generate" do test "should get generate" do
assert true # TODO: Implement test crns = [course_sections(:cs112001).crn, course_sections(:cs112002).crn]
# assert_response :success post "/api/generate", params: crns.to_json, headers: { 'CONTENT_TYPE' => 'application/json' }
# DTSTAMP and UID lines uniquely identify events, so we can't test against them.
# so remove all the lines starting with them.
# the \r characters are also annoying so just remove them too
gen = @response.body.split("\n").select {|line| !line.include?("DTSTAMP") && !line.include?("UID")}.join("\n").gsub(/\r/, "")
correct_ical = File.open("test/test.ics").read.gsub(/\r/, "")
assert_equal correct_ical, gen
end end
end end
...@@ -10,8 +10,8 @@ class CoursesControllerTest < ActionDispatch::IntegrationTest ...@@ -10,8 +10,8 @@ class CoursesControllerTest < ActionDispatch::IntegrationTest
assert_equal courses_count, courses_returned.count assert_equal courses_count, courses_returned.count
end end
test '#index should return filtered by subject' do test '#index should return filtered by subject case insensitive' do
get courses_url subject: "CS" get courses_url subject: "Cs"
assert_response :success assert_response :success
courses_returned = JSON.parse @response.body courses_returned = JSON.parse @response.body
......
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
may1st: # tuesday may1st: # tuesday
date: 2018-05-01 date: <%= Date.new(2018, 5, 30) %>
semester: one semester: fall2018
may2nd: # wednesday may2nd: # wednesday
date: 2018-05-02 date: <%= Date.new(2018, 5, 31) %>
semester: two semester: fall2018
...@@ -6,11 +6,11 @@ cs112001: ...@@ -6,11 +6,11 @@ cs112001:
section_type: MyString section_type: MyString
title: MyString title: MyString
instructor: MyString instructor: MyString
start_date: 2018-04-07 start_date: 2018-05-21
end_date: 2018-04-07 end_date: 2018-06-04
days: MyString days: MWF
start_time: MyString start_time: 12:00 pm
end_time: MyString end_time: 1:00 pm
location: MyString location: MyString
status: MyString status: MyString
campus: MyString campus: MyString
...@@ -24,11 +24,11 @@ cs112002: ...@@ -24,11 +24,11 @@ cs112002:
section_type: MyString section_type: MyString
title: MyString title: MyString
instructor: MyString instructor: MyString
start_date: 2018-04-07 start_date: 2018-05-21
end_date: 2018-04-07 end_date: 2018-06-04
days: MyString days: TR
start_time: MyString start_time: 11:00 am
end_time: MyString end_time: 2:00 pm
location: MyString location: MyString
status: MyString status: MyString
campus: MyString campus: MyString
...@@ -42,11 +42,11 @@ cs211001: ...@@ -42,11 +42,11 @@ cs211001:
section_type: MyString section_type: MyString
title: MyString title: MyString
instructor: MyString instructor: MyString
start_date: 2018-04-07 start_date: 2018-05-21
end_date: 2018-04-07 end_date: 2018-06-04
days: MyString days: TR
start_time: MyString start_time: 2:30 pm
end_time: MyString end_time: 3:00 pm
location: MyString location: MyString
status: MyString status: MyString
campus: MyString campus: MyString
......
BEGIN:VCALENDAR
VERSION:2.0
PRODID:icalendar-ruby
CALSCALE:GREGORIAN
BEGIN:VEVENT
DTSTART:20180521T120000
DTEND:20180521T130000
DESCRIPTION:MyString
LOCATION:MyString
SUMMARY:MyString
RRULE:FREQ=WEEKLY;UNTIL=20180604T130000;BYDAY=MO,WE,FR
EXDATE:20180530T120000
EXDATE:20180531T120000
END:VEVENT
BEGIN:VEVENT
DTSTART:20180521T110000
DTEND:20180521T140000
DESCRIPTION:MyString
LOCATION:MyString
SUMMARY:MyString2
RRULE:FREQ=WEEKLY;UNTIL=20180604T140000;BYDAY=TU,TH
EXDATE:20180530T110000
EXDATE:20180531T110000
EXDATE:20180521T110000
END:VEVENT
END:VCALENDAR
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment