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.



15
16
17
18
# File 'lib/one_roster/client.rb', line 15

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.



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

def authenticated
  @authenticated
end

#ca_cert_pathObject

Returns the value of attribute ca_cert_path.



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

def ca_cert_path
  @ca_cert_path
end

#endpoint_prefixObject

Returns the value of attribute endpoint_prefix.



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

def endpoint_prefix
  @endpoint_prefix
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

#scopeObject

Returns the value of attribute scope.



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

def scope
  @scope
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

#service_accountObject

Returns the value of attribute service_account.



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

def 
  @service_account
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

#staffer_username_replace_withObject

Returns the value of attribute staffer_username_replace_with.



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

def staffer_username_replace_with
  @staffer_username_replace_with
end

#staffer_username_search_forObject

Returns the value of attribute staffer_username_search_for.



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

def staffer_username_search_for
  @staffer_username_search_for
end

#student_username_replace_withObject

Returns the value of attribute student_username_replace_with.



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

def student_username_replace_with
  @student_username_replace_with
end

#student_username_search_forObject

Returns the value of attribute student_username_search_for.



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

def student_username_search_for
  @student_username_search_for
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)


20
21
22
23
24
# File 'lib/one_roster/client.rb', line 20

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

Instance Method Details

#admins(record_uids = []) ⇒ Object



53
54
55
56
57
58
59
60
61
62
# File 'lib/one_roster/client.rb', line 53

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



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/one_roster/client.rb', line 141

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)


159
160
161
# File 'lib/one_roster/client.rb', line 159

def authenticated?
  @authenticated
end

#classrooms(course_codes = []) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/one_roster/client.rb', line 71

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



163
164
165
# File 'lib/one_roster/client.rb', line 163

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

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



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/one_roster/client.rb', line 115

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



131
132
133
134
135
136
137
138
139
# File 'lib/one_roster/client.rb', line 131

def enrollments(classroom_uids = [])
  authenticate

  enrollments = parse_enrollments(classroom_uids)

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

  enrollments
end

#schoolsObject



64
65
66
67
68
69
# File 'lib/one_roster/client.rb', line 64

def schools
  authenticate

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

#set_auth_headers(token, cookie) ⇒ Object



182
183
184
# File 'lib/one_roster/client.rb', line 182

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

#termsObject



105
106
107
108
109
110
111
112
113
# File 'lib/one_roster/client.rb', line 105

def terms
  authenticate

  endpoint = academic_sessions_endpoint

  type = Types::Term

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

#tokenObject



167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/one_roster/client.rb', line 167

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

  credential_params = { grant_type: 'client_credentials',
                        scope: 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