learnsprout 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,209 @@
1
+ module LearnSprout
2
+ class Client
3
+ include LearnSprout::Connection
4
+
5
+ def initialize(api_key)
6
+ @api_key = api_key
7
+ end
8
+
9
+ def org(org_id)
10
+ org = get("org/#{org_id}?apikey=#{@api_key}")
11
+ org["client"] = self
12
+ Org.new(org)
13
+ end
14
+
15
+ def orgs
16
+ orgs = get("org?apikey=#{@api_key}")
17
+ instances_from_orgs(orgs)
18
+ end
19
+
20
+ def school(org_id, school_id)
21
+ school = get("org/#{org_id}/school/#{school_id}?apikey=#{@api_key}")
22
+ school["org_id"] = org_id
23
+ school["client"] = self
24
+ School.new(school)
25
+ end
26
+
27
+ def schools(org_id)
28
+ schools = get("org/#{org_id}/school?apikey=#{@api_key}")
29
+ instances_from_schools(org_id, schools)
30
+ end
31
+
32
+ def student(org_id, student_id)
33
+ student = get("org/#{org_id}/student/#{student_id}?apikey=#{@api_key}")
34
+ student["org_id"] = org_id
35
+ student["client"] = self
36
+ Student.new(student)
37
+ end
38
+
39
+ def students(org_id, options = {})
40
+ school_id = options[:school_id]
41
+ if (school_id)
42
+ students = get("org/#{org_id}/school/#{school_id}/student?apikey=#{@api_key}")
43
+ else
44
+ students = get("org/#{org_id}/student?apikey=#{@api_key}")
45
+ end
46
+ instances_from_students(org_id, students)
47
+ end
48
+
49
+ def section(org_id, section_id)
50
+ url = "org/#{org_id}/section/#{section_id}?apikey=#{@api_key}"
51
+ section = get(url)
52
+ section["org_id"] = org_id
53
+ section["client"] = self
54
+ Section.new(section)
55
+ end
56
+
57
+ def sections(org_id, options = {})
58
+ school_id = options[:school_id]
59
+ if (school_id)
60
+ url = "org/#{org_id}/school/#{school_id}/section?apikey=#{@api_key}"
61
+ else
62
+ url = "org/#{org_id}/section?apikey=#{@api_key}"
63
+ end
64
+ sections = get(url)
65
+ instances_from_sections(org_id, sections)
66
+ end
67
+
68
+ def teacher(org_id, teacher_id)
69
+ url = "org/#{org_id}/teacher/#{teacher_id}?apikey=#{@api_key}"
70
+ teacher = get(url)
71
+ teacher["org_id"] = org_id
72
+ teacher["client"] = self
73
+ Teacher.new(teacher)
74
+ end
75
+
76
+ def teachers(org_id, options = {})
77
+ school_id = options[:school_id]
78
+ if (school_id)
79
+ url = "org/#{org_id}/school/#{school_id}/teacher?apikey=#{@api_key}"
80
+ else
81
+ url = "org/#{org_id}/teacher?apikey=#{@api_key}"
82
+ end
83
+ teachers = get(url)
84
+ instances_from_teachers(org_id, teachers)
85
+ end
86
+
87
+ def term(org_id, term_id)
88
+ url = "org/#{org_id}/term/#{term_id}?apikey=#{@api_key}"
89
+ term = get(url)
90
+ term["org_id"] = org_id
91
+ term["client"] = self
92
+ Term.new(term)
93
+ end
94
+
95
+ def terms(org_id, options = {})
96
+ school_id = options[:school_id]
97
+ if (school_id)
98
+ url = "org/#{org_id}/school/#{school_id}/term?apikey=#{@api_key}"
99
+ else
100
+ url = "org/#{org_id}/term?apikey=#{@api_key}"
101
+ end
102
+ terms = get(url)
103
+ instances_from_terms(org_id, terms)
104
+ end
105
+
106
+ def current_term(org_id, school_id)
107
+ url = "org/#{org_id}/school/#{school_id}/term/current?apikey=#{@api_key}"
108
+ term = get(url)
109
+ term["org_id"] = org_id
110
+ term["client"] = self
111
+ Term.new(term)
112
+ end
113
+
114
+ def course(org_id, course_id)
115
+ url = "org/#{org_id}/course/#{course_id}?apikey=#{@api_key}"
116
+ course = get(url)
117
+ course["org_id"] = org_id
118
+ course["client"] = self
119
+ Course.new(course)
120
+ end
121
+
122
+ def courses(org_id, options = {})
123
+ school_id = options[:school_id]
124
+ if (school_id)
125
+ url = "org/#{org_id}/school/#{school_id}/course?apikey=#{@api_key}"
126
+ else
127
+ url = "org/#{org_id}/course?apikey=#{@api_key}"
128
+ end
129
+ courses = get(url)
130
+ instances_from_courses(org_id, courses)
131
+ end
132
+
133
+ private
134
+ def instances_from_orgs(orgs)
135
+ org_instances = []
136
+ orgs.each do |org|
137
+ org["client"] = self
138
+ org_instances << Org.new(org)
139
+ end
140
+ return org_instances
141
+ end
142
+
143
+ def instances_from_schools(org_id, schools)
144
+ school_instances = []
145
+ #TODO Next
146
+ schools["data"].each do |school|
147
+ school["client"] = self
148
+ school["org_id"] = org_id
149
+ school_instances << School.new(school)
150
+ end
151
+ return school_instances
152
+ end
153
+
154
+ def instances_from_students(org_id, students)
155
+ student_instances = []
156
+ #TODO Next
157
+ students["data"].each do |student|
158
+ student["client"] = self
159
+ student["org_id"] = org_id
160
+ student_instances << Student.new(student)
161
+ end
162
+ return student_instances
163
+ end
164
+
165
+ def instances_from_sections(org_id, sections)
166
+ section_instances = []
167
+ #TODO Next
168
+ sections["data"].each do |section|
169
+ section["client"] = self
170
+ section["org_id"] = org_id
171
+ section_instances << Section.new(section)
172
+ end
173
+ return section_instances
174
+ end
175
+
176
+ def instances_from_teachers(org_id, teachers)
177
+ teacher_instances = []
178
+ #TODO Next
179
+ teachers["data"].each do |teacher|
180
+ teacher["client"] = self
181
+ teacher["org_id"] = org_id
182
+ teacher_instances << Teacher.new(teacher)
183
+ end
184
+ return teacher_instances
185
+ end
186
+
187
+ def instances_from_terms(org_id, terms)
188
+ term_instances = []
189
+ #TODO Next
190
+ terms["data"].each do |term|
191
+ term["client"] = self
192
+ term["org_id"] = org_id
193
+ term_instances << Term.new(term)
194
+ end
195
+ return term_instances
196
+ end
197
+
198
+ def instances_from_courses(org_id, courses)
199
+ course_instances = []
200
+ #TODO Next
201
+ courses["data"].each do |course|
202
+ course["client"] = self
203
+ course["org_id"] = org_id
204
+ course_instances << Course.new(course)
205
+ end
206
+ return course_instances
207
+ end
208
+ end
209
+ end
@@ -0,0 +1,45 @@
1
+ module LearnSprout
2
+ module Connection
3
+ private
4
+ def connection
5
+ default_options = {
6
+ :headers => {
7
+ :accept => 'application/json',
8
+ :content_type => 'application/json',
9
+ :user_agent => LearnSprout.user_agent,
10
+ }
11
+ }
12
+
13
+ @connection ||= Faraday.new(LearnSprout.endpoint, default_options) do |builder|
14
+ builder.use LearnSprout::Response::FollowRedirects
15
+ builder.use LearnSprout::Response::ParseJson
16
+
17
+ builder.use Faraday::Request::UrlEncoded
18
+
19
+ builder.response :logger if LearnSprout.debugging?
20
+ builder.adapter Faraday.default_adapter
21
+ end
22
+ end
23
+
24
+ def get(path, params={})
25
+ request(:get, path, params)
26
+ end
27
+
28
+ def post(path, params={})
29
+ request(:post, path, params)
30
+ end
31
+
32
+ def request(method, path, params)
33
+ response = connection.send(method) do |request|
34
+ case method.to_sym
35
+ when :delete, :get
36
+ request.url(path)
37
+ when :post
38
+ request.path = path
39
+ request.body = MultiJson.encode(params) unless params.empty?
40
+ end
41
+ end
42
+ response.body
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,25 @@
1
+ module LearnSprout
2
+ class Course
3
+ include LearnSprout::Connection
4
+
5
+ attr_accessor :course_id,
6
+ :school_id,
7
+ :name,
8
+ :number,
9
+ :time_updated
10
+
11
+ def initialize(attrs={})
12
+ @client = attrs["client"]
13
+ @org_id = attrs["org_id"]
14
+ @course_id = attrs["id"]
15
+ @name = attrs["name"]
16
+ @number = attrs["number"]
17
+ @time_updated = attrs["time_updated"]
18
+ @school_id = attrs["school"]["id"]
19
+ end
20
+
21
+ def school
22
+ @client.school(@org_id, @school_id)
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,5 @@
1
+ module LearnSprout
2
+ class RequestException < Exception
3
+ end
4
+ end
5
+
@@ -0,0 +1,12 @@
1
+ module LearnSprout
2
+ class Nces
3
+
4
+ attr_accessor :district_id,
5
+ :school_id
6
+
7
+ def initialize(attrs={})
8
+ @district_id = attrs["district_id"]
9
+ @school_id = attrs["school_id"]
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,62 @@
1
+ module LearnSprout
2
+ class Org
3
+ include LearnSprout::Connection
4
+
5
+ attr_accessor :org_id,
6
+ :name
7
+
8
+ def initialize(attrs={})
9
+ @client = attrs["client"]
10
+ @org_id = attrs["id"]
11
+ @name = attrs["name"]
12
+ end
13
+
14
+ def school(school_id)
15
+ @client.school(@org_id, school_id)
16
+ end
17
+
18
+ def schools
19
+ @client.schools(@org_id)
20
+ end
21
+
22
+ def students
23
+ @client.students(@org_id)
24
+ end
25
+
26
+ def student(student_id)
27
+ @client.student(@org_id, student_id)
28
+ end
29
+
30
+ def sections
31
+ @client.sections(@org_id)
32
+ end
33
+
34
+ def section(section_id)
35
+ @client.section(@org_id, section_id)
36
+ end
37
+
38
+ def teachers
39
+ @client.teachers(@org_id)
40
+ end
41
+
42
+ def teacher(teacher_id)
43
+ @client.teacher(@org_id, teacher_id)
44
+ end
45
+
46
+ def terms
47
+ @client.terms(@org_id)
48
+ end
49
+
50
+ def term(term_id)
51
+ @client.term(@org_id, term_id)
52
+ end
53
+
54
+ def courses
55
+ @client.courses(@org_id)
56
+ end
57
+
58
+ def course(course_id)
59
+ @client.course(@org_id, course_id)
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,12 @@
1
+ module LearnSprout
2
+ class Phone
3
+
4
+ attr_accessor :fax,
5
+ :main
6
+
7
+ def initialize(attrs={})
8
+ @fax = attrs["fax"]
9
+ @main = attrs["main"]
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,44 @@
1
+ module LearnSprout
2
+ module Response
3
+ class RedirectLimitReached < Faraday::Error::ClientError
4
+ attr_reader :response
5
+
6
+ def initialize(response)
7
+ super "too many redirects; last one to: #{response['location']}"
8
+ @response = response
9
+ end
10
+ end
11
+
12
+ class FollowRedirects < Faraday::Middleware
13
+ REDIRECTS = [301, 302, 303, 307]
14
+ # default value for max redirects followed
15
+ FOLLOW_LIMIT = 3
16
+
17
+ def initialize(app, options = {})
18
+ super(app)
19
+ @options = options
20
+ @follow_limit = options[:limit] || FOLLOW_LIMIT
21
+ end
22
+
23
+ def call(env)
24
+ process_response(@app.call(env), @follow_limit)
25
+ end
26
+
27
+ def process_response(response, follows)
28
+ response.on_complete do |env|
29
+ if redirect? response
30
+ raise RedirectLimitReached, response if follows.zero?
31
+ env[:url] += response['location']
32
+ env[:method] = :get
33
+ response = process_response(@app.call(env), follows - 1)
34
+ end
35
+ end
36
+ response
37
+ end
38
+
39
+ def redirect?(response)
40
+ REDIRECTS.include? response.status
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,22 @@
1
+ module LearnSprout
2
+ module Response
3
+ class ParseJson < Faraday::Response::Middleware
4
+ def on_complete(env)
5
+ if respond_to? :parse
6
+ env[:body] = parse(env[:body]) unless [204,302,304,307].index env[:status]
7
+ end
8
+ end
9
+
10
+ def parse(body)
11
+ case body
12
+ when ''
13
+ nil
14
+ else
15
+ response_hash = ::MultiJson.decode(body)
16
+
17
+ #raise LearnSprout::RequestException, response_hash["message"] if response_hash["message"]
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,69 @@
1
+ module LearnSprout
2
+ class School
3
+ include LearnSprout::Connection
4
+
5
+ attr_accessor :school_id,
6
+ :name,
7
+ :number,
8
+ :nces,
9
+ :phone,
10
+ :address
11
+
12
+ def initialize(attrs={})
13
+ @client = attrs["client"]
14
+ @org_id = attrs["org_id"]
15
+ @school_id = attrs["id"]
16
+ @name = attrs["name"]
17
+ @number = attrs["number"]
18
+ @nces = Nces.new(attrs["nces"])
19
+ @phone = Phone.new(attrs["phone"])
20
+ @address = Address.new(attrs["address"])
21
+ end
22
+
23
+ #TODO Add org method?
24
+
25
+ def student(student_id)
26
+ @client.student(@org_id, student_id)
27
+ end
28
+
29
+ def students
30
+ @client.students(@org_id, :school_id => @school_id)
31
+ end
32
+
33
+ def section(section_id)
34
+ @client.section(@org_id, section_id)
35
+ end
36
+
37
+ def sections
38
+ @client.sections(@org_id, :school_id => @school_id)
39
+ end
40
+
41
+ def teacher(teacher_id)
42
+ @client.teacher(@org_id, teacher_id)
43
+ end
44
+
45
+ def teachers
46
+ @client.teachers(@org_id, :school_id => @school_id)
47
+ end
48
+
49
+ def term(term_id)
50
+ @client.term(@org_id, term_id)
51
+ end
52
+
53
+ def terms
54
+ @client.terms(@org_id, :school_id => @school_id)
55
+ end
56
+
57
+ def current_term
58
+ @client.current_term(@org_id, @school_id)
59
+ end
60
+
61
+ def course(course_id)
62
+ @client.course(@org_id, course_id)
63
+ end
64
+
65
+ def courses
66
+ @client.courses(@org_id, :school_id => @school_id)
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,18 @@
1
+ module LearnSprout
2
+ class Section
3
+ include LearnSprout::Connection
4
+
5
+ attr_accessor :section_id,
6
+ :number,
7
+ :room
8
+
9
+ def initialize(attrs={})
10
+ @client = attrs["client"]
11
+ @org_id = attrs["org_id"]
12
+ @section_id = attrs["id"]
13
+ @number = attrs["number"]
14
+ @room = attrs["room"]
15
+ #TODO: Other attrs
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,16 @@
1
+ module LearnSprout
2
+ class Student
3
+ include LearnSprout::Connection
4
+
5
+ attr_accessor :student_id,
6
+ :name
7
+
8
+ def initialize(attrs={})
9
+ @client = attrs["client"]
10
+ @org_id = attrs["org_id"]
11
+ @student_id = attrs["id"]
12
+ @name = attrs["name"]
13
+ #TODO: Other attrs
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,30 @@
1
+ module LearnSprout
2
+ class Teacher
3
+ include LearnSprout::Connection
4
+
5
+ attr_accessor :teacher_id,
6
+ :first_name,
7
+ :middle_name,
8
+ :last_name,
9
+ :title,
10
+ :number,
11
+ :time_updated,
12
+ :email
13
+
14
+ #TODO Other fields
15
+
16
+ def initialize(attrs={})
17
+ @client = attrs["client"]
18
+ @org_id = attrs["org_id"]
19
+ @teacher_id = attrs["id"]
20
+ @first_name = attrs["first_name"]
21
+ @middle_name = attrs["middle_name"]
22
+ @last_name = attrs["last_name"]
23
+ @title = attrs["title"]
24
+ @number = attrs["number"]
25
+ @time_updated = attrs["time_updated"]
26
+ @email = attrs["email"]
27
+ #TODO: Other attrs
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,22 @@
1
+ module LearnSprout
2
+ class Term
3
+ include LearnSprout::Connection
4
+
5
+ attr_accessor :term_id,
6
+ :name,
7
+ :end_date,
8
+ :start_date
9
+
10
+ #TODO Other fields
11
+
12
+ def initialize(attrs={})
13
+ @client = attrs["client"]
14
+ @org_id = attrs["org_id"]
15
+ @term_id = attrs["id"]
16
+ @name = attrs["name"]
17
+ @end_date = Date.parse(attrs["end_date"])
18
+ @start_date = Date.parse(attrs["start_date"])
19
+ #TODO: Other attrs
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,4 @@
1
+ module LearnSprout
2
+ VERSION = "0.0.1"
3
+ end
4
+
@@ -0,0 +1,48 @@
1
+ module LearnSprout def self.endpoint=(endpoint)
2
+ @@endpoint = endpoint
3
+ end
4
+
5
+ def self.endpoint
6
+ @@endpoint
7
+ end
8
+
9
+ def self.user_agent=(user_agent)
10
+ @@user_agent = user_agent
11
+ end
12
+
13
+ def self.user_agent
14
+ @@user_agent
15
+ end
16
+
17
+ def self.debugging?
18
+ !!@@debug
19
+ end
20
+
21
+ def self.debug=(debug)
22
+ @@debug = debug
23
+ end
24
+
25
+ self.debug = false
26
+ self.user_agent = "LearnSprout Ruby Wrapper"
27
+ self.endpoint = "https://beta.api.learnsprout.com/"
28
+ end
29
+
30
+ require 'faraday'
31
+ require 'multi_json'
32
+
33
+ require "learnsprout/exceptions"
34
+ require "learnsprout/response/parse_json"
35
+ require "learnsprout/response/follow_redirects"
36
+ require "learnsprout/connection"
37
+ require "learnsprout/client"
38
+ require "learnsprout/org"
39
+ require "learnsprout/school"
40
+ require "learnsprout/student"
41
+ require "learnsprout/section"
42
+ require "learnsprout/teacher"
43
+ require "learnsprout/term"
44
+ require "learnsprout/course"
45
+ require "learnsprout/nces"
46
+ require "learnsprout/phone"
47
+ require "learnsprout/address"
48
+ require "learnsprout/version"