Class: OneRoster::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/one_roster/client.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(oauth_strategy = 'oauth') ⇒ Client

Returns a new instance of Client.



12
13
14
15
# File 'lib/one_roster/client.rb', line 12

def initialize(oauth_strategy = 'oauth')
  @authenticated = false
  @oauth_strategy = oauth_strategy
end

Instance Attribute Details

#api_urlObject

Returns the value of attribute api_url.



5
6
7
# File 'lib/one_roster/client.rb', line 5

def api_url
  @api_url
end

#app_idObject

Returns the value of attribute app_id.



5
6
7
# File 'lib/one_roster/client.rb', line 5

def app_id
  @app_id
end

#app_secretObject

Returns the value of attribute app_secret.



5
6
7
# File 'lib/one_roster/client.rb', line 5

def app_secret
  @app_secret
end

#app_tokenObject

Returns the value of attribute app_token.



5
6
7
# File 'lib/one_roster/client.rb', line 5

def app_token
  @app_token
end

#authenticatedObject (readonly)

Returns the value of attribute authenticated.



10
11
12
# File 'lib/one_roster/client.rb', line 10

def authenticated
  @authenticated
end

#loggerObject

Returns the value of attribute logger.



5
6
7
# File 'lib/one_roster/client.rb', line 5

def logger
  @logger
end

#oauth_strategyObject

Returns the value of attribute oauth_strategy.



5
6
7
# File 'lib/one_roster/client.rb', line 5

def oauth_strategy
  @oauth_strategy
end

#only_provision_current_termsObject

Returns the value of attribute only_provision_current_terms.



5
6
7
# File 'lib/one_roster/client.rb', line 5

def only_provision_current_terms
  @only_provision_current_terms
end

#roster_appObject

Returns the value of attribute roster_app.



5
6
7
# File 'lib/one_roster/client.rb', line 5

def roster_app
  @roster_app
end

#sentry_clientObject

Returns the value of attribute sentry_client.



5
6
7
# File 'lib/one_roster/client.rb', line 5

def sentry_client
  @sentry_client
end

#staff_username_sourceObject

Returns the value of attribute staff_username_source.



5
6
7
# File 'lib/one_roster/client.rb', line 5

def staff_username_source
  @staff_username_source
end

#token_content_typeObject

Returns the value of attribute token_content_type.



5
6
7
# File 'lib/one_roster/client.rb', line 5

def token_content_type
  @token_content_type
end

#token_urlObject

Returns the value of attribute token_url.



5
6
7
# File 'lib/one_roster/client.rb', line 5

def token_url
  @token_url
end

#username_sourceObject

Returns the value of attribute username_source.



5
6
7
# File 'lib/one_roster/client.rb', line 5

def username_source
  @username_source
end

#vendor_keyObject

Returns the value of attribute vendor_key.



5
6
7
# File 'lib/one_roster/client.rb', line 5

def vendor_key
  @vendor_key
end

Class Method Details

.configure {|client| ... } ⇒ Object

Yields:

  • (client)


17
18
19
20
21
# File 'lib/one_roster/client.rb', line 17

def self.configure
  client = new
  yield(client) if block_given?
  client
end

Instance Method Details

#admins(record_uids = []) ⇒ Object



40
41
42
43
44
45
46
47
48
49
# File 'lib/one_roster/client.rb', line 40

def admins(record_uids = [])
  authenticate

  records = Paginator.fetch(connection, ADMINS_ENDPOINT, :get, Types::Admin, client: self).force

  return records if record_uids.empty?

  record_uids_set = record_uids.to_set
  records.select { |record| record_uids_set.include?(record.uid) }
end

#authenticateObject



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/one_roster/client.rb', line 128

