1 require 'json'
2
3 module Databasedotcom
4 module Chatter
5 # Parent class of all feeds and inherits from Collection. This class is not intended to be instantiated. Methods should be called on subclasses, which are all are dynamically defined (except for FilterFeed). Defined feeds are *NewsFeed*, *UserProfileFeed*, *RecordFeed*, *ToFeed*, *PeopleFeed*, *GroupsFeed*, *FilesFeed*, *CompanyFeed*, and *FilterFeed*.
6 class Feed < Collection
7
8 # Returns an enumerable Feed of FeedItem objects that make up the feed with the specified _id_. Should not be called as a class method on Feed, but as a method on subclasses.
9 #
10 # NewsFeed.find(@client) #=> [#<FeedItem ...>, #<FeedItem ...>, ...]
11 # PeopleFeed.find(@client, "userid") #=> [#<FeedItem ...>, #<FeedItem ...>, ...]
12 # FilterFeed.find(@client, "me", "000") #=> [#<FeedItem ...>, #<FeedItem ...>, ...]
13 #
14 # _id_prefix_ is only applicable for FilterFeed.
15 def self.find(client, id="me", id_prefix=nil)
16 path_components = %w(services data)
17 path_components << "v#{client.version}"
18 path_components.concat(%w(chatter feeds))
19 path_components << feed_type
20 path_components << id unless feed_type == "company"
21 path_components << id_prefix
22 path_components << "feed-items"
23 path = "/" + path_components.compact.join('/')
24 result = client.http_get(path)
25 response = JSON.parse(result.body)
26 collection = self.new(client, nil, response["nextPageUrl"], response["previousPageUrl"], response["currentPageUrl"])
27 response["items"].each do |item|
28 collection << FeedItem.new(client, item)
29 end
30 collection
31 end
32
33 # Posts a FeedItem to a Feed specified by _user_id_. Should not be called as a class method on Feed, but as a method on subclasses.
34 #
35 # UserProfileFeed.post(@client, "me", :text => "This is a status update about Salesforce.", :url => "http://www.salesforce.com")
36 #
37 # Returns the newly created FeedItem.
38 def self.post(client, user_id, parameters)
39 url = "/services/data/v#{client.version}/chatter/feeds/#{feed_type}/#{user_id}/feed-items"
40 response = client.http_post(url, nil, parameters)
41 Databasedotcom::Chatter::FeedItem.new(client, response.body)
42 end
43
44 # Posts a file to a Feed specified by _user_id_. Should not be called as a class method on Feed, but as a method on subclasses.
45 #
46 # UserProfileFeed.post_file(@client, "me", File.open("MyFile"), "text/plain", "MyFile", :desc => "This is an uploaded text file.")
47 #
48 # Returns the newly created FeedItem.
49 def self.post_file(client, user_id, io, file_type, file_name, parameters={})
50 url = "/services/data/v#{client.version}/chatter/feeds/#{feed_type}/#{user_id}/feed-items"
51 response = client.http_multipart_post(url, {"feedItemFileUpload" => UploadIO.new(io, file_type, file_name), "fileName" => file_name}, parameters)
52 Databasedotcom::Chatter::FeedItem.new(client, response.body)
53 end
54
55 private
56
57 def self.feed_type
58 self.name.match(/.+::(.+)Feed$/)[1].resourcerize
59 end
60 end
61
62 FEED_TYPES = %w(News UserProfile Record To People Groups Files Company)
63 end
64 en