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.



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

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.



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

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

#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

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


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

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

Instance Method Details

#admins(record_uids = []) ⇒ Object



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

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



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

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)


133
134
135
# File 'lib/one_roster/client.rb', line 133

def authenticated?
  @authenticated
end

#classrooms(course_codes = []) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/one_roster/client.rb', line 50

def classrooms(course_codes = [])
  authenticate

  oneroster_classes = classes

  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]

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

#connectionObject



137
138
139
# File 'lib/one_roster/client.rb', line 137

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

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



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/one_roster/client.rb', line 89

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



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

def enrollments(classroom_uids = [])
  authenticate

  enrollments = parse_enrollments(classroom_uids)

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

  enrollments
end

#set_auth_headers(token, cookie) ⇒ Object



156
157
158
# File 'lib/one_roster/client.rb', line 156

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

#termsObject



79
80
81
82
83
84
85
86
87
# File 'lib/one_roster/client.rb', line 79

def terms
  authenticate

  endpoint = OneRoster::ACADEMIC_SESSIONS_ENDPOINT

  type = Types::Term

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

#tokenObject



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

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