Class: OneRoster::Client
- Inherits:
-
Object
- Object
- OneRoster::Client
- Defined in:
- lib/one_roster/client.rb
Instance Attribute Summary collapse
-
#api_url ⇒ Object
Returns the value of attribute api_url.
-
#app_id ⇒ Object
Returns the value of attribute app_id.
-
#app_secret ⇒ Object
Returns the value of attribute app_secret.
-
#app_token ⇒ Object
Returns the value of attribute app_token.
-
#authenticated ⇒ Object
readonly
Returns the value of attribute authenticated.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#oauth_strategy ⇒ Object
Returns the value of attribute oauth_strategy.
-
#roster_app ⇒ Object
Returns the value of attribute roster_app.
-
#staff_username_source ⇒ Object
Returns the value of attribute staff_username_source.
-
#token_content_type ⇒ Object
Returns the value of attribute token_content_type.
-
#token_url ⇒ Object
Returns the value of attribute token_url.
-
#username_source ⇒ Object
Returns the value of attribute username_source.
-
#vendor_key ⇒ Object
Returns the value of attribute vendor_key.
Class Method Summary collapse
Instance Method Summary collapse
- #authenticate ⇒ Object
- #authenticated? ⇒ Boolean
- #classrooms(course_codes = []) ⇒ Object
- #connection ⇒ Object
- #courses(course_codes = [], oneroster_classes = classes) ⇒ Object
- #enrollments(classroom_uids = []) ⇒ Object
-
#initialize(oauth_strategy = 'oauth') ⇒ Client
constructor
A new instance of Client.
- #set_auth_headers(token, cookie) ⇒ Object
- #terms ⇒ Object
- #token ⇒ Object
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_url ⇒ Object
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_id ⇒ Object
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_secret ⇒ Object
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_token ⇒ Object
Returns the value of attribute app_token.
5 6 7 |
# File 'lib/one_roster/client.rb', line 5 def app_token @app_token end |
#authenticated ⇒ Object (readonly)
Returns the value of attribute authenticated.
9 10 11 |
# File 'lib/one_roster/client.rb', line 9 def authenticated @authenticated end |
#logger ⇒ Object
Returns the value of attribute logger.
5 6 7 |
# File 'lib/one_roster/client.rb', line 5 def logger @logger end |
#oauth_strategy ⇒ Object
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_app ⇒ Object
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_source ⇒ Object
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_type ⇒ Object
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_url ⇒ Object
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_source ⇒ Object
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_key ⇒ Object
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
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
#authenticate ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/one_roster/client.rb', line 104 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
122 123 124 |
# File 'lib/one_roster/client.rb', line 122 def authenticated? @authenticated end |
#classrooms(course_codes = []) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/one_roster/client.rb', line 39 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 |
#connection ⇒ Object
126 127 128 |
# File 'lib/one_roster/client.rb', line 126 def connection @connection ||= Connection.new(self, oauth_strategy) end |
#courses(course_codes = [], oneroster_classes = classes) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/one_roster/client.rb', line 78 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
94 95 96 97 98 99 100 101 102 |
# File 'lib/one_roster/client.rb', line 94 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
145 146 147 |
# File 'lib/one_roster/client.rb', line 145 def set_auth_headers(token, ) connection.set_auth_headers(token['access_token'], ) end |
#terms ⇒ Object
68 69 70 71 72 73 74 75 76 |
# File 'lib/one_roster/client.rb', line 68 def terms authenticate endpoint = OneRoster::ACADEMIC_SESSIONS_ENDPOINT type = Types::Term Paginator.fetch(connection, endpoint, :get, type, client: self).force end |
#token ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/one_roster/client.rb', line 130 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 |