diff --git a/features/step_definitions/client_steps.rb b/features/step_definitions/client_steps.rb index 045ad0b..dd1709b 100644 --- a/features/step_definitions/client_steps.rb +++ b/features/step_definitions/client_steps.rb @@ -100,7 +100,7 @@ end Given /^(?:When )?I request students for an org with id ([a-f0-9]{24}) and a school with id ([a-f0-9]{24})$/ do |org_id, school_id| - @page = @client.students(org_id, school_id) + @page = @client.students(org_id, :school_id => school_id) end Given /^(?:When )?I request students for that school$/ do diff --git a/lib/learnsprout/address.rb b/lib/learnsprout/address.rb index 2eb556b..e14d68b 100644 --- a/lib/learnsprout/address.rb +++ b/lib/learnsprout/address.rb @@ -7,10 +7,10 @@ class Address :zip def initialize(attrs={}) - @city = attrs["city"] - @state = attrs["state"] - @street = attrs["street"] - @zip = attrs["zip"] + @city = attrs["city"] + @state = attrs["state"] + @street = attrs["street"] + @zip = attrs["zip"] end end end diff --git a/lib/learnsprout/course.rb b/lib/learnsprout/course.rb index 5755db7..e365ca1 100644 --- a/lib/learnsprout/course.rb +++ b/lib/learnsprout/course.rb @@ -1,24 +1,26 @@ module LearnSprout class Course - attr_accessor :course_id, + attr_accessor :id, :course_id, :school_id, :name, :number, - :time_updated + :time_updated, + :updated_at def initialize(attrs={}) - @client = attrs["client"] - @org_id = attrs["org_id"] - @course_id = attrs["id"] - @name = attrs["name"] - @number = attrs["number"] - @time_updated = attrs["time_updated"] - @school_id = attrs["school"]["id"] + @client = attrs["client"] + @org_id = attrs["org_id"] + self.id = @course_id = attrs["id"] + @name = attrs["name"] + @number = attrs["number"] + @time_updated = attrs["time_updated"] + @updated_at = Time.at(@time_updated) if @time_updated + @school_id = attrs["school"]["id"] end def school - @client.school(@org_id, @school_id) + @client.school(@org_id, @school_id) end end end diff --git a/lib/learnsprout/nces.rb b/lib/learnsprout/nces.rb index e30d372..57c7f6d 100644 --- a/lib/learnsprout/nces.rb +++ b/lib/learnsprout/nces.rb @@ -5,8 +5,8 @@ class Nces :school_id def initialize(attrs={}) - @district_id = attrs["district_id"] - @school_id = attrs["school_id"] + @district_id = attrs["district_id"] + @school_id = attrs["school_id"] end end end diff --git a/lib/learnsprout/org.rb b/lib/learnsprout/org.rb index 4661275..5d238a8 100644 --- a/lib/learnsprout/org.rb +++ b/lib/learnsprout/org.rb @@ -1,12 +1,12 @@ module LearnSprout class Org - attr_accessor :org_id, + attr_accessor :id, :org_id, :name def initialize(attrs={}) @client = attrs["client"] - @org_id = attrs["id"] + self.id = @org_id = attrs["id"] @name = attrs["name"] end diff --git a/lib/learnsprout/page.rb b/lib/learnsprout/page.rb index 5281d64..ecb4ae5 100644 --- a/lib/learnsprout/page.rb +++ b/lib/learnsprout/page.rb @@ -1,33 +1,48 @@ module LearnSprout - class Page - include LearnSprout::Connection - - attr_accessor :items - - def initialize(url, type, extras = {}) - @items = [] - @type = type - @extras = extras - data = get(url) - - @nextUrl = data["next"].to_s - # Remove base URL if it's included - prefix = LearnSprout.endpoint.to_s - if @nextUrl[0, prefix.length] == prefix - @nextUrl = @nextUrl[prefix.length, @nextUrl.length - prefix.length] - end - - #TODO handle non-page URL? - data["data"].each do |item| - extras.each do |key, value| - item[key] = value - end - @items << type.new(item) - end - end + class Page + include LearnSprout::Connection + + attr_accessor :items + + def initialize(url, type, extras = {}) + return false if url.nil? || url == "" + @items = [] + @type = type + @extras = extras + data = get(url) - def next - return Page.new(@nextUrl, @type, @extras) + @nextUrl = data["next"].to_s + # Remove base URL if it's included + prefix = LearnSprout.endpoint.to_s + if @nextUrl[0, prefix.length] == prefix + @nextUrl = @nextUrl[prefix.length, @nextUrl.length - prefix.length] + end + + #TODO handle non-page URL? + if data['data'] + data['data'].each do |item| + @items << type.new(item.merge(extras)) end + end + end + + + # page = api_client.schools(org_id) + # while page.any? do + # ... + # end + def any? + @items && @items.any? + end + + def each(&block) + return unless self.any? + self.items.map(&block) + return self.next.each(&block) + end + + def next + return Page.new(@nextUrl, @type, @extras) end + end end diff --git a/lib/learnsprout/phone.rb b/lib/learnsprout/phone.rb index 7dfaa85..5afa43b 100644 --- a/lib/learnsprout/phone.rb +++ b/lib/learnsprout/phone.rb @@ -6,9 +6,9 @@ class Phone :home def initialize(attrs={}) - @fax = attrs["fax"] - @main = attrs["main"] - @home = attrs["home"] + @fax = attrs["fax"] + @main = attrs["main"] + @home = attrs["home"] end end end diff --git a/lib/learnsprout/school.rb b/lib/learnsprout/school.rb index 13c1daf..7f638c5 100644 --- a/lib/learnsprout/school.rb +++ b/lib/learnsprout/school.rb @@ -1,30 +1,32 @@ module LearnSprout class School - attr_accessor :school_id, + attr_accessor :id, :school_id, :name, :number, :nces, :phone, :address, - :time_updated + :time_updated, + :updated_at def initialize(attrs={}) - @client = attrs["client"] - @org_id = attrs["org_id"] - @school_id = attrs["id"] - @name = attrs["name"] - @number = attrs["number"] - @nces = Nces.new(attrs["nces"]) - @phone = Phone.new(attrs["phone"]) - @address = Address.new(attrs["address"]) - @time_updated = attrs["time_updated"] + @client = attrs["client"] + @org_id = attrs["org_id"] + self.id = @school_id = attrs["id"] + @name = attrs["name"] + @number = attrs["number"] + @nces = Nces.new(attrs["nces"]) + @phone = Phone.new(attrs["phone"]) if attrs["phone"] + @address = Address.new(attrs["address"]) + @time_updated = attrs["time_updated"] + @updated_at = Time.at(@time_updated) if @time_updated end #TODO Add org method? def student(student_id) - @client.student(@org_id, student_id) + @client.student(@org_id, student_id) end def students @@ -32,7 +34,7 @@ def students end def section(section_id) - @client.section(@org_id, section_id) + @client.section(@org_id, section_id) end def sections @@ -40,7 +42,7 @@ def sections end def teacher(teacher_id) - @client.teacher(@org_id, teacher_id) + @client.teacher(@org_id, teacher_id) end def teachers @@ -48,7 +50,7 @@ def teachers end def term(term_id) - @client.term(@org_id, term_id) + @client.term(@org_id, term_id) end def terms @@ -60,7 +62,7 @@ def current_term end def course(course_id) - @client.course(@org_id, course_id) + @client.course(@org_id, course_id) end def courses diff --git a/lib/learnsprout/section.rb b/lib/learnsprout/section.rb index 80c68f9..d3f56f2 100644 --- a/lib/learnsprout/section.rb +++ b/lib/learnsprout/section.rb @@ -1,56 +1,62 @@ module LearnSprout class Section - attr_accessor :term_id, + attr_accessor :id, :term_id, :section_id, :number, :room, :time_updated, :teacher_id, :school_id, - :course_id + :course_id, + :student_ids def initialize(attrs={}) - @client = attrs["client"] - @org_id = attrs["org_id"] - @section_id = attrs["id"] - @number = attrs["number"] - @room = attrs["room"] - @term_id = attrs["term"] && attrs["term"]["id"] - @teacher_id = attrs["teacher"] && attrs["teacher"]["id"] - @school_id = attrs["school"] && attrs["school"]["id"] - @course_id = attrs["course"] && attrs["course"]["id"] - @time_updated = attrs["time_updated"] - @student_ids = [] - if attrs["students"] - attrs["students"].each do |student| - @student_ids.push student["id"] - end + @client = attrs["client"] + @org_id = attrs["org_id"] + self.id = @section_id = attrs["id"] + @number = attrs["number"] + @room = attrs["room"] + @term_id = attrs["term"] && attrs["term"]["id"] + @teacher_id = attrs["teacher"] && attrs["teacher"]["id"] + @school_id = attrs["school"] && attrs["school"]["id"] + @course_id = attrs["course"] && attrs["course"]["id"] + @time_updated = attrs["time_updated"] + @updated_at = Time.at(@time_updated) if @time_updated + @student_ids = [] + if attrs["students"] + attrs["students"].each do |student| + if student.is_a?(Hash) + @student_ids.push student["id"] + else + @student_ids.push student + end end + end end def term - @term_id && @client.term(@org_id, @term_id) + @term_id && @client.term(@org_id, @term_id) end def teacher - @teacher_id && @client.teacher(@org_id, @teacher_id) + @teacher_id && @client.teacher(@org_id, @teacher_id) end def school - @school_id && @client.school(@org_id, @school_id) + @school_id && @client.school(@org_id, @school_id) end def course - @course_id && @client.course(@org_id, @course_id) + @course_id && @client.course(@org_id, @course_id) end def students temp_students = [] if @student_ids.count > 0 - @student_ids.each do |student_id| - temp_students.push @client.student(@org_id, student_id) - end + @student_ids.each do |student_id| + temp_students.push @client.student(@org_id, student_id) + end end return temp_students end diff --git a/lib/learnsprout/student.rb b/lib/learnsprout/student.rb index eb49f3c..a6ad47e 100644 --- a/lib/learnsprout/student.rb +++ b/lib/learnsprout/student.rb @@ -1,7 +1,7 @@ module LearnSprout class Student - attr_accessor :student_id, + attr_accessor :id, :student_id, :last_name, :grade, :address, @@ -11,28 +11,30 @@ class Student :birthday, :first_name, :time_updated, + :updated_at, :middle_name, :gender, :exit_date, :entry_date def initialize(attrs={}) - @client = attrs["client"] - @org_id = attrs["org_id"] - @student_id = attrs["id"] - @last_name = attrs["last_name"] - @grade = attrs["grade"] - @address = Address.new(attrs["address"]) - @school_id = attrs["school"]["id"] - @number = attrs["number"] - @phone = Phone.new(attrs["phone"]) - @birthday = attrs["birthday"] && Date.parse(attrs["birthday"]) - @first_name = attrs["first_name"] - @time_updated = attrs["time_updated"] - @middle_name = attrs["middle_name"] - @gender = attrs["gender"] - @exit_date = attrs["exit_date"] && Date.parse(attrs["exit_date"]) - @entry_date = attrs["entry_date"] && Date.parse(attrs["entry_date"]) + @client = attrs["client"] + @org_id = attrs["org_id"] + self.id = @student_id = attrs["id"] + @last_name = attrs["last_name"] + @grade = attrs["grade"] + @address = Address.new(attrs["address"]) + @school_id = attrs["school"]["id"] + @number = attrs["number"] + @phone = Phone.new(attrs["phone"]) + @birthday = attrs["birthday"] && Date.parse(attrs["birthday"]) + @first_name = attrs["first_name"] + @time_updated = attrs["time_updated"] + @updated_at = Time.at(@time_updated) if @time_updated + @middle_name = attrs["middle_name"] + @gender = attrs["gender"] + @exit_date = attrs["exit_date"] && Date.parse(attrs["exit_date"]) + @entry_date = attrs["entry_date"] && Date.parse(attrs["entry_date"]) end end end diff --git a/lib/learnsprout/teacher.rb b/lib/learnsprout/teacher.rb index 2ba0ab4..3bdd0c0 100644 --- a/lib/learnsprout/teacher.rb +++ b/lib/learnsprout/teacher.rb @@ -1,7 +1,7 @@ module LearnSprout class Teacher - attr_accessor :teacher_id, + attr_accessor :id, :teacher_id, :first_name, :middle_name, :last_name, @@ -11,26 +11,28 @@ class Teacher :number, :phone, :time_updated, + :updated_at, :email def initialize(attrs={}) - @client = attrs["client"] - @org_id = attrs["org_id"] - @teacher_id = attrs["id"] - @first_name = attrs["first_name"] - @middle_name = attrs["middle_name"] - @last_name = attrs["last_name"] - @title = attrs["title"] - @address = attrs["address"] && Address.new(attrs["address"]) - @school_id = attrs["school"] && attrs["school"]["id"] - @number = attrs["number"] - @phone = attrs["phone"] && Phone.new(attrs["phone"]) - @time_updated = attrs["time_updated"] - @email = attrs["email"] + @client = attrs["client"] + @org_id = attrs["org_id"] + self.id = @teacher_id = attrs["id"] + @first_name = attrs["first_name"] + @middle_name = attrs["middle_name"] + @last_name = attrs["last_name"] + @title = attrs["title"] + @address = attrs["address"] && Address.new(attrs["address"]) + @school_id = attrs["school"] && attrs["school"]["id"] + @number = attrs["number"] + @phone = attrs["phone"] && Phone.new(attrs["phone"]) + @time_updated = attrs["time_updated"] + @updated_at = Time.at(@time_updated) if @time_updated + @email = attrs["email"] end def school - @school_id && @client.school(@org_id, @school_id) + @school_id && @client.school(@org_id, @school_id) end end end diff --git a/lib/learnsprout/term.rb b/lib/learnsprout/term.rb index b6b383c..245b3e5 100644 --- a/lib/learnsprout/term.rb +++ b/lib/learnsprout/term.rb @@ -1,42 +1,47 @@ module LearnSprout class Term - attr_accessor :term_id, + attr_accessor :id, :term_id, :name, :end_date, :start_date, :school_id, - :time_updated + :time_updated, + :updated_at, + :section_ids def initialize(attrs={}) - @client = attrs["client"] - @org_id = attrs["org_id"] - @term_id = attrs["id"] - @name = attrs["name"] - @end_date = attrs["end_date"] && Date.parse(attrs["end_date"]) - @start_date = attrs["start_date"] && Date.parse(attrs["start_date"]) - @school_id = attrs["school_id"] - @time_updated = attrs["time_updated"] - @section_ids = [] - if attrs["sections"] - attrs["sections"].each do |section| - @section_ids.push section["id"] - end - end + @client = attrs["client"] + @org_id = attrs["org_id"] + self.id = @term_id = attrs["id"] + @name = attrs["name"] + @end_date = attrs["end_date"] && Date.parse(attrs["end_date"]) if attrs["end_date"] + @start_date = attrs["start_date"] && Date.parse(attrs["start_date"]) if attrs["start_date"] + @school_id = attrs["school_id"] + @time_updated = attrs["time_updated"] + @updated_at = Time.at(@time_updated) if @time_updated + @section_ids = [] + if attrs["sections"] + @section_ids = attrs["sections"].collect { |section| section["id"] } + end + end + + def current? + return true if @start_date && !@end_date && Time.now >= @start_date + return true if @end_date && !@start_date && Time.now <= @end_date + return true if !@end_date && !@start_date + return true if Time.now >= @start_date && Time.now <= @end_date + false end - # Gets the sections for this term. This method is extremely slow as it retrieves a list of IDs - # for associated terms then performs a separate request for each term for each ID. - # - # TODO Optimize somehow, perhaps by lazy loading def sections - temp_sections = [] - if @section_ids.count > 0 - @section_ids.each do |section_id| - temp_sections.push @client.section(@org_id, section_id) - end + return @sections if @sections + return [] if @section_ids.empty? + @sections = [] + @client.sections(@org_id, school_id: @school_id).each do |section| + @sections.push section if @section_ids.include?(section.id) end - return temp_sections + return @sections end end -end +end