From 2aa96a154f11b2f907377605ebcd99b1a2786f0a Mon Sep 17 00:00:00 2001 From: "Thomas R. Koll" Date: Mon, 6 Aug 2012 19:20:15 +0200 Subject: [PATCH 01/15] Alias term_id, school_id etc. to id --- lib/learnsprout/course.rb | 4 ++-- lib/learnsprout/org.rb | 4 ++-- lib/learnsprout/school.rb | 4 ++-- lib/learnsprout/section.rb | 4 ++-- lib/learnsprout/student.rb | 4 ++-- lib/learnsprout/teacher.rb | 4 ++-- lib/learnsprout/term.rb | 4 ++-- 7 files changed, 14 insertions(+), 14 deletions(-) diff --git a/lib/learnsprout/course.rb b/lib/learnsprout/course.rb index 5755db7..70cb3b1 100644 --- a/lib/learnsprout/course.rb +++ b/lib/learnsprout/course.rb @@ -1,7 +1,7 @@ module LearnSprout class Course - attr_accessor :course_id, + attr_accessor :id, :course_id, :school_id, :name, :number, @@ -10,7 +10,7 @@ class Course def initialize(attrs={}) @client = attrs["client"] @org_id = attrs["org_id"] - @course_id = attrs["id"] + self.id = @course_id = attrs["id"] @name = attrs["name"] @number = attrs["number"] @time_updated = attrs["time_updated"] 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/school.rb b/lib/learnsprout/school.rb index 13c1daf..31ac840 100644 --- a/lib/learnsprout/school.rb +++ b/lib/learnsprout/school.rb @@ -1,7 +1,7 @@ module LearnSprout class School - attr_accessor :school_id, + attr_accessor :id, :school_id, :name, :number, :nces, @@ -12,7 +12,7 @@ class School def initialize(attrs={}) @client = attrs["client"] @org_id = attrs["org_id"] - @school_id = attrs["id"] + self.id = @school_id = attrs["id"] @name = attrs["name"] @number = attrs["number"] @nces = Nces.new(attrs["nces"]) diff --git a/lib/learnsprout/section.rb b/lib/learnsprout/section.rb index 80c68f9..6fb884e 100644 --- a/lib/learnsprout/section.rb +++ b/lib/learnsprout/section.rb @@ -1,7 +1,7 @@ module LearnSprout class Section - attr_accessor :term_id, + attr_accessor :id, :term_id, :section_id, :number, :room, @@ -13,7 +13,7 @@ class Section def initialize(attrs={}) @client = attrs["client"] @org_id = attrs["org_id"] - @section_id = attrs["id"] + self.id = @section_id = attrs["id"] @number = attrs["number"] @room = attrs["room"] @term_id = attrs["term"] && attrs["term"]["id"] diff --git a/lib/learnsprout/student.rb b/lib/learnsprout/student.rb index eb49f3c..ff082d9 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, @@ -19,7 +19,7 @@ class Student def initialize(attrs={}) @client = attrs["client"] @org_id = attrs["org_id"] - @student_id = attrs["id"] + self.id = @student_id = attrs["id"] @last_name = attrs["last_name"] @grade = attrs["grade"] @address = Address.new(attrs["address"]) diff --git a/lib/learnsprout/teacher.rb b/lib/learnsprout/teacher.rb index 2ba0ab4..dc0d3fc 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, @@ -16,7 +16,7 @@ class Teacher def initialize(attrs={}) @client = attrs["client"] @org_id = attrs["org_id"] - @teacher_id = attrs["id"] + self.id = @teacher_id = attrs["id"] @first_name = attrs["first_name"] @middle_name = attrs["middle_name"] @last_name = attrs["last_name"] diff --git a/lib/learnsprout/term.rb b/lib/learnsprout/term.rb index b6b383c..7ea9860 100644 --- a/lib/learnsprout/term.rb +++ b/lib/learnsprout/term.rb @@ -1,7 +1,7 @@ module LearnSprout class Term - attr_accessor :term_id, + attr_accessor :id, :term_id, :name, :end_date, :start_date, @@ -11,7 +11,7 @@ class Term def initialize(attrs={}) @client = attrs["client"] @org_id = attrs["org_id"] - @term_id = attrs["id"] + self.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"]) From bb3335c8b8233f7a0337a6eb013f112802cf83dc Mon Sep 17 00:00:00 2001 From: "Thomas R. Koll" Date: Mon, 6 Aug 2012 19:22:34 +0200 Subject: [PATCH 02/15] Convert unix timestamp of time_updated into updated_at as a proper Time object --- lib/learnsprout/course.rb | 4 +++- lib/learnsprout/school.rb | 4 +++- lib/learnsprout/section.rb | 1 + lib/learnsprout/student.rb | 2 ++ lib/learnsprout/teacher.rb | 2 ++ lib/learnsprout/term.rb | 4 +++- 6 files changed, 14 insertions(+), 3 deletions(-) diff --git a/lib/learnsprout/course.rb b/lib/learnsprout/course.rb index 70cb3b1..23ed7c2 100644 --- a/lib/learnsprout/course.rb +++ b/lib/learnsprout/course.rb @@ -5,7 +5,8 @@ class Course :school_id, :name, :number, - :time_updated + :time_updated, + :updated_at def initialize(attrs={}) @client = attrs["client"] @@ -14,6 +15,7 @@ def initialize(attrs={}) @name = attrs["name"] @number = attrs["number"] @time_updated = attrs["time_updated"] + @updated_at = Time.at(@time_updated) @school_id = attrs["school"]["id"] end diff --git a/lib/learnsprout/school.rb b/lib/learnsprout/school.rb index 31ac840..2dc0900 100644 --- a/lib/learnsprout/school.rb +++ b/lib/learnsprout/school.rb @@ -7,7 +7,8 @@ class School :nces, :phone, :address, - :time_updated + :time_updated, + :updated_at def initialize(attrs={}) @client = attrs["client"] @@ -19,6 +20,7 @@ def initialize(attrs={}) @phone = Phone.new(attrs["phone"]) @address = Address.new(attrs["address"]) @time_updated = attrs["time_updated"] + @updated_at = Time.at(@time_updated) end #TODO Add org method? diff --git a/lib/learnsprout/section.rb b/lib/learnsprout/section.rb index 6fb884e..be356ae 100644 --- a/lib/learnsprout/section.rb +++ b/lib/learnsprout/section.rb @@ -21,6 +21,7 @@ def initialize(attrs={}) @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) @student_ids = [] if attrs["students"] attrs["students"].each do |student| diff --git a/lib/learnsprout/student.rb b/lib/learnsprout/student.rb index ff082d9..94519a6 100644 --- a/lib/learnsprout/student.rb +++ b/lib/learnsprout/student.rb @@ -11,6 +11,7 @@ class Student :birthday, :first_name, :time_updated, + :updated_at, :middle_name, :gender, :exit_date, @@ -29,6 +30,7 @@ def initialize(attrs={}) @birthday = attrs["birthday"] && Date.parse(attrs["birthday"]) @first_name = attrs["first_name"] @time_updated = attrs["time_updated"] + @updated_at = Time.at(@time_updated) @middle_name = attrs["middle_name"] @gender = attrs["gender"] @exit_date = attrs["exit_date"] && Date.parse(attrs["exit_date"]) diff --git a/lib/learnsprout/teacher.rb b/lib/learnsprout/teacher.rb index dc0d3fc..8dc8255 100644 --- a/lib/learnsprout/teacher.rb +++ b/lib/learnsprout/teacher.rb @@ -11,6 +11,7 @@ class Teacher :number, :phone, :time_updated, + :updated_at, :email def initialize(attrs={}) @@ -26,6 +27,7 @@ def initialize(attrs={}) @number = attrs["number"] @phone = attrs["phone"] && Phone.new(attrs["phone"]) @time_updated = attrs["time_updated"] + @updated_at = Time.at(@time_updated) @email = attrs["email"] end diff --git a/lib/learnsprout/term.rb b/lib/learnsprout/term.rb index 7ea9860..cc22583 100644 --- a/lib/learnsprout/term.rb +++ b/lib/learnsprout/term.rb @@ -6,7 +6,8 @@ class Term :end_date, :start_date, :school_id, - :time_updated + :time_updated, + :updated_at def initialize(attrs={}) @client = attrs["client"] @@ -17,6 +18,7 @@ def initialize(attrs={}) @start_date = attrs["start_date"] && Date.parse(attrs["start_date"]) @school_id = attrs["school_id"] @time_updated = attrs["time_updated"] + @updated_at = Time.at(@time_updated) @section_ids = [] if attrs["sections"] attrs["sections"].each do |section| From 89928c44289da6b2fb1dcb41d574a24628ae3f5c Mon Sep 17 00:00:00 2001 From: "Thomas R. Koll" Date: Mon, 6 Aug 2012 19:24:07 +0200 Subject: [PATCH 03/15] Add Page#any? so we know when it is enough --- lib/learnsprout/page.rb | 58 ++++++++++++++++++++++++----------------- 1 file changed, 34 insertions(+), 24 deletions(-) diff --git a/lib/learnsprout/page.rb b/lib/learnsprout/page.rb index 5281d64..552d5dc 100644 --- a/lib/learnsprout/page.rb +++ b/lib/learnsprout/page.rb @@ -1,33 +1,43 @@ module LearnSprout - class Page - include LearnSprout::Connection + class Page + include LearnSprout::Connection - attr_accessor :items + attr_accessor :items - def initialize(url, type, extras = {}) - @items = [] - @type = type - @extras = extras - data = get(url) + def initialize(url, type, extras = {}) + return false if url.nil? || url == "" + @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 + @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 + #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 - def next - return Page.new(@nextUrl, @type, @extras) - end + + # page = api_client.schools(org_id) + # while page.any? do + # ... + # end + def any? + @items && @items.any? + end + + def next + return Page.new(@nextUrl, @type, @extras) end + end end From 18aeb269456ebc721d3d994e7e89a3f01abbd26e Mon Sep 17 00:00:00 2001 From: "Thomas R. Koll" Date: Tue, 7 Aug 2012 07:11:55 +0200 Subject: [PATCH 04/15] Add Page#each --- lib/learnsprout/page.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/learnsprout/page.rb b/lib/learnsprout/page.rb index 552d5dc..8ffeab3 100644 --- a/lib/learnsprout/page.rb +++ b/lib/learnsprout/page.rb @@ -36,6 +36,12 @@ 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 From 3ae09d735b8d9aa33994ede9fa03115fcc94dd14 Mon Sep 17 00:00:00 2001 From: "Thomas R. Koll" Date: Tue, 7 Aug 2012 11:36:32 +0200 Subject: [PATCH 05/15] Normalize indention --- lib/learnsprout/address.rb | 8 +++---- lib/learnsprout/course.rb | 18 +++++++-------- lib/learnsprout/nces.rb | 4 ++-- lib/learnsprout/phone.rb | 6 ++--- lib/learnsprout/school.rb | 30 ++++++++++++------------- lib/learnsprout/section.rb | 46 +++++++++++++++++++------------------- lib/learnsprout/student.rb | 34 ++++++++++++++-------------- lib/learnsprout/teacher.rb | 30 ++++++++++++------------- lib/learnsprout/term.rb | 36 ++++++++++++++--------------- 9 files changed, 106 insertions(+), 106 deletions(-) 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 23ed7c2..519cf27 100644 --- a/lib/learnsprout/course.rb +++ b/lib/learnsprout/course.rb @@ -9,18 +9,18 @@ class Course :updated_at def initialize(attrs={}) - @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) - @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) + @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/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 2dc0900..1c3c7c8 100644 --- a/lib/learnsprout/school.rb +++ b/lib/learnsprout/school.rb @@ -11,22 +11,22 @@ class School :updated_at def initialize(attrs={}) - @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"]) - @address = Address.new(attrs["address"]) - @time_updated = attrs["time_updated"] - @updated_at = Time.at(@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"]) + @address = Address.new(attrs["address"]) + @time_updated = attrs["time_updated"] + @updated_at = Time.at(@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 @@ -34,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 @@ -42,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 @@ -50,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 @@ -62,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 be356ae..68388a1 100644 --- a/lib/learnsprout/section.rb +++ b/lib/learnsprout/section.rb @@ -11,47 +11,47 @@ class Section :course_id def initialize(attrs={}) - @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) - @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) + @student_ids = [] + if attrs["students"] + attrs["students"].each do |student| + @student_ids.push student["id"] 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 94519a6..cd4a74c 100644 --- a/lib/learnsprout/student.rb +++ b/lib/learnsprout/student.rb @@ -18,23 +18,23 @@ class Student :entry_date def initialize(attrs={}) - @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) - @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) + @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 8dc8255..fa293b1 100644 --- a/lib/learnsprout/teacher.rb +++ b/lib/learnsprout/teacher.rb @@ -15,24 +15,24 @@ class Teacher :email def initialize(attrs={}) - @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) - @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) + @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 cc22583..af5227c 100644 --- a/lib/learnsprout/term.rb +++ b/lib/learnsprout/term.rb @@ -10,21 +10,21 @@ class Term :updated_at def initialize(attrs={}) - @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"]) - @start_date = attrs["start_date"] && Date.parse(attrs["start_date"]) - @school_id = attrs["school_id"] - @time_updated = attrs["time_updated"] - @updated_at = Time.at(@time_updated) - @section_ids = [] - if attrs["sections"] - attrs["sections"].each do |section| - @section_ids.push section["id"] - 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"]) + @start_date = attrs["start_date"] && Date.parse(attrs["start_date"]) + @school_id = attrs["school_id"] + @time_updated = attrs["time_updated"] + @updated_at = Time.at(@time_updated) + @section_ids = [] + if attrs["sections"] + attrs["sections"].each do |section| + @section_ids.push section["id"] end + end end # Gets the sections for this term. This method is extremely slow as it retrieves a list of IDs @@ -34,11 +34,11 @@ def initialize(attrs={}) 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 + @section_ids.each do |section_id| + temp_sections.push @client.section(@org_id, section_id) + end end return temp_sections end end -end +end \ No newline at end of file From c05ee7f79cf1dbed27ba3687de66eeb1f024abd1 Mon Sep 17 00:00:00 2001 From: "Thomas R. Koll" Date: Tue, 7 Aug 2012 20:47:11 +0200 Subject: [PATCH 06/15] Improve Term#sections --- lib/learnsprout/section.rb | 3 ++- lib/learnsprout/term.rb | 23 +++++++++-------------- 2 files changed, 11 insertions(+), 15 deletions(-) diff --git a/lib/learnsprout/section.rb b/lib/learnsprout/section.rb index 68388a1..1467c6b 100644 --- a/lib/learnsprout/section.rb +++ b/lib/learnsprout/section.rb @@ -8,7 +8,8 @@ class Section :time_updated, :teacher_id, :school_id, - :course_id + :course_id, + :student_ids def initialize(attrs={}) @client = attrs["client"] diff --git a/lib/learnsprout/term.rb b/lib/learnsprout/term.rb index af5227c..796c5a9 100644 --- a/lib/learnsprout/term.rb +++ b/lib/learnsprout/term.rb @@ -7,7 +7,8 @@ class Term :start_date, :school_id, :time_updated, - :updated_at + :updated_at, + :section_ids def initialize(attrs={}) @client = attrs["client"] @@ -21,24 +22,18 @@ def initialize(attrs={}) @updated_at = Time.at(@time_updated) @section_ids = [] if attrs["sections"] - attrs["sections"].each do |section| - @section_ids.push section["id"] - end + @section_ids = attrs["sections"].collect { |section| section["id"] } end 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 \ No newline at end of file From 34d9b9ce4863d666e6594aeaf14bcf2af3576a99 Mon Sep 17 00:00:00 2001 From: "Thomas R. Koll" Date: Mon, 13 Aug 2012 15:37:03 +0200 Subject: [PATCH 07/15] Fail-proof against time_update being nil --- lib/learnsprout/course.rb | 2 +- lib/learnsprout/school.rb | 2 +- lib/learnsprout/section.rb | 2 +- lib/learnsprout/student.rb | 2 +- lib/learnsprout/teacher.rb | 2 +- lib/learnsprout/term.rb | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/learnsprout/course.rb b/lib/learnsprout/course.rb index 519cf27..e365ca1 100644 --- a/lib/learnsprout/course.rb +++ b/lib/learnsprout/course.rb @@ -15,7 +15,7 @@ def initialize(attrs={}) @name = attrs["name"] @number = attrs["number"] @time_updated = attrs["time_updated"] - @updated_at = Time.at(@time_updated) + @updated_at = Time.at(@time_updated) if @time_updated @school_id = attrs["school"]["id"] end diff --git a/lib/learnsprout/school.rb b/lib/learnsprout/school.rb index 1c3c7c8..54efaee 100644 --- a/lib/learnsprout/school.rb +++ b/lib/learnsprout/school.rb @@ -20,7 +20,7 @@ def initialize(attrs={}) @phone = Phone.new(attrs["phone"]) @address = Address.new(attrs["address"]) @time_updated = attrs["time_updated"] - @updated_at = Time.at(@time_updated) + @updated_at = Time.at(@time_updated) if @time_updated end #TODO Add org method? diff --git a/lib/learnsprout/section.rb b/lib/learnsprout/section.rb index 1467c6b..08e8fa7 100644 --- a/lib/learnsprout/section.rb +++ b/lib/learnsprout/section.rb @@ -22,7 +22,7 @@ def initialize(attrs={}) @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) + @updated_at = Time.at(@time_updated) if @time_updated @student_ids = [] if attrs["students"] attrs["students"].each do |student| diff --git a/lib/learnsprout/student.rb b/lib/learnsprout/student.rb index cd4a74c..a6ad47e 100644 --- a/lib/learnsprout/student.rb +++ b/lib/learnsprout/student.rb @@ -30,7 +30,7 @@ def initialize(attrs={}) @birthday = attrs["birthday"] && Date.parse(attrs["birthday"]) @first_name = attrs["first_name"] @time_updated = attrs["time_updated"] - @updated_at = Time.at(@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"]) diff --git a/lib/learnsprout/teacher.rb b/lib/learnsprout/teacher.rb index fa293b1..3bdd0c0 100644 --- a/lib/learnsprout/teacher.rb +++ b/lib/learnsprout/teacher.rb @@ -27,7 +27,7 @@ def initialize(attrs={}) @number = attrs["number"] @phone = attrs["phone"] && Phone.new(attrs["phone"]) @time_updated = attrs["time_updated"] - @updated_at = Time.at(@time_updated) + @updated_at = Time.at(@time_updated) if @time_updated @email = attrs["email"] end diff --git a/lib/learnsprout/term.rb b/lib/learnsprout/term.rb index 796c5a9..db0ed60 100644 --- a/lib/learnsprout/term.rb +++ b/lib/learnsprout/term.rb @@ -19,7 +19,7 @@ def initialize(attrs={}) @start_date = attrs["start_date"] && Date.parse(attrs["start_date"]) @school_id = attrs["school_id"] @time_updated = attrs["time_updated"] - @updated_at = Time.at(@time_updated) + @updated_at = Time.at(@time_updated) if @time_updated @section_ids = [] if attrs["sections"] @section_ids = attrs["sections"].collect { |section| section["id"] } From ad4eadfec46cabc121773c566a8813b5c7b1c3b7 Mon Sep 17 00:00:00 2001 From: Zachary Crockett Date: Wed, 5 Sep 2012 00:56:07 -0500 Subject: [PATCH 08/15] Fix incorrect parameter in client step definition causing students.feature:18 to fail --- features/step_definitions/client_steps.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 From cb0b0a9adeaf9baff5732151473cc78a9a7a4a66 Mon Sep 17 00:00:00 2001 From: Zachary Crockett Date: Wed, 5 Sep 2012 00:57:13 -0500 Subject: [PATCH 09/15] Only return sections for a Term, not all sections for a School --- lib/learnsprout/term.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/learnsprout/term.rb b/lib/learnsprout/term.rb index db0ed60..d4b2565 100644 --- a/lib/learnsprout/term.rb +++ b/lib/learnsprout/term.rb @@ -31,9 +31,9 @@ def 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) + @sections.push section if @section_ids.include?(section.id) end return @sections end end -end \ No newline at end of file +end From 098690795f5249b12fe792e5c268b04de5114cf9 Mon Sep 17 00:00:00 2001 From: Zachary Crockett Date: Wed, 5 Sep 2012 03:35:27 -0500 Subject: [PATCH 10/15] Prevent exception due to nil data --- lib/learnsprout/page.rb | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/learnsprout/page.rb b/lib/learnsprout/page.rb index 8ffeab3..ecb4ae5 100644 --- a/lib/learnsprout/page.rb +++ b/lib/learnsprout/page.rb @@ -19,11 +19,10 @@ def initialize(url, type, extras = {}) end #TODO handle non-page URL? - data["data"].each do |item| - extras.each do |key, value| - item[key] = value + if data['data'] + data['data'].each do |item| + @items << type.new(item.merge(extras)) end - @items << type.new(item) end end From 437ab64b7eab2aee9b08b107a5d57c6d8291fdf6 Mon Sep 17 00:00:00 2001 From: "Thomas R. Koll" Date: Wed, 5 Sep 2012 12:50:56 +0100 Subject: [PATCH 11/15] students in section are now a proper array in Learnsprout --- lib/learnsprout/section.rb | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/lib/learnsprout/section.rb b/lib/learnsprout/section.rb index 08e8fa7..ec3487d 100644 --- a/lib/learnsprout/section.rb +++ b/lib/learnsprout/section.rb @@ -23,12 +23,7 @@ def initialize(attrs={}) @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| - @student_ids.push student["id"] - end - end + @student_ids = attrs["students"] || [] end def term From 9198af6163a8970cb1bebe14c93bc6eeafa9d056 Mon Sep 17 00:00:00 2001 From: "Thomas R. Koll" Date: Wed, 12 Sep 2012 07:35:41 +0200 Subject: [PATCH 12/15] Part-revert last commit and make section.student work both ways --- lib/learnsprout/section.rb | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/learnsprout/section.rb b/lib/learnsprout/section.rb index ec3487d..0382f26 100644 --- a/lib/learnsprout/section.rb +++ b/lib/learnsprout/section.rb @@ -23,7 +23,16 @@ def initialize(attrs={}) @course_id = attrs["course"] && attrs["course"]["id"] @time_updated = attrs["time_updated"] @updated_at = Time.at(@time_updated) if @time_updated - @student_ids = attrs["students"] || [] + @student_ids = [] + if attrs["students"] + attrs["students"].each do |student| + if student["id"].is_a?(Hash) + @student_ids.push student["id"] + else + @student_ids.push student + end + end + end end def term From 4f9f017e83f982e0f9de3d45dd2ab4fd2ee5b7f4 Mon Sep 17 00:00:00 2001 From: "Thomas R. Koll" Date: Fri, 14 Sep 2012 13:38:14 +0200 Subject: [PATCH 13/15] Add Term#current? --- lib/learnsprout/term.rb | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/learnsprout/term.rb b/lib/learnsprout/term.rb index d4b2565..245b3e5 100644 --- a/lib/learnsprout/term.rb +++ b/lib/learnsprout/term.rb @@ -15,8 +15,8 @@ def initialize(attrs={}) @org_id = attrs["org_id"] self.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"]) + @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 @@ -26,6 +26,14 @@ def initialize(attrs={}) 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 + def sections return @sections if @sections return [] if @section_ids.empty? From 3142e163f73ed6ac24375e05f04b15c99205ed42 Mon Sep 17 00:00:00 2001 From: "Thomas R. Koll" Date: Mon, 17 Sep 2012 08:40:18 +0200 Subject: [PATCH 14/15] Small bugfix --- lib/learnsprout/section.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/learnsprout/section.rb b/lib/learnsprout/section.rb index 0382f26..d3f56f2 100644 --- a/lib/learnsprout/section.rb +++ b/lib/learnsprout/section.rb @@ -26,7 +26,7 @@ def initialize(attrs={}) @student_ids = [] if attrs["students"] attrs["students"].each do |student| - if student["id"].is_a?(Hash) + if student.is_a?(Hash) @student_ids.push student["id"] else @student_ids.push student From 095138824fb89599506a8c97d1edb230a53c7857 Mon Sep 17 00:00:00 2001 From: "Thomas R. Koll" Date: Tue, 25 Sep 2012 15:30:19 +0200 Subject: [PATCH 15/15] Cover case where phone is nil --- lib/learnsprout/school.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/learnsprout/school.rb b/lib/learnsprout/school.rb index 54efaee..7f638c5 100644 --- a/lib/learnsprout/school.rb +++ b/lib/learnsprout/school.rb @@ -17,7 +17,7 @@ def initialize(attrs={}) @name = attrs["name"] @number = attrs["number"] @nces = Nces.new(attrs["nces"]) - @phone = Phone.new(attrs["phone"]) + @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