forked from mattparmett/pcr-ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from gterrono/rewrite
Initial rewrite of the sdk
- Loading branch information
Showing
6 changed files
with
209 additions
and
186 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,58 @@ | ||
class Course < PCR | ||
attr_accessor :aliases, :credits, :description, :history, :id, | ||
:name, :path, :reviews, :sections, :semester, | ||
:retrieved, :valid, :version | ||
|
||
def initialize(path, semester, api_endpt, token) | ||
@path, @semester = path, semester | ||
@api_endpt, @token = api_endpt, token | ||
|
||
# Hit api | ||
api_url = makeURL(self.path) | ||
json = JSON.parse(open(api_url).read) | ||
|
||
# List of sections | ||
section_list = json['result']['sections']['values'] | ||
@sections = [] | ||
section_list.each do |section| | ||
@sections << Section.new(section['path'], @api_endpt, @token) | ||
end | ||
|
||
# Assign attrs | ||
attrs = %w(aliases credits description history id name reviews | ||
retrieved valid version) | ||
attrs.each do |attr| | ||
if json['result'][attr] | ||
self.instance_variable_set("@#{attr}", json['result'][attr]) | ||
else | ||
self.instance_variable_set("@#{attr}", json[attr]) | ||
require 'classes/resource' | ||
|
||
module PCR | ||
class Course | ||
include Comparable | ||
include PCR::Resource | ||
attr_reader :aliases, :credits, :description, :history, :id, | ||
:name, :path, :reviews, :sections, :semester, | ||
:retrieved, :valid, :version | ||
|
||
def initialize(path, semester) | ||
#TODO: Don't need to pass in semester | ||
@path, @semester = path, semester | ||
|
||
# Hit api | ||
json = PCR.get_json(path) | ||
|
||
# List of sections | ||
@sections = json['result']['sections']['values'].map do |section| | ||
Section.new(section['path']) | ||
end | ||
|
||
# Assign attrs | ||
attrs = %w(aliases credits description history id name reviews | ||
retrieved valid version) | ||
set_attrs(attrs, json) | ||
end | ||
end | ||
|
||
def compareSemester(other) | ||
year = self.semester[0..3] | ||
season = self.semester[4] | ||
compYear = other.semester[0..3] | ||
compSeason = other.semester[4] | ||
|
||
if year.to_i > compYear.to_i #Later year | ||
return 1 | ||
elsif year.to_i < compYear.to_i #Earlier year | ||
return -1 | ||
elsif year.to_i == compYear.to_i #Same year, so test season | ||
if season > compSeason #Season is later | ||
return 1 | ||
elsif season = compSeason #Exact same time | ||
return 0 | ||
elsif season < compSeason #compSeason is later | ||
return -1 | ||
end | ||
|
||
def <=>(other) | ||
#TODO: Throw error if not same course | ||
return year <=> other.year unless year == other.year | ||
season <=> other.season | ||
end | ||
end | ||
|
||
def average(metric) | ||
# Aggregate ratings across all sections | ||
total, num = 0, 0 | ||
self.sections.each do |section| | ||
section.reviews.each do |review| | ||
total += review.send(metric).to_f | ||
num += 1 | ||
|
||
def average(metric) | ||
# Aggregate ratings across all sections | ||
total, num = 0, 0 | ||
#TODO: inject | ||
self.sections.each do |section| | ||
section.reviews.each do |review| | ||
total += review.send(metric).to_f | ||
num += 1 | ||
end | ||
end | ||
|
||
# Return average value across all sections | ||
(total / num) | ||
end | ||
|
||
# Return average value across all sections | ||
(total / num) | ||
end | ||
|
||
end | ||
def year | ||
@semester[0..3].to_i | ||
end | ||
|
||
def season | ||
@semester[4] | ||
end | ||
end | ||
end |
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 |
---|---|---|
@@ -1,70 +1,64 @@ | ||
class CourseHistory < PCR | ||
attr_accessor :course_code, :courses, :id, :path, :retrieved, :valid, :version | ||
|
||
def initialize(course_code, api_endpt, token) | ||
@course_code = course_code | ||
@api_endpt = api_endpt | ||
@token = token | ||
|
||
# Read JSON from PCR API | ||
api_url = makeURL("coursehistories/#{self.course_code}") | ||
json = JSON.parse(open(api_url).read) | ||
|
||
# List of courses in coursehistory | ||
course_list = json['result']['courses'] | ||
@courses = [] | ||
course_list.each do |course| | ||
@courses << Course.new(course['path'], course['semester'], @api_endpt, @token) | ||
end | ||
# Sort course list by semester | ||
@courses.sort! { |a,b| a.compareSemester(b) } | ||
|
||
# Assign rest of attrs | ||
attrs = %w(id path reviews retrieved valid version) | ||
attrs.each do |attr| | ||
if json['result'][attr] | ||
self.instance_variable_set("@#{attr}", json['result'][attr]) | ||
else | ||
self.instance_variable_set("@#{attr}", json[attr]) | ||
end | ||
end | ||
end | ||
|
||
def recent(metric) | ||
# Select most recent course | ||
course = @courses[-1] | ||
require 'classes/resource' | ||
|
||
module PCR | ||
class CourseHistory | ||
include PCR::Resource | ||
attr_reader :course_code, :courses, :id, :path, :retrieved, :valid, :version | ||
|
||
# Aggregate ratings for metric | ||
total, num = 0, 0 | ||
course.sections.each do |section| | ||
section.reviews.each do |review| | ||
total += review.send(metric).to_f | ||
num += 1 | ||
def initialize(course_code) | ||
@course_code = course_code | ||
|
||
# Read JSON from PCR API | ||
json = PCR.get_json("coursehistories/#{self.course_code}") | ||
|
||
# List of courses in coursehistory | ||
@courses = json['result']['courses'].map do |course| | ||
Course.new(course['path'], course['semester']) | ||
end | ||
|
||
# Sort course list by semester | ||
@courses.sort! | ||
|
||
# Assign rest of attrs | ||
attrs = %w(id path reviews retrieved valid version) | ||
set_attrs(attrs, json) | ||
end | ||
|
||
# Return average value across most recent sections | ||
(total / num) | ||
end | ||
|
||
def average(metric) | ||
# Aggregate ratings across all sections | ||
total, num = 0, 0 | ||
courses.each do |course| | ||
def recent(metric) | ||
# Select most recent course | ||
course = @courses[-1] | ||
|
||
# Aggregate ratings for metric | ||
total, num = 0, 0 | ||
course.sections.each do |section| | ||
section.reviews.each do |review| | ||
total += review.send(metric).to_f | ||
num += 1 | ||
end | ||
end | ||
|
||
# Return average value across most recent sections | ||
(total / num) | ||
end | ||
|
||
# Return average value across all sections | ||
(total / num) | ||
end | ||
|
||
def name | ||
self.courses.last.name | ||
def average(metric) | ||
# Aggregate ratings across all sections | ||
total, num = 0, 0 | ||
courses.each do |course| | ||
course.sections.each do |section| | ||
section.reviews.each do |review| | ||
total += review.send(metric).to_f | ||
num += 1 | ||
end | ||
end | ||
end | ||
|
||
# Return average value across all sections | ||
total / num | ||
end | ||
|
||
def name | ||
self.courses.last.name | ||
end | ||
end | ||
|
||
end | ||
end |
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,9 @@ | ||
module PCR | ||
module Resource | ||
def set_attrs(attrs, json) | ||
attrs.each do |attr| | ||
self.instance_variable_set("@#{attr}", json['result'][attr] || json[attr]) | ||
end | ||
end | ||
end | ||
end |
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 |
---|---|---|
@@ -1,16 +1,27 @@ | ||
#review_new.rb | ||
require 'classes/resource' | ||
|
||
class Review | ||
#attr_accessor | ||
|
||
def initialize(review_hash) | ||
# Assign ratings | ||
ratings = review_hash['ratings'] | ||
ratings.each do |name, val| | ||
self.instance_variable_set("@#{name}", val) | ||
self.class.send(:attr_accessor, name) | ||
#self.class_eval("def #{name};@#{name};end") | ||
module PCR | ||
class Review | ||
include PCR::Resource | ||
attr_reader :instructor, :num_reviewers, :num_students, :retrieved, | ||
:comments, :id | ||
|
||
def initialize(path) | ||
@path = path | ||
|
||
# Hit api | ||
json = PCR.get_json(path) | ||
|
||
# Assign attrs | ||
attrs = %w(instructor num_reviewers num_students amount_learned comments | ||
retrieved id) | ||
set_attrs(attrs, json) | ||
|
||
# Assign ratings | ||
json['result']['ratings'].each do |name, val| | ||
self.instance_variable_set("@#{name}", val) | ||
self.class.send(:attr_accessor, name) | ||
end | ||
end | ||
end | ||
|
||
end | ||
end |
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 |
---|---|---|
@@ -1,36 +1,28 @@ | ||
class Section < PCR | ||
attr_accessor :aliases, :course, :group, :id, :instructors, | ||
:meetingtimes, :name, :path, :reviews, | ||
:sectionnum, :retrieved, :valid, :version | ||
|
||
def initialize(path, api_endpt, token) | ||
@path = path | ||
@api_endpt = api_endpt | ||
@token = token | ||
|
||
# Hit api | ||
api_url = makeURL(self.path) | ||
json = JSON.parse(open(api_url).read) | ||
|
||
# Get reviews | ||
# Usually one, but may be > 1 | ||
@reviews = [] | ||
reviews_url = makeURL(json['result']['reviews']['path']) | ||
reviews_json = JSON.parse(open(reviews_url).read) | ||
reviews_json['result']['values'].each do |review| | ||
@reviews << Review.new(review) | ||
end | ||
|
||
# Assign attrs | ||
attrs = %w(aliases course group id instructors meetingtimes name | ||
sectionnum retrieved valid version) | ||
attrs.each do |attr| | ||
if json['result'][attr] | ||
self.instance_variable_set("@#{attr}", json['result'][attr]) | ||
else | ||
self.instance_variable_set("@#{attr}", json[attr]) | ||
require 'classes/resource' | ||
|
||
module PCR | ||
class Section | ||
include PCR::Resource | ||
attr_reader :aliases, :course, :group, :id, :instructors, | ||
:meetingtimes, :name, :path, :reviews, | ||
:sectionnum, :retrieved, :valid, :version | ||
|
||
def initialize(path) | ||
@path = path | ||
|
||
# Hit api | ||
json = PCR.get_json(path) | ||
|
||
# Get reviews | ||
# Usually one, but may be > 1 | ||
@reviews = json['result']['reviews']['values'].map do |review| | ||
Review.new(review['path']) | ||
end | ||
|
||
# Assign attrs | ||
attrs = %w(aliases course group id instructors meetingtimes name | ||
sectionnum retrieved valid version) | ||
set_attrs(attrs, json) | ||
end | ||
end | ||
|
||
end | ||
end |
Oops, something went wrong.