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.



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

def initialize
  @api_url         = API_URL
  @tokens_endpoint = TOKENS_ENDPOINT
end

Instance Attribute Details

#api_urlObject (readonly)

Returns the value of attribute api_url.



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

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

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



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

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)


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

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

Instance Method Details

#admins(record_uids = []) ⇒ Object



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

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



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

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



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/clever/client.rb', line 107

def classrooms(*)
  authenticate

  fetched_courses = courses

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

#connectionObject



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

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

#enrollments(classroom_uids = []) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
# File 'lib/clever/client.rb', line 132

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



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

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



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

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

#send_grade(request_body) ⇒ Object



144
145
146
147
148
# File 'lib/clever/client.rb', line 144

def send_grade(request_body)
  authenticate

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

#tokensObject



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

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

#user_uid_for_code(code) ⇒ Object



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

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