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.



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

def initialize
  @api_url         = API_URL
  @tokens_endpoint = TOKENS_ENDPOINT
end

Instance Attribute Details

#api_urlObject (readonly)

Returns the value of attribute api_url.



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

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

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



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

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)


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

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

Instance Method Details

#admins(record_uids = []) ⇒ Object



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

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



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

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



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

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



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

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

#enrollments(classroom_uids = []) ⇒ Object



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

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



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

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



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

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



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

def schools
  authenticate

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

#send_grade(request_body) ⇒ Object



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

def send_grade(request_body)
  authenticate

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

#tokensObject



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

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

#user_uid_for_code(code) ⇒ Object



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

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