Class: Clever::Client

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeClient

Returns a new instance of Client.



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

def initialize
  @api_url         = API_URL
  @tokens_endpoint = TOKENS_ENDPOINT
end

Instance Attribute Details

#api_urlObject (readonly)

Returns the value of attribute api_url.



10
11
12
# File 'lib/clever/client.rb', line 10

def api_url
  @api_url
end

#app_idObject

Returns the value of attribute app_id.



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

def app_id
  @app_id
end

#app_tokenObject

Returns the value of attribute app_token.



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

def app_token
  @app_token
end

#loggerObject

Returns the value of attribute logger.



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

def logger
  @logger
end

#redirect_uriObject

Returns the value of attribute redirect_uri.



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

def redirect_uri
  @redirect_uri
end

#sentry_clientObject

Returns the value of attribute sentry_client.



5
6
7
# File 'lib/clever/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/clever/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/clever/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/clever/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/clever/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/clever/client.rb', line 5

def student_username_search_for
  @student_username_search_for
end

#sync_idObject

Returns the value of attribute sync_id.



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

def sync_id
  @sync_id
end

#tokens_endpointObject (readonly)

Returns the value of attribute tokens_endpoint.



10
11
12
# File 'lib/clever/client.rb', line 10

def tokens_endpoint
  @tokens_endpoint
end

#username_sourceObject

Returns the value of attribute username_source.



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

def username_source
  @username_source
end

#vendor_keyObject

Returns the value of attribute vendor_key.



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

def vendor_key
  @vendor_key
end

#vendor_secretObject

Returns the value of attribute vendor_secret.



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

def vendor_secret
  @vendor_secret
end

Class Method Details

.configure {|client| ... } ⇒ Object

Yields:

  • (client)


17
18
19
20
21
# File 'lib/clever/client.rb', line 17

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

Instance Method Details

#admins(record_uids = []) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/clever/client.rb', line 91

def admins(record_uids = [])
  authenticate

  district_admins = Paginator.fetch(connection, Clever::DISTRICT_ADMINS_ENDPOINT,
                                    :get, Types::DistrictAdmin, client: self).force

  school_admins = Paginator.fetch(connection, Clever::SCHOOL_ADMINS_ENDPOINT,
                                  :get, Types::SchoolAdmin, client: self).force

  admins = (district_admins + school_admins).uniq(&:uid)

  return admins if record_uids.empty?

  record_uids_set = record_uids.to_set
  admins.select { |record| record_uids_set.include?(record.uid) }
end

#authenticate(app_id = @app_id) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/clever/client.rb', line 23

def authenticate(app_id = @app_id)
  return if @app_token

  response = tokens

  fail ConnectionError, response.raw_body unless response.success?

  set_token(response, app_id)
end

#classroomsObject

discard params to make the API behave the same as the one roster gem



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/clever/client.rb', line 116

def classrooms(*)
  authenticate

  fetched_courses = courses
  fetched_schools = schools

  terms_hash = terms.each_with_object({}) { |term, terms| terms[term.uid] = term  }

  sections.map do |section|
    course = fetched_courses.find { |clever_course| clever_course.uid == section.course }
    term = terms_hash[section.term_id]
    school = fetched_schools.find { |school| school.uid == section.school_uid }

    Types::Classroom.new(
      'id' => section.uid,
      'name' => section.name,
      'period' => section.period,
      'course_number' => course&.number,
      'grades' => section.grades,
      'subjects' => section.subjects,
      'term_name' => term&.name,
      'term_start_date' => term&.start_date,
      'term_end_date' => term&.end_date,
      'term_id' => section.term_id,
      'school_name' => school&.name,
      'school_uid' => school&.uid
    )
  end
end

#connectionObject



33
34
35
# File 'lib/clever/client.rb', line 33

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

#enrollments(classroom_uids = []) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
# File 'lib/clever/client.rb', line 146

def enrollments(classroom_uids = [])
  authenticate

  fetched_sections = sections

  enrollments = parse_enrollments(classroom_uids, fetched_sections)

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

  enrollments
end

#events(starting_after) ⇒ Object



68
69
70
71
72
73
# File 'lib/clever/client.rb', line 68

def events(starting_after)
  authenticate

  endpoint = "#{Clever::EVENTS_ENDPOINT}?starting_after=#{starting_after}"
  Paginator.fetch(connection, endpoint, :get, Types::Event, client: self).force
end

#most_recent_eventObject



59
60
61
62
63
64
65
66
# File 'lib/clever/client.rb', line 59

def most_recent_event
  authenticate

  endpoint = "#{Clever::EVENTS_ENDPOINT}?ending_before=last&limit=1"

  event = @connection.execute(endpoint).body[0]
  Types::Event.new(event['data']) if event
end

#schoolsObject



108
109
110
111
112
113
# File 'lib/clever/client.rb', line 108

def schools
  authenticate

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

#send_grade(request_body) ⇒ Object



158
159
160
161
162
# File 'lib/clever/client.rb', line 158

def send_grade(request_body)
  authenticate

  @connection.execute(GRADES_ENDPOINT, :post, nil, request_body)
end

#tokensObject



37
38
39
40
41
# File 'lib/clever/client.rb', line 37

def tokens
  response = connection.execute(@tokens_endpoint)
  map_response!(response, Types::Token)
  response
end

#user_uid_for_code(code) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/clever/client.rb', line 43

def user_uid_for_code(code)
  response = connection.execute(USER_TOKEN_ENDPOINT,
                                :post,
                                nil,
                                { code: code,
                                  grant_type: 'authorization_code',
                                  redirect_uri: redirect_uri })

  fail ConnectionError, response.raw_body unless response.success?

  connection.set_token(response.raw_body['access_token'])

  response = connection.execute(ME_ENDPOINT, :get)
  response&.body&.dig('id')
end