def authenticate
  return if authenticated?

  if oauth_strategy == 'oauth2'
    response = token

    fail ConnectionError, response.raw_body unless response.success?

    set_auth_headers(response.raw_body, response.headers['set-cookie'])
  else
    response = connection.execute(TEACHERS_ENDPOINT, :get, limit: 1)

    fail ConnectionError, response.raw_body unless response.success?
  end

  @authenticated = true
end

#authenticated?Boolean

Returns:

  • (Boolean)


146
147
148
# File 'lib/one_roster/client.rb', line 146

def authenticated?
  @authenticated
end

#classrooms(course_codes = []) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/one_roster/client.rb', line 58

def classrooms(course_codes = [])
  authenticate

  oneroster_classes = classes
  fetched_schools = schools

  terms_hash = terms.each_with_object({}) { |term, terms| terms[term.uid] = term }

  courses = courses(course_codes, oneroster_classes)

  oneroster_classes.each_with_object([]) do |oneroster_class, oneroster_classes|
    course = courses.find { |course| course.uid == oneroster_class.course_uid }
    next unless course

    term = terms_hash[oneroster_class.term_id&.first]
    school = fetched_schools.find { |school| school.uid == oneroster_class.school_uid }

    oneroster_classes << Types::Classroom.new(
      'id' => oneroster_class.uid,
      'name' => oneroster_class.title,
      'course_number' => course.course_code,
      'period' => oneroster_class.period,
      'grades' => oneroster_class.grades,
      'subjects' => oneroster_class.subjects,
      'term_name' => term&.name,
      'term_start_date' => term&.start_date,
      'term_end_date' => term&.end_date,
      'term_id' => oneroster_class.term_id,
      'school_name' => school&.name,
      'school_uid' => school&.uid
    )
  end
end

#connectionObject



150
151
152
# File 'lib/one_roster/client.rb', line 150

def connection
  @connection ||= Connection.new(self, oauth_strategy)
end

#courses(course_codes = [], oneroster_classes = classes) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/one_roster/client.rb', line 102

def courses(course_codes = [], oneroster_classes = classes)
  authenticate

  class_course_numbers = oneroster_classes.map(&:course_uid)

  courses = Paginator.fetch(
    connection,
    COURSES_ENDPOINT,
    :get,
    Types::Course,
    client: self
  ).force

  parse_courses(courses, course_codes, class_course_numbers)
end

#enrollments(classroom_uids = []) ⇒ Object



118
119
120
121
122
123
124
125
126
# File 'lib/one_roster/client.rb', line 118

def enrollments(classroom_uids = [])
  authenticate

  enrollments = parse_enrollments(classroom_uids)

  p "Found #{enrollments.values.flatten.length} enrollments."

  enrollments
end

#schoolsObject



51
52
53
54
55
56
# File 'lib/one_roster/client.rb', line 51

def schools
  authenticate

  Paginator.fetch(connection, SCHOOLS_ENDPOINT,
                  :get, Types::School, client: self).force
end

#set_auth_headers(token, cookie) ⇒ Object



169
170
171
# File 'lib/one_roster/client.rb', line 169

def set_auth_headers(token, cookie)
  connection.set_auth_headers(token['access_token'], cookie)
end

#termsObject



92
93
94
95
96
97
98
99
100
# File 'lib/one_roster/client.rb', line 92

def terms
  authenticate

  endpoint = OneRoster::ACADEMIC_SESSIONS_ENDPOINT

  type = Types::Term

  Paginator.fetch(connection, endpoint, :get, type, client: self).force
end

#tokenObject



154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/one_roster/client.rb', line 154

def token
  url = token_url || "#{api_url}/token"

  credential_params = { grant_type: 'client_credentials',
                        scope: 'https://purl.imsglobal.org/spec/or/v1p1/scope/roster-core.readonly' }

  if roster_app == 'infinite_campus'
    connection.execute(url, :post, credential_params, nil, token_content_type)
  elsif roster_app == 'synergy'
    connection.execute(url, :post, nil, credential_params, token_content_type)
  else
    connection.execute(url, :post)
  end
end