clever_sdk 0.9.0.pre.pre

Sign up to get free protection for your applications and to get access to all the features.
Files changed (72) hide show
  1. checksums.yaml +7 -0
  2. data/.circleci/config.yml +26 -0
  3. data/.env.sample +20 -0
  4. data/.gitignore +22 -0
  5. data/.tool-versions +1 -0
  6. data/CODE_OF_CONDUCT.md +74 -0
  7. data/Gemfile +6 -0
  8. data/LICENSE.txt +21 -0
  9. data/README.md +65 -0
  10. data/Rakefile +10 -0
  11. data/bin/console +31 -0
  12. data/bin/setup +8 -0
  13. data/bin/test +6 -0
  14. data/clever_sdk.gemspec +44 -0
  15. data/lib/clever_sdk/api/authentication.rb +38 -0
  16. data/lib/clever_sdk/api/courses.rb +54 -0
  17. data/lib/clever_sdk/api/districts.rb +23 -0
  18. data/lib/clever_sdk/api/events.rb +26 -0
  19. data/lib/clever_sdk/api/resources.rb +49 -0
  20. data/lib/clever_sdk/api/response.rb +59 -0
  21. data/lib/clever_sdk/api/schools.rb +68 -0
  22. data/lib/clever_sdk/api/sections.rb +70 -0
  23. data/lib/clever_sdk/api/terms.rb +46 -0
  24. data/lib/clever_sdk/api/users.rb +81 -0
  25. data/lib/clever_sdk/api.rb +81 -0
  26. data/lib/clever_sdk/authentication.rb +67 -0
  27. data/lib/clever_sdk/client/courses.rb +45 -0
  28. data/lib/clever_sdk/client/districts.rb +21 -0
  29. data/lib/clever_sdk/client/events.rb +22 -0
  30. data/lib/clever_sdk/client/resources.rb +40 -0
  31. data/lib/clever_sdk/client/schools.rb +54 -0
  32. data/lib/clever_sdk/client/sections.rb +59 -0
  33. data/lib/clever_sdk/client/terms.rb +40 -0
  34. data/lib/clever_sdk/client/users.rb +66 -0
  35. data/lib/clever_sdk/client.rb +41 -0
  36. data/lib/clever_sdk/configuration.rb +13 -0
  37. data/lib/clever_sdk/data/course/refs.rb +36 -0
  38. data/lib/clever_sdk/data/course.rb +44 -0
  39. data/lib/clever_sdk/data/courses.rb +79 -0
  40. data/lib/clever_sdk/data/district.rb +129 -0
  41. data/lib/clever_sdk/data/districts.rb +37 -0
  42. data/lib/clever_sdk/data/event.rb +93 -0
  43. data/lib/clever_sdk/data/events.rb +79 -0
  44. data/lib/clever_sdk/data/me.rb +34 -0
  45. data/lib/clever_sdk/data/resource/refs.rb +45 -0
  46. data/lib/clever_sdk/data/resource.rb +60 -0
  47. data/lib/clever_sdk/data/resources.rb +79 -0
  48. data/lib/clever_sdk/data/school/refs.rb +42 -0
  49. data/lib/clever_sdk/data/school.rb +88 -0
  50. data/lib/clever_sdk/data/schools.rb +79 -0
  51. data/lib/clever_sdk/data/section/refs.rb +45 -0
  52. data/lib/clever_sdk/data/section.rb +100 -0
  53. data/lib/clever_sdk/data/sections.rb +79 -0
  54. data/lib/clever_sdk/data/sso.rb +44 -0
  55. data/lib/clever_sdk/data/term/refs.rb +32 -0
  56. data/lib/clever_sdk/data/term.rb +46 -0
  57. data/lib/clever_sdk/data/terms.rb +79 -0
  58. data/lib/clever_sdk/data/token.rb +67 -0
  59. data/lib/clever_sdk/data/tokeninfo.rb +30 -0
  60. data/lib/clever_sdk/data/tokens.rb +35 -0
  61. data/lib/clever_sdk/data/user/contact_role.rb +19 -0
  62. data/lib/clever_sdk/data/user/district_admin_role.rb +19 -0
  63. data/lib/clever_sdk/data/user/refs.rb +56 -0
  64. data/lib/clever_sdk/data/user/staff_role.rb +19 -0
  65. data/lib/clever_sdk/data/user/student_role.rb +19 -0
  66. data/lib/clever_sdk/data/user/teacher_role.rb +19 -0
  67. data/lib/clever_sdk/data/user.rb +123 -0
  68. data/lib/clever_sdk/data/users.rb +79 -0
  69. data/lib/clever_sdk/error.rb +27 -0
  70. data/lib/clever_sdk/version.rb +5 -0
  71. data/lib/clever_sdk.rb +36 -0
  72. metadata +255 -0
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CleverSDK
4
+ module Data
5
+ class Resource
6
+ class Refs
7
+ # private
8
+ def initialize(resource)
9
+ @resource = resource
10
+ end
11
+
12
+ def district
13
+ client.course_district(@resource.id)
14
+ end
15
+
16
+ def courses(limit: nil, starting_after: nil, ending_before: nil)
17
+ client.resource_courses(@resource.id, limit: limit, starting_after: starting_after, ending_before: ending_before)
18
+ end
19
+
20
+ def sections(limit: nil, starting_after: nil, ending_before: nil)
21
+ client.resource_sections(@resource.id, limit: limit, starting_after: starting_after, ending_before: ending_before)
22
+ end
23
+
24
+ # role - acceptable values are nil, :student, :teacher
25
+ def users(role: nil, limit: nil, starting_after: nil, ending_before: nil)
26
+ client.resource_users(@resource.id, role: role, limit: limit, starting_after: starting_after, ending_before: ending_before)
27
+ end
28
+
29
+ def resource_courses(access_token:, id:, limit: nil, starting_after: nil, ending_before: nil)
30
+ get(
31
+ "https://api.clever.com/v3.0/resources/#{id}/courses",
32
+ params: params(limit: limit, starting_after: starting_after, ending_before: ending_before),
33
+ headers: bearer_headers(access_token)
34
+ )
35
+ end
36
+
37
+ private
38
+
39
+ def client
40
+ @client ||= CleverSDK::Client.new(@resource.response.access_token)
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "clever_sdk/data/resource/refs"
4
+
5
+ module CleverSDK
6
+ module Data
7
+ class Resource
8
+ # private
9
+ attr_reader :data, :response
10
+
11
+ # private
12
+ def initialize(data, response)
13
+ @data = data
14
+ @response = response
15
+ end
16
+
17
+ def id
18
+ data.dig("id")
19
+ end
20
+
21
+ def resource_id
22
+ data.dig("resource_id")
23
+ end
24
+
25
+ def district
26
+ data.dig("district")
27
+ end
28
+
29
+ def title
30
+ data.dig("title")
31
+ end
32
+
33
+ def roles
34
+ data.dig("roles")
35
+ end
36
+
37
+ def created
38
+ DateTime.parse(data.dig("created"))
39
+ rescue
40
+ nil
41
+ end
42
+
43
+ def updated
44
+ DateTime.parse(data.dig("updated"))
45
+ rescue
46
+ nil
47
+ end
48
+
49
+ def refs
50
+ @refs ||= CleverSDK::Data::Resource::Refs.new(self)
51
+ end
52
+
53
+ def inspect
54
+ "#<#{self.class.name}(#{id}) #{title}>"
55
+ end
56
+
57
+ alias_method :to_h, :data
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,79 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "clever_sdk/data/resource"
4
+
5
+ module CleverSDK
6
+ module Data
7
+ class Resources
8
+ include Enumerable
9
+
10
+ # private
11
+ attr_reader :response
12
+
13
+ # private
14
+ def initialize(response)
15
+ @response = response
16
+ end
17
+
18
+ def resources
19
+ Array(response.body.dig("resources"))
20
+ end
21
+
22
+ def each &block
23
+ return enum_for :each unless block
24
+
25
+ resources
26
+ .lazy
27
+ .map { |resource| CleverSDK::Data::Resource.new(resource, response) }
28
+ .each(&block)
29
+ end
30
+
31
+ def all &block
32
+ return enum_for :all unless block
33
+
34
+ page = self
35
+ loop do
36
+ page.each(&block)
37
+ page = page.next
38
+ break if page.nil?
39
+ end
40
+ end
41
+
42
+ def next?
43
+ link = find_link("next")
44
+ !link.nil?
45
+ end
46
+
47
+ def next
48
+ link = find_link("next")
49
+
50
+ return nil if link.nil?
51
+
52
+ self.class.new(CleverSDK::Api.new.page(path: link.dig("uri"), access_token: response.access_token))
53
+ end
54
+
55
+ def prev?
56
+ link = find_link("prev")
57
+ !link.nil?
58
+ end
59
+
60
+ def prev
61
+ link = find_link("prev")
62
+
63
+ return nil if link.nil?
64
+
65
+ self.class.new(CleverSDK::Api.new.page(path: link.dig("uri"), access_token: response.access_token))
66
+ end
67
+
68
+ def inspect
69
+ "#<#{self.class.name}:0x#{(object_id * 2).to_s(16).rjust(16, "0")} resources:#{resources.count}>"
70
+ end
71
+
72
+ private
73
+
74
+ def find_link rel
75
+ Array(response.body.dig("links")).find { |link| link.dig("rel") == rel }
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CleverSDK
4
+ module Data
5
+ class School
6
+ class Refs
7
+ # private
8
+ def initialize(school)
9
+ @school = school
10
+ end
11
+
12
+ def courses(limit: nil, starting_after: nil, ending_before: nil)
13
+ client.school_courses(@school.id, limit: limit, starting_after: starting_after, ending_before: ending_before)
14
+ end
15
+
16
+ def district
17
+ client.school_district(@school.id)
18
+ end
19
+
20
+ def sections(limit: nil, starting_after: nil, ending_before: nil)
21
+ client.school_sections(@school.id, limit: limit, starting_after: starting_after, ending_before: ending_before)
22
+ end
23
+
24
+ def terms(limit: nil, starting_after: nil, ending_before: nil)
25
+ client.school_terms(@school.id, limit: limit, starting_after: starting_after, ending_before: ending_before)
26
+ end
27
+
28
+ # role - acceptable values are nil, :staff, :student, :teacher
29
+ # primary - acceptable values are nil, true
30
+ def users(role: nil, primary: nil, limit: nil, starting_after: nil, ending_before: nil)
31
+ client.school_users(@school.id, role: role, primary: primary, limit: limit, starting_after: starting_after, ending_before: ending_before)
32
+ end
33
+
34
+ private
35
+
36
+ def client
37
+ @client ||= CleverSDK::Client.new(@school.response.access_token)
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,88 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "clever_sdk/data/school/refs"
4
+
5
+ module CleverSDK
6
+ module Data
7
+ class School
8
+ # private
9
+ attr_reader :data, :response
10
+
11
+ # private
12
+ def initialize(data, response)
13
+ @data = data
14
+ @response = response
15
+ end
16
+
17
+ def id
18
+ data.dig("id")
19
+ end
20
+
21
+ def district
22
+ data.dig("district")
23
+ end
24
+
25
+ def name
26
+ data.dig("name")
27
+ end
28
+
29
+ def sis_id
30
+ data.dig("sis_id")
31
+ end
32
+
33
+ def low_grade
34
+ data.dig("low_grade")
35
+ end
36
+
37
+ def high_grade
38
+ data.dig("high_grade")
39
+ end
40
+
41
+ def school_number
42
+ data.dig("school_number")
43
+ end
44
+
45
+ def principal
46
+ data.dig("principal")
47
+ end
48
+
49
+ def state_id
50
+ data.dig("state_id")
51
+ end
52
+
53
+ def phone
54
+ data.dig("phone")
55
+ end
56
+
57
+ def ext
58
+ data.dig("ext")
59
+ end
60
+
61
+ def location
62
+ data.dig("location")
63
+ end
64
+
65
+ def created
66
+ DateTime.parse(data.dig("created"))
67
+ rescue
68
+ nil
69
+ end
70
+
71
+ def last_modified
72
+ DateTime.parse(data.dig("last_modified"))
73
+ rescue
74
+ nil
75
+ end
76
+
77
+ def refs
78
+ @refs ||= CleverSDK::Data::School::Refs.new(self)
79
+ end
80
+
81
+ def inspect
82
+ "#<#{self.class.name}(#{id}) #{name}>"
83
+ end
84
+
85
+ alias_method :to_h, :data
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,79 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "clever_sdk/data/school"
4
+
5
+ module CleverSDK
6
+ module Data
7
+ class Schools
8
+ include Enumerable
9
+
10
+ # private
11
+ attr_reader :response
12
+
13
+ # private
14
+ def initialize(response)
15
+ @response = response
16
+ end
17
+
18
+ def data
19
+ Array(response.body.dig("data"))
20
+ end
21
+
22
+ def each &block
23
+ return enum_for :each unless block
24
+
25
+ data
26
+ .lazy
27
+ .map { |datum| CleverSDK::Data::School.new(datum.dig("data"), response) }
28
+ .each(&block)
29
+ end
30
+
31
+ def all &block
32
+ return enum_for :all unless block
33
+
34
+ page = self
35
+ loop do
36
+ page.each(&block)
37
+ page = page.next
38
+ break if page.nil?
39
+ end
40
+ end
41
+
42
+ def next?
43
+ link = find_link("next")
44
+ !link.nil?
45
+ end
46
+
47
+ def next
48
+ link = find_link("next")
49
+
50
+ return nil if link.nil?
51
+
52
+ self.class.new(CleverSDK::Api.new.page(path: link.dig("uri"), access_token: response.access_token))
53
+ end
54
+
55
+ def prev?
56
+ link = find_link("prev")
57
+ !link.nil?
58
+ end
59
+
60
+ def prev
61
+ link = find_link("prev")
62
+
63
+ return nil if link.nil?
64
+
65
+ self.class.new(CleverSDK::Api.new.page(path: link.dig("uri"), access_token: response.access_token))
66
+ end
67
+
68
+ def inspect
69
+ "#<#{self.class.name}:0x#{(object_id * 2).to_s(16).rjust(16, "0")} schools:#{data.count}>"
70
+ end
71
+
72
+ private
73
+
74
+ def find_link rel
75
+ Array(response.body.dig("links")).find { |link| link.dig("rel") == rel }
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CleverSDK
4
+ module Data
5
+ class Section
6
+ class Refs
7
+ # private
8
+ def initialize(section)
9
+ @section = section
10
+ end
11
+
12
+ def course
13
+ client.section_course(@section.id)
14
+ end
15
+
16
+ def district
17
+ client.section_district(@section.id)
18
+ end
19
+
20
+ def resources(limit: nil, starting_after: nil, ending_before: nil)
21
+ client.section_resources(@section.id, limit: limit, starting_after: starting_after, ending_before: ending_before)
22
+ end
23
+
24
+ def school
25
+ client.section_school(@section.id)
26
+ end
27
+
28
+ def term
29
+ client.section_term(@section.id)
30
+ end
31
+
32
+ # role - acceptable values are nil, :staff, :student, :teacher
33
+ def users(role: nil, limit: nil, starting_after: nil, ending_before: nil)
34
+ client.section_users(@section.id, role: role, limit: limit, starting_after: starting_after, ending_before: ending_before)
35
+ end
36
+
37
+ private
38
+
39
+ def client
40
+ @client ||= CleverSDK::Client.new(@section.response.access_token)
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,100 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "clever_sdk/data/section/refs"
4
+
5
+ module CleverSDK
6
+ module Data
7
+ class Section
8
+ # private
9
+ attr_reader :data, :response
10
+
11
+ # private
12
+ def initialize(data, response)
13
+ @data = data
14
+ @response = response
15
+ end
16
+
17
+ def id
18
+ data.dig("id")
19
+ end
20
+
21
+ def course
22
+ data.dig("course")
23
+ end
24
+
25
+ def district
26
+ data.dig("district")
27
+ end
28
+
29
+ def school
30
+ data.dig("school")
31
+ end
32
+
33
+ def term_id
34
+ data.dig("term_id")
35
+ end
36
+
37
+ def name
38
+ data.dig("name")
39
+ end
40
+
41
+ def subject
42
+ data.dig("subject")
43
+ end
44
+
45
+ def section_number
46
+ data.dig("section_number")
47
+ end
48
+
49
+ def sis_id
50
+ data.dig("sis_id")
51
+ end
52
+
53
+ def grade
54
+ data.dig("grade")
55
+ end
56
+
57
+ def period
58
+ data.dig("period")
59
+ end
60
+
61
+ def ext
62
+ data.dig("ext")
63
+ end
64
+
65
+ def students
66
+ data.dig("students")
67
+ end
68
+
69
+ def teacher
70
+ data.dig("teacher")
71
+ end
72
+
73
+ def teachers
74
+ data.dig("teachers")
75
+ end
76
+
77
+ def created
78
+ DateTime.parse(data.dig("created"))
79
+ rescue
80
+ nil
81
+ end
82
+
83
+ def last_modified
84
+ DateTime.parse(data.dig("last_modified"))
85
+ rescue
86
+ nil
87
+ end
88
+
89
+ def refs
90
+ @refs ||= CleverSDK::Data::Section::Refs.new(self)
91
+ end
92
+
93
+ def inspect
94
+ "#<#{self.class.name}(#{id}) #{name}>"
95
+ end
96
+
97
+ alias_method :to_h, :data
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,79 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "clever_sdk/data/section"
4
+
5
+ module CleverSDK
6
+ module Data
7
+ class Sections
8
+ include Enumerable
9
+
10
+ # private
11
+ attr_reader :response
12
+
13
+ # private
14
+ def initialize(response)
15
+ @response = response
16
+ end
17
+
18
+ def data
19
+ Array(response.body.dig("data"))
20
+ end
21
+
22
+ def each &block
23
+ return enum_for :each unless block
24
+
25
+ data
26
+ .lazy
27
+ .map { |datum| CleverSDK::Data::Section.new(datum.dig("data"), response) }
28
+ .each(&block)
29
+ end
30
+
31
+ def all &block
32
+ return enum_for :all unless block
33
+
34
+ page = self
35
+ loop do
36
+ page.each(&block)
37
+ page = page.next
38
+ break if page.nil?
39
+ end
40
+ end
41
+
42
+ def next?
43
+ link = find_link("next")
44
+ !link.nil?
45
+ end
46
+
47
+ def next
48
+ link = find_link("next")
49
+
50
+ return nil if link.nil?
51
+
52
+ self.class.new(CleverSDK::Api.new.page(path: link.dig("uri"), access_token: response.access_token))
53
+ end
54
+
55
+ def prev?
56
+ link = find_link("prev")
57
+ !link.nil?
58
+ end
59
+
60
+ def prev
61
+ link = find_link("prev")
62
+
63
+ return nil if link.nil?
64
+
65
+ self.class.new(CleverSDK::Api.new.page(path: link.dig("uri"), access_token: response.access_token))
66
+ end
67
+
68
+ def inspect
69
+ "#<#{self.class.name}:0x#{(object_id * 2).to_s(16).rjust(16, "0")} sections:#{data.count}>"
70
+ end
71
+
72
+ private
73
+
74
+ def find_link rel
75
+ Array(response.body.dig("links")).find { |link| link.dig("rel") == rel }
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,44 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CleverSDK
4
+ module Data
5
+ ##
6
+ # Represents the data returned from a successful call to CleverSDK::Authentication#sso.
7
+ class SSO
8
+ # public
9
+ attr_reader :code
10
+ # private
11
+ attr_reader :response
12
+
13
+ # private
14
+ def initialize(code, response)
15
+ @code = code
16
+ @response = response
17
+ end
18
+
19
+ def access_token
20
+ response.body.dig("access_token")
21
+ end
22
+
23
+ def token_type
24
+ response.body.dig("token_type")
25
+ end
26
+
27
+ def me
28
+ @me ||= CleverSDK.authentication.me(access_token)
29
+ end
30
+
31
+ def user
32
+ @user ||= CleverSDK.client(access_token).user(me.id)
33
+ end
34
+
35
+ def inspect
36
+ "#<#{self.class.name}(#{code}) #{access_token}>"
37
+ end
38
+
39
+ def to_h
40
+ response.body.merge("code" => code, "me" => @me, "user" => @user).delete_if {|_k, v| v.nil?}
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CleverSDK
4
+ module Data
5
+ class Term
6
+ class Refs
7
+ # private
8
+ def initialize(term)
9
+ @term = term
10
+ end
11
+
12
+ def district
13
+ client.term_district(@term.id)
14
+ end
15
+
16
+ def schools(limit: nil, starting_after: nil, ending_before: nil)
17
+ client.term_schools(@term.id, limit: limit, starting_after: starting_after, ending_before: ending_before)
18
+ end
19
+
20
+ def sections(limit: nil, starting_after: nil, ending_before: nil)
21
+ client.term_sections(@term.id, limit: limit, starting_after: starting_after, ending_before: ending_before)
22
+ end
23
+
24
+ private
25
+
26
+ def client
27
+ @client ||= CleverSDK::Client.new(@term.response.access_token)
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end