Module: Engrade

Defined in:
lib/engrade.rb,
lib/engrade/browser.rb,
lib/engrade/version.rb,
lib/engrade/exceptions.rb,
lib/engrade/return_types.rb

Defined Under Namespace

Classes: Assignment, Browser, Classroom, InvalidClassId, InvalidKey, InvalidLogin, InvalidSession, InvalidTask, MissingTask

Constant Summary collapse

VERSION =
"1.2.3"

Class Method Summary collapse

Class Method Details

.all_assignments(classes) ⇒ Object



126
127
128
129
130
131
132
133
# File 'lib/engrade.rb', line 126

def self.all_assignments(classes)
  assignments = []
  clid_array(classes).each do |clid|
    (Engrade.gradebook :clid => clid).
      map { |assn| assignments << Assignment.new(assn) }
  end
  assignments
end

.array(input) ⇒ Object



149
150
151
# File 'lib/engrade.rb', line 149

def self.array(input)
  input = input.instance_of?(Array) ? input : [input]
end

.assignments(classes, options = {}) ⇒ Object

.assignments returns an array(of Assignment objects) of every assignment from the input classes. the classes parameter can be the class id, a Classroom object, an array of class ids, or an array of Classroom objects. assignments can be filtered by calling with the additional :only or :except option



89
90
91
# File 'lib/engrade.rb', line 89

def self.assignments(classes, options={})
  filter_assignments all_assignments(classes), options
end

.base_uriObject



17
# File 'lib/engrade.rb', line 17

def self.base_uri; 'https://api.engrade.com/api/' end

.browserObject

browser is used to extend the engrade api to allow for the removal of assignment comments



29
# File 'lib/engrade.rb', line 29

def self.browser; @browser end

.classes(options = {}) ⇒ Object

.classes returns an array of active Classroom objects(data structure to represent classes). when no argument is given, all classes are returned. the classes can be filtered by calling with the :only or :except and :type (3=active, 2=archived, 1=trashed) option.



79
80
81
# File 'lib/engrade.rb', line 79

def self.classes(options={})
  filter_classes teacher_classes, options
end

.clid_array(classes) ⇒ Object



154
155
156
157
158
# File 'lib/engrade.rb', line 154

def self.clid_array(classes)
  classes =  array(classes)
  classes.map! { |cl| cl.clid } if classes.first.instance_of? Classroom 
  classes
end

.default_paramsObject

default_params are included in all post requests



24
# File 'lib/engrade.rb', line 24

def self.default_params; @default_params end

.delete!(assignments) ⇒ Object

.delete! takes in an array of Assignment object, or a single Assignment object and deletes those assignments from the Engrade webpage. it also removes the comments from those assignments before deleting, because of a bug in the Engrade system where comments from deleted assignments reappear when assignment ids are recycled.



100
101
102
103
104
105
106
# File 'lib/engrade.rb', line 100

def self.delete!(assignments)
  assignments = array(assignments)
  assignments.each do |assn|
    @browser.remove_comments assn.clid, assn.assnid
    post :apitask => 'assignment-edit', :clid => assn.clid, :assnid => assn.assnid, :delete => '1'
  end
end

.filter_assignments(array = [], options = {}) ⇒ Object



120
121
122
123
124
# File 'lib/engrade.rb', line 120

def self.filter_assignments(array=[], options={})
  array.select! { |assn| assn.title.to_s.match options[:only]} if options[:only]
  array.reject! { |assn| assn.title.to_s.match options[:except]} if options[:except]
  array
end

.filter_classes(array = [], options = {}) ⇒ Object

HELPER METHODS #



112
113
114
115
116
117
118
# File 'lib/engrade.rb', line 112

def self.filter_classes(array=[], options={})
  array.select! { |cl| cl.name.to_s.match options[:only]} if options[:only]
  array.reject! { |cl| cl.name.to_s.match options[:except]} if options[:except]
  array.select! { |cl| cl.folder == options[:type] } if options[:type]
  array.select! { |cl| cl.syr == options[:syr] } if options[:syr]
  array
end

.gradebook(query = {}) ⇒ Object

Raises:



141
142
143
144
145
146
# File 'lib/engrade.rb', line 141

def self.gradebook(query={})
  response = post({ :apitask => 'gradebook' }.merge query)
  raise InvalidClassId, 'Invalid class' if response.
    match("You are not authorized to view this class")
  JSON(response)['assignments']
end

.login(u, p) ⇒ Object

.login posts a valid username and password to the api, and stores the responding session token in default_params

Raises:



66
67
68
69
70
71
72
# File 'lib/engrade.rb', line 66

def self.(u, p)
  response = post :apitask => 'login', :usr => u, :pwd => p
  raise InvalidLogin, 'Invalid username/password' if response.
    match(/Invalid (username|password)/)
  @browser. u, p
  @default_params.store :ses, JSON(response)['values']['ses']
end

.post(query = {}) ⇒ Object

.post merges the default_params with query, a hash of input fields, and posts them to the api Example: Engrade.post(=> ‘login’, :usr => ‘myusername’, :pwd => ‘secret’)

Raises:



49
50
51
52
53
54
55
56
# File 'lib/engrade.rb', line 49

def self.post(query={})
  response = RestClient.post base_uri, query.merge(@default_params)
  raise InvalidKey, 'Invalid apikey' if response.match("Invalid apikey")
  raise MissingTask, 'No apitask specified' if response.match("No apitask specified")
  raise InvalidTask, 'Invalid apitask' if response.match("Invalid apitask")
  raise InvalidSession, 'Not logged in' if response.match("Not logged in")
  response
end

.reset!Object



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

def self.reset!
  @default_params = { :api => 'json' }
end

.set_apikey(key) ⇒ Object



59
60
61
# File 'lib/engrade.rb', line 59

def self.set_apikey(key)
  @default_params.store :apikey, key
end

.set_ses(ses) ⇒ Object



37
38
39
# File 'lib/engrade.rb', line 37

def self.set_ses(ses)
  @default_params.store :ses, ses
end

.teacher_classes(query = {}) ⇒ Object



135
136
137
138
# File 'lib/engrade.rb', line 135

def self.teacher_classes(query={})
  class_hash = JSON(post :apitask => 'teacher-classes')['classes']
  class_hash.map { |cl| Classroom.new(cl) }
end