edmodo-api 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -190,9 +190,13 @@ module Edmodo
190
190
  #
191
191
  # => user_token: The user token for the user sending the post.
192
192
  # => content: The text of the message.
193
- # => recipients: Array of objects specifying the recipients of the post. These can be either users (specified by a user_token) or groups (specified by a group_id).
193
+ # => user_recipients: array of user_tokens that will receive the event.
194
+ # => group_recipients: array of group_ids that will receive the event.
194
195
  # => attachments (Optional): array of objects specifying links/embed codes to include in the post message.
195
- def user_post user_token, content, recipients, attachments = nil
196
+ def user_post user_token, content, user_recipients, group_recipients, attachments = nil
197
+
198
+ recipients = generate_user_groups_array user_recipients, group_recipients
199
+
196
200
  request(:post, resource_uri("userPost"), {:user_token => user_token, :content => content, :recipients => recipients, :attachments => attachments}.delete_if{ |k,v| v.nil? })
197
201
  end
198
202
 
@@ -271,11 +275,17 @@ module Edmodo
271
275
  #
272
276
  # => user_token: The user_token of the user to set the event on behalf for.
273
277
  # => description: The description of the event.
274
- # => start_date: The start date of the event (specified in the format YYYY-MM-DD).
275
- # => end_date: The end date of the event (specified in the format YYYY-MM-DD). If this is a single day event, the end date should simply be the same as the start date.
276
- # => recipients: array of objects specifying the recipients of the post. These can be either users (specified by a user_token) or groups (specified by a group_id).
277
- def new_event user_token, description, start_date, end_date, recipients
278
- request(:post, resource_uri("newEvent"), {:user_token => user_token, :description => description, :start_date => start_date, :end_date => end_date, :recipients => recipients})
278
+ # => start_date: The start date of the event.
279
+ # => 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.
280
+ # => user_recipients: array of user_tokens that will receive the event.
281
+ # => groups_recipients: array of group_ids that will receive the event.
282
+ def new_event user_token, description, start_date, end_date, user_recipients, group_recipients
283
+
284
+ raise EdmodoApiError.new("Invalid object type for start or end date") unless start_date.is_a?(Date) && end_date.is_a?(Date)
285
+
286
+ recipients = generate_user_groups_array user_recipients, group_recipients
287
+
288
+ 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})
279
289
  end
280
290
 
281
291
  # Adds a resource (url or embed code) to a user's Library
@@ -308,6 +318,23 @@ module Edmodo
308
318
  }
309
319
  end
310
320
 
321
+ # Generates an array of hashes from an array of user ids and group ids
322
+ # Params:
323
+ #
324
+ # => user: array of user_tokens.
325
+ # => groups: array of group_ids.
326
+ def generate_user_groups_array users, groups
327
+ users = Array(users)
328
+
329
+ user_array = users.map{|token| {:user_token => token}}
330
+
331
+ groups = Array(groups)
332
+
333
+ group_array = groups.map{|id| {:group_id => id}}
334
+
335
+ (user_array | group_array).flatten
336
+ end
337
+
311
338
  # Checks for all required variables to be set when an instance is created and throws errors if they haven't
312
339
  def raise_init_errors
313
340
  raise EdmodoApiError.new("Edmodo API Error: Api key was not set") if @api_key.empty?
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Edmodo
4
4
  module API
5
- VERSION = "0.1.0"
5
+ VERSION = "0.1.1"
6
6
  end
7
7
  end
@@ -203,8 +203,11 @@ describe Edmodo::API::Client do
203
203
  end
204
204
 
205
205
  it 'should get the correct response back from the userPost request' do
206
-
207
- response = @client.user_post("b020c42d1", "This is my test message", [{:user_token => "b020c42d1"}, {:user_token => "693d5c765"}, {:group_id => 379557}], [{:type => "link", :title => "A link", :url => "http://www.edmodo.com"}, {:type => "embed", :title => "An embed with an optional thumbnail url", :thumb_url => "http://images.edmodo.com/images/logos/edmodo_134x43.png"}])
206
+ users = ["b020c42d1", "693d5c765"]
207
+
208
+ groups = 379557
209
+
210
+ response = @client.user_post("b020c42d1", "This is my test message", users, groups, [{:type => "link", :title => "A link", :url => "http://www.edmodo.com"}, {:type => "embed", :title => "An embed with an optional thumbnail url", :thumb_url => "http://images.edmodo.com/images/logos/edmodo_134x43.png"}])
208
211
 
209
212
  response.should == {"status" => "success"}
210
213
  end
@@ -254,9 +257,11 @@ describe Edmodo::API::Client do
254
257
 
255
258
  it 'should get the correct hash response from the newEvent request' do
256
259
 
257
- recipients = [{:user_token => "b020c42d1"},{:group_id => 379557}]
260
+ users = "b020c42d1"
261
+
262
+ groups = 379557
258
263
 
259
- response = @client.new_event("b020c42d1", "Pizza party tomorrow", "2011-12-07", "2011-12-07", recipients)
264
+ response = @client.new_event("b020c42d1", "Pizza party tomorrow", Date.today, Date.today, users, groups)
260
265
 
261
266
  response.should == {"event_id" => 621119}
262
267
  end
@@ -41,7 +41,7 @@ uri = "#{Edmodo::API::Config.endpoints[:sandbox]}/setGrade?api_key=#{@api_key}&g
41
41
  FakeWeb.register_uri(:post, uri, :body => '{"user_token":"83a8e614d", "score":"3", "total":"10"}')
42
42
 
43
43
  # newEvent request uri
44
- uri = "#{Edmodo::API::Config.endpoints[:sandbox]}/newEvent?api_key=#{@api_key}&user_token=b020c42d1&description=Pizza%20party%20tomorrow&start_date=2011-12-07&end_date=2011-12-07&recipients=%5B%7B%22user_token%22%3A%22b020c42d1%22%7D%2C%7B%22group_id%22%3A379557%7D%5D"
44
+ uri = "#{Edmodo::API::Config.endpoints[:sandbox]}/newEvent?api_key=#{@api_key}&user_token=b020c42d1&description=Pizza%20party%20tomorrow&start_date=#{Date.today.to_s}&end_date=#{Date.today.to_s}&recipients=%5B%7B%22user_token%22%3A%22b020c42d1%22%7D%2C%7B%22group_id%22%3A379557%7D%5D"
45
45
 
46
46
  FakeWeb.register_uri(:post, uri, :body => '{"event_id":621119}')
47
47
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: edmodo-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: