Class: PlatformSdk::OneRoster::Client

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

Overview

OneRoster Client which wraps the swagger generated OneRoster Management APIs

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(oneroster_base_url, auth_client) ⇒ Client

Returns a new instance of Client.



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/platform_sdk/one_roster/client.rb', line 10

def initialize(oneroster_base_url, auth_client)
  @auth = auth_client

  uri = URI.parse(oneroster_base_url)
  ::OneRosterClient.configure do |config|
    # Configure OAuth2 access token for authorization: OAuth2Security
    config.scheme = uri.scheme
    config.host = uri.host
    config.base_path = uri.path
  end

  init_apis
end

Instance Attribute Details

#academic_sessions_apiObject

Returns the value of attribute academic_sessions_api.



7
8
9
# File 'lib/platform_sdk/one_roster/client.rb', line 7

def academic_sessions_api
  @academic_sessions_api
end

#classes_apiObject

Returns the value of attribute classes_api.



7
8
9
# File 'lib/platform_sdk/one_roster/client.rb', line 7

def classes_api
  @classes_api
end

#courses_apiObject

Returns the value of attribute courses_api.



7
8
9
# File 'lib/platform_sdk/one_roster/client.rb', line 7

def courses_api
  @courses_api
end

#demographics_apiObject

Returns the value of attribute demographics_api.



7
8
9
# File 'lib/platform_sdk/one_roster/client.rb', line 7

def demographics_api
  @demographics_api
end

#enrollments_apiObject

Returns the value of attribute enrollments_api.



7
8
9
# File 'lib/platform_sdk/one_roster/client.rb', line 7

def enrollments_api
  @enrollments_api
end

#line_items_apiObject

Returns the value of attribute line_items_api.



7
8
9
# File 'lib/platform_sdk/one_roster/client.rb', line 7

def line_items_api
  @line_items_api
end

#orgs_apiObject

Returns the value of attribute orgs_api.



7
8
9
# File 'lib/platform_sdk/one_roster/client.rb', line 7

def orgs_api
  @orgs_api
end

#results_apiObject

Returns the value of attribute results_api.



7
8
9
# File 'lib/platform_sdk/one_roster/client.rb', line 7

def results_api
  @results_api
end

#users_apiObject

Returns the value of attribute users_api.



7
8
9
# File 'lib/platform_sdk/one_roster/client.rb', line 7

def users_api
  @users_api
end

Instance Method Details

#error(api_error) ⇒ Object



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

def error(api_error)
  case api_error.response[:status]
  when 404
    nil
  when 401
    jwt_expiry_time = @auth.token.present? ? @auth.jwt_expiry_time(@auth.token[:access_token]) : "No token present"
    puts "Unauthorized exception when calling OneRoster resource.  Current JWT expiry time = #{jwt_expiry_time}"
    raise
  else
    puts "Exception when calling OneRoster resource: #{api_error}"
    raise
  end
end

#function_name(key) ⇒ Object



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

def function_name(key)
  {
    student: "user",
    agents: "user",
    orgs: "org",
    school: "org",
    terms: "academic_session",
    school_year: "academic_session",
    grading_period: "academic_session",
    parent: "academic_session"
  }[key] || key
end

#init_apisObject



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/platform_sdk/one_roster/client.rb', line 24

def init_apis
  @users_api = ::OneRosterClient::UsersManagementApi.new
  @classes_api = ::OneRosterClient::ClassesManagementApi.new
  @courses_api = ::OneRosterClient::CoursesManagementApi.new
  @enrollments_api = ::OneRosterClient::EnrollmentsManagementApi.new
  @academic_sessions_api = ::OneRosterClient::AcademicSessionsManagementApi.new
  @line_items_api = ::OneRosterClient::LineItemsManagementApi.new
  @results_api = ::OneRosterClient::ResultsManagementApi.new
  @orgs_api = ::OneRosterClient::OrgsManagementApi.new
  @demographics_api = ::OneRosterClient::DemographicsManagementApi.new
end

#key_map(key) ⇒ Object



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

def key_map(key)
  {
    class: "_class",
    demographic: "demographics"
  }[key] || key
end

#management_api(record_type) ⇒ Object



113
114
115
116
# File 'lib/platform_sdk/one_roster/client.rb', line 113

def management_api(record_type)
  pluralized_record_type = pluralize_oneroster_type(record_type)
  instance_variable_get "@#{pluralized_record_type}_api"
end

#pluralize_oneroster_type(record_type) ⇒ Object



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

def pluralize_oneroster_type(record_type)
  if record_type == :class
    "#{record_type}es"
  else
    "#{record_type}s"
  end
end

#records_typeArray<(UsersType, Integer, Hash)>

Returns UsersType data, response status code and response headers.

Parameters:

  • opts (Hash)

    the optional parameters

Returns:

  • (Array<(UsersType, Integer, Hash)>)

    UsersType data, response status code and response headers



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

%i[users classes courses enrollments demographics orgs line_items results academic_sessions].each do |records_type|
  define_method(records_type) do |opts = {}|
    api = instance_variable_get "@#{records_type}_api"
    with_rescue_and_set_token do
      one_roster_records(api, records_type, opts)
    end
  end
end

#with_rescue_and_set_tokenObject



72
73
74
75
76
77
# File 'lib/platform_sdk/one_roster/client.rb', line 72

def with_rescue_and_set_token
  ::OneRosterClient.configure.access_token = @auth.auth_token
  yield
rescue ::OneRosterClient::Error => e
  error(e)
end