Class: Edmodo::API::Client

Inherits:
Object
  • Object
show all
Includes:
Request, HTTParty
Defined in:
lib/edmodo-api/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key, options = {}) ⇒ Client

Initializes a new instance of the Edmodo API client Options:

> mode: Sets the mode to production or sandbox



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/edmodo-api/client.rb', line 23

def initialize(api_key, options = {})

  options = defaults.merge(options)

  @format = options[:format]
  @mode = options[:mode]
  @api_key = (api_key || ENV['EDMODO_API_KEY'] || "").strip

  raise_init_errors

  # Adding the api key as a default parameter to all requests
  self.class.default_params :api_key => @api_key

  @endpoint = Edmodo::API::Config.endpoints[@mode]

end

Instance Attribute Details

#api_keyObject (readonly)

Returns the value of attribute api_key.



15
16
17
# File 'lib/edmodo-api/client.rb', line 15

def api_key
  @api_key
end

#modeObject (readonly)

Returns the value of attribute mode.



16
17
18
# File 'lib/edmodo-api/client.rb', line 16

def mode
  @mode
end

Instance Method Details

#add_to_library(user_token, publisher_owned, resource) ⇒ Object

Adds a resource (url or embed code) to a user’s Library Params:

> user_token: The user_token of the user that will have the resource added to her/his library.

> publisher_owned: If you want the resources’s author to be the publisher account associated with the app, set this parameter. Set to ‘1’ if you want the resource to be publisher owned

> resource: Object specifying the link or embed code to add to the user’s library.



297
298
299
300
301
# File 'lib/edmodo-api/client.rb', line 297

def add_to_library user_token, publisher_owned, resource
  publisher_owned_int = publisher_owned ? 1 : 0

  request(:post, resource_uri("addToLibrary"), {:user_token => user_token, :publisher_owned => publisher_owned_int, :resource => resource })
end

#assignments_coming_due(user_token) ⇒ Object

This call can be used in conjunction with turnInAssignment to allow a user to submit content from the app for a particular assignment in Edmodo. Params:

> user_token: User token of the user



122
123
124
# File 'lib/edmodo-api/client.rb', line 122

def assignments_coming_due user_token
  request :get, resource_uri("assignmentsComingDue", @format), {:user_token => user_token}
end

#award_badge(user_token, badge_id) ⇒ Object

Awards a badge to a given user. Params:

> badge_id: Badge ID of the badge being awarded

> user_token: User token of the user receiving the badge



240
241
242
# File 'lib/edmodo-api/client.rb', line 240

def award_badge user_token, badge_id
  request(:post, resource_uri("awardBadge"), {:user_token => user_token, :badge_id => badge_id})
end

#badges_awarded(user_token) ⇒ Object

Returns an array of badges awarded by the app to the given user token. Params:

> user_token: The user_token to get badges awarded for.



146
147
148
# File 'lib/edmodo-api/client.rb', line 146

def badges_awarded user_token
  request :get, resource_uri("badgesAwarded", @format), {:user_token => user_token}
end

#children(user_token) ⇒ Object

Returns an array of data for students, given a specified parent user token. Params:

> user_token: User token of the parent



170
171
172
# File 'lib/edmodo-api/client.rb', line 170

def children user_token
  request :get, resource_uri("children", @format), {:user_token => user_token}
end

#classmates(user_token) ⇒ Object

Returns an array of user data for all students that belong to at least one group with the student specified by user token. Params:

> user_token: User token of the student



90
91
92
# File 'lib/edmodo-api/client.rb', line 90

def classmates user_token
  request :get, resource_uri("classmates", @format), {:user_token => user_token}
end

#events_by_app(user_token) ⇒ Object

Returns an array of events set on behalf of the specified user by the app. Params:

> user_token: The user token for the user from which events were set for.



154
155
156
# File 'lib/edmodo-api/client.rb', line 154

def events_by_app user_token
  request :get, resource_uri("eventsByApp", @format), {:user_token => user_token}
end

#grades_set_by_app_for_group(group_id) ⇒ Object

Returns an array of grades set by the app for the given group. Params:

> group_id: The group to get grades for.



138
139
140
# File 'lib/edmodo-api/client.rb', line 138

def grades_set_by_app_for_group group_id
  request :get, resource_uri("gradesSetByAppForGroup", @format), {:group_id => group_id}
end

#grades_set_by_app_for_user(user_token) ⇒ Object

Returns an array of grades set by the app for the given user token. Params:

> user_token: User token of the user to get grades for



130
131
132
# File 'lib/edmodo-api/client.rb', line 130

def grades_set_by_app_for_user user_token
  request :get, resource_uri("gradesSetByAppForUser", @format), {:user_token => user_token}
end

#groups(group_ids) ⇒ Object

Returns group data for a given array of group ids. Params:

> group_ids: array of group ids



64
65
66
67
68
# File 'lib/edmodo-api/client.rb', line 64

def groups group_ids
  group_ids = Array(group_ids)

  request :get, resource_uri("groups", @format), {:group_ids => group_ids.to_json}
end

#groups_for_user(user_token) ⇒ Object

Returns the data for groups a user belongs to given a user token. Params:

> user_token: User token



74
75
76
# File 'lib/edmodo-api/client.rb', line 74

def groups_for_user user_token
  request :get, resource_uri("groupsForUser", @format), {:user_token => user_token}
end

#launch_requests(launch_key) ⇒ Object

Returns user data for the user that requested the application launch. Params:

> launch_key: launch_key that was passed to the application’s server.



46
47
48
# File 'lib/edmodo-api/client.rb', line 46

def launch_requests launch_key
  request :get, resource_uri("launchRequests", @format), {:launch_key => launch_key}
end

#members(group_id) ⇒ Object

Returns an array of user data for members of a group, specified by group id. Params:

> group_id: integer Group ID



82
83
84
# File 'lib/edmodo-api/client.rb', line 82

def members group_id
  request :get, resource_uri("members", @format), {:group_id => group_id}
end

#new_event(user_token, description, start_date, end_date, user_recipients, group_recipients) ⇒ Object

Set a new event on behalf of a user to specified recipients. Events will be seen on the calendar as well as notifications as the event approaches. Params:

> user_token: The user_token of the user to set the event on behalf for.

> description: The description of the event.

> start_date: The start date of the event.

> end_date: The end date of the event. If this is a single day event, the end date should simply be the same as the start date.

> user_recipients: array of user_tokens that will receive the event.

> groups_recipients: array of group_ids that will receive the event.

Raises:



282
283
284
285
286
287
288
289
# File 'lib/edmodo-api/client.rb', line 282

def new_event user_token, description, start_date, end_date, user_recipients, group_recipients

  raise EdmodoApiError.new("Invalid object type for start or end date") unless start_date.is_a?(Date) && end_date.is_a?(Date)

  recipients = generate_user_groups_array user_recipients, group_recipients

  request(:post, resource_uri("newEvent"), {:user_token => user_token, :description => description, :start_date => start_date.to_s, :end_date => end_date.to_s, :recipients => recipients})
end

#new_grade(group_id, title, total) ⇒ Object

Add a new grade to the gradebook for a given group. Params:

> group_id: The group this grade will be created in

> title: The title for this grade

> total: The total grade possible for this grade



259
260
261
# File 'lib/edmodo-api/client.rb', line 259

def new_grade group_id, title, total
  request(:post, resource_uri("newGrade"), {:group_id => group_id, :title => title, :total => total})
end

#parents(user_token) ⇒ Object

Returns an array of parent user data, given a specified student user token. Params:

> user_token: User token of the student



162
163
164
# File 'lib/edmodo-api/client.rb', line 162

def parents user_token
  request :get, resource_uri("parents", @format), {:user_token => user_token}
end

#profiles(user_tokens) ⇒ Object

Fetches user profile information. Currently, this is only for teacher users to obtain their school information (if a school is set for the teacher). Params:

> user_token: Array of teacher user tokens.



179
180
181
182
183
184
# File 'lib/edmodo-api/client.rb', line 179

def profiles user_tokens

  user_tokens = Array(user_tokens)

  request :get, resource_uri("profiles", "json"), {:user_tokens => user_tokens.to_json}
end

#register_badge(badge_title, description, image_url) ⇒ Object

Registers a badge with Edmodo, returning a badge id that can be used to award a badge that will display on an Edmodo user’s profile. Params:

> badge_title: limit 50 characters

> description: limit 140 characters

> image_url: url to badge image, should be 114x114 pixels. Accepted image types: jpg, gif, png



220
221
222
# File 'lib/edmodo-api/client.rb', line 220

def register_badge badge_title, description, image_url 
  request(:post, resource_uri("registerBadge"), {:badge_title => badge_title, :description => description, :image_url => image_url}.delete_if { |k,v| v.nil? })
end

#revoke_badge(user_token, badge_id) ⇒ Object

Revokes a badge that has been awarded to a given user. Params:

> badge_id: Badge ID of the badge being revoked

> user_token: User token of the user who has been awarded the badge and whom it will be revoked from.



249
250
251
# File 'lib/edmodo-api/client.rb', line 249

def revoke_badge user_token, badge_id
  request(:post, resource_uri("revokeBadge"), {:user_token => user_token, :badge_id => badge_id})
end

#set_grade(user_token, grade_id, score) ⇒ Object

Set a score for a grade given a specific user token. Params:

> grade_id: The grade the score is being set upon

> user_token: The user_token of the user to set the grade for

> score: The score to set for this grade



269
270
271
# File 'lib/edmodo-api/client.rb', line 269

def set_grade user_token, grade_id, score
  request(:post, resource_uri("setGrade"), {:user_token => user_token, :grade_id => grade_id, :score => score})
end

#set_notification(user_token, notification_count) ⇒ Object

Sets a specified count of app notifications for the user. Params:

> user_token: The user_token of the user that will have the resource added to her/his library.

> notification_count: The number to add to the user’s notification count for the app



308
309
310
# File 'lib/edmodo-api/client.rb', line 308

def set_notification user_token, notification_count
  request(:post, resource_uri("setNotification"), {:user_token => user_token, :notification_count => notification_count})
end

#teacher_connections(user_token) ⇒ Object

Returns an array of assignments coming due (in the next 60 days) for the user specified by the token. Params:

> user_token: User token of the user



114
115
116
# File 'lib/edmodo-api/client.rb', line 114

def teacher_connections user_token
  request :get, resource_uri("teacherConnections", @format), {:user_token => user_token}
end

#teachermates(user_token) ⇒ Object

Returns an array of user data for all teachers that are connected to the teacher specified by user token. Params:

> user_token: User token of the teacher



106
107
108
# File 'lib/edmodo-api/client.rb', line 106

def teachermates user_token
  request :get, resource_uri("teachermates", @format), {:user_token => user_token}
end

#teachers(user_token) ⇒ Object

Returns an array of user data for all teachers for a student specified by user token. Params:

> user_token: User token of the student



98
99
100
# File 'lib/edmodo-api/client.rb', line 98

def teachers user_token
  request :get, resource_uri("teachers", @format), {:user_token => user_token}
end

#turn_in_assignment(user_token, assignment_id, content, attachments = nil) ⇒ Object

Submits a response to the specified assignment for the given user. The id’s for assignments coming due can be retrieved using the assignments_coming_due. Params:

> user_token: The user token for the user turning in the assignment.

> assignment_id: Assignment Id for the assignment to turn in, obtained from the assignments_coming_due

> content: Text of the submission

> attachments (Optional): Array of objects specifying links/embed codes to include in the assignment submission



210
211
212
# File 'lib/edmodo-api/client.rb', line 210

def turn_in_assignment user_token, assignment_id, content, attachments = nil
  request(:post, resource_uri("turnInAssignment"), {:user_token => user_token, :assignment_id => assignment_id, :content => content, :attachments => attachments}.delete_if{ |k,v| v.nil? } )
end

#update_badge(badge_id, badge_title, description, image_url = nil) ⇒ Object

Returns an array of user data for all teachers for a student specified by user token. Params:

> badge_id: the registered badge id

> badge_title: limit 50 characters

> description: limit 140 characters

> image_url (Optional): If you wish to replace the image of the badge, specify the url of the new badge image. Otherwise, to keep the old badge image, you do not need to specify this parameter.



231
232
233
# File 'lib/edmodo-api/client.rb', line 231

def update_badge badge_id, badge_title, description, image_url = nil
  request(:post, resource_uri("updateBadge"), {:badge_id => badge_id, :badge_title => badge_title, :description => description, :image_url => image_url}. delete_if{ |k,v| v.nil? })
end

#user_post(user_token, content, user_recipients, group_recipients, attachments = nil) ⇒ Object

Send a post to the recipient group(s) or user(s) from the user token specified. Params:

> user_token: The user token for the user sending the post.

> content: The text of the message.

> user_recipients: array of user_tokens that will receive the event.

> group_recipients: array of group_ids that will receive the event.

> attachments (Optional): array of objects specifying links/embed codes to include in the post message.



196
197
198
199
200
201
# File 'lib/edmodo-api/client.rb', line 196

def user_post user_token, content, user_recipients, group_recipients, attachments = nil

  recipients = generate_user_groups_array user_recipients, group_recipients

  request(:post, resource_uri("userPost"), {:user_token => user_token, :content => content, :recipients => recipients, :attachments => attachments}.delete_if{ |k,v| v.nil? })
end

#users(user_tokens) ⇒ Object

Returns user data for a given user token or array of user tokens. Params:

> user_tokens: array of user tokens



54
55
56
57
58
# File 'lib/edmodo-api/client.rb', line 54

def users user_tokens
  user_tokens = Array(user_tokens)

  request :get, resource_uri("users", @format), {:user_tokens => user_tokens.to_json}
end