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.



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

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.



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

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

#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)


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

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

Instance Method Details

#admins(record_uids = []) ⇒ Object



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

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



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

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)


148
149
150
# File 'lib/one_roster/client.rb', line 148

def authenticated?
  @authenticated
end

#classrooms(course_codes = []) ⇒ Object



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
91
92
# File 'lib/one_roster/client.rb', line 60

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



152
153
154
# File 'lib/one_roster/client.rb', line 152

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

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



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

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



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

def enrollments(classroom_uids = [])
  authenticate

  enrollments = parse_enrollments(classroom_uids)

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

  enrollments
end

#schoolsObject



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

def schools
  authenticate

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

#set_auth_headers(token, cookie) ⇒ Object



171
172
173
# File 'lib/one_roster/client.rb', line 171

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

#termsObject



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

def terms
  authenticate

  endpoint = OneRoster::ACADEMIC_SESSIONS_ENDPOINT

  type = Types::Term

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

#tokenObject



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

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