1 module Databasedotcom
2 module Chatter
3 # Defines methods for entities that can have photos i.e. Users, Groups.
4 module PhotoMethods
5 def self.included(base)
6 base.extend ClassMethods
7 end
8
9 # Defines class methods for resources that can have photos.
10 module ClassMethods
11 # Returns a Hash with urls for the small and large versions of the photo for a resource.
12 def photo(client, resource_id)
13 url = "/services/data/v#{client.version}/chatter/#{self.resource_name}/#{resource_id}/photo"
14 result = client.http_get(url)
15 JSON.parse(result.body)
16 end
17
18 # Uploads a photo for a resource with id _resource_id_.
19 #
20 # User.upload_photo(@client, "me", File.open("SomePicture.png"), "image/png")
21 def upload_photo(client, resource_id, io, file_type)
22 url = "/services/data/v#{client.version}/chatter/#{self.resource_name}/#{resource_id}/photo"
23 result = client.http_multipart_post(url, {"fileUpload" => UploadIO.new(io, file_type)})
24 JSON.parse(result.body)
25 end
26
27 # Deletes the photo for the resource with id _resource_id_.
28 def delete_photo(client, resource_id)
29 client.http_delete "/services/data/v#{client.version}/chatter/#{self.resource_name}/#{resource_id}/photo"
30 end
31 end
32
33 # Returns a Hash with urls for the small and large versions of the photo for this resource.
34 #
35 # User.find(@client, "me").photo #=> {"smallPhotoUrl"=>"/small/photo/url", "largePhotoUrl"=>"/large/photo/url"}
36 def photo
37 self.raw_hash["photo"]
38 end
39
40 # Uploads a photo for this resource.
41 #
42 # me = User.find(@client)
43 # me.upload_photo(File.open("SomePicture.png"), "image/png")
44 def upload_photo(io, file_type)
45 self.class.upload_photo(self.client, self.id, io, file_type)
46 end
47
48 # Deletes the photo for this resource.
49 def delete_photo
50 self.class.delete_photo(self.client, self.id)
51 photo
52 end
53 end
54 end
55 en