Class: Databasedotcom::Chatter::Record

Inherits:
Object
  • Object
show all
Defined in:
lib/databasedotcom/chatter/record.rb

Overview

Superclasses all Chatter resources except feeds. Some methods may not be supported by the Force.com API for certain subclasses.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, response) ⇒ Record

Create a new record from the returned JSON response of an API request. Sets the client, name, id, url, and type attributes. Saves the raw response as raw_hash.



10
11
12
13
14
15
16
17
# File 'lib/databasedotcom/chatter/record.rb', line 10

def initialize(client, response)
  @client = client
  @raw_hash = response.is_a?(Hash) ? response : JSON.parse(response)
  @name = @raw_hash["name"]
  @id = @raw_hash["id"]
  @url = @raw_hash["url"]
  @type = @raw_hash["type"]
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



7
8
9
# File 'lib/databasedotcom/chatter/record.rb', line 7

def client
  @client
end

#idObject (readonly)

Returns the value of attribute id.



7
8
9
# File 'lib/databasedotcom/chatter/record.rb', line 7

def id
  @id
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/databasedotcom/chatter/record.rb', line 7

def name
  @name
end

#raw_hashObject (readonly)

Returns the value of attribute raw_hash.



7
8
9
# File 'lib/databasedotcom/chatter/record.rb', line 7

def raw_hash
  @raw_hash
end

#typeObject (readonly)

Returns the value of attribute type.



7
8
9
# File 'lib/databasedotcom/chatter/record.rb', line 7

def type
  @type
end

#urlObject (readonly)

Returns the value of attribute url.



7
8
9
# File 'lib/databasedotcom/chatter/record.rb', line 7

def url
  @url
end

Class Method Details

.all(client, parameters = {}) ⇒ Object

Return a Collection of all records.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/databasedotcom/chatter/record.rb', line 50

def self.all(client, parameters={})
  path_components = ["/services/data/v#{client.version}/chatter"]
  if parameters.has_key?(:user_id)
    path_components << "users/#{parameters[:user_id]}"
    parameters.delete(:user_id)
  end
  path_components << self.resource_name
  url = path_components.join('/')
  result = client.http_get(url, parameters)
  response = JSON.parse(result.body)
  collection = Databasedotcom::Collection.new(client, self.total_size_of_collection(response), response["nextPageUrl"], response["previousPageUrl"], response["currentPageUrl"])
  self.collection_from_response(response).each do |resource|
    collection << self.new(client, resource)
  end
  collection
end

.delete(client, resource_id, parameters = {}) ⇒ Object

Delete the Record identified by resource_id.



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/databasedotcom/chatter/record.rb', line 68

def self.delete(client, resource_id, parameters={})
  path_components = ["/services/data/v#{client.version}/chatter"]
  if parameters.has_key?(:user_id)
    path_components << "users/#{parameters[:user_id]}"
    parameters.delete(:user_id)
  end
  path_components << self.resource_name
  path_components << resource_id
  path = path_components.join('/')
  client.http_delete(path, parameters)
end

.find(client, resource_id, parameters = {}) ⇒ Object

Find a single Record or a Collection of records by id. resource_id can be a single id or a list of ids.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/databasedotcom/chatter/record.rb', line 20

def self.find(client, resource_id, parameters={})
  if resource_id.is_a?(Array)
    resource_ids = resource_id.join(',')
    url = "/services/data/v#{client.version}/chatter/#{self.resource_name}/batch/#{resource_ids}"
    response = JSON.parse(client.http_get(url, parameters).body)
    good_results = response["results"].select { |r| r["statusCode"] == 200 }
    collection = Databasedotcom::Collection.new(client, good_results.length)
    good_results.each do |result|
      collection << self.new(client, result["result"])
    end
    collection
  else
    path_components = ["/services/data/v#{client.version}/chatter"]
    if parameters.has_key?(:user_id)
      path_components << "users/#{parameters[:user_id]}"
      parameters.delete(:user_id)
    end
    path_components << "#{self.resource_name}/#{resource_id}"
    url = path_components.join('/')
    response = JSON.parse(client.http_get(url, parameters).body)
    self.new(client, response)
  end
end

.resource_nameObject

The REST resource name of this Record.

GroupMembership.resource_name  #=>  group-memberships


103
104
105
# File 'lib/databasedotcom/chatter/record.rb', line 103

def self.resource_name
  (self.name.split('::').last).resourcerize + "s"
end

.search(client, query, parameters = {}) ⇒ Object

Return a Collection of records that match the query.



45
46
47
# File 'lib/databasedotcom/chatter/record.rb', line 45

def self.search(client, query, parameters={})
  self.all(client, parameters.merge(self.search_parameter_name => query))
end

Instance Method Details

#delete(parameters = {}) ⇒ Object

Delete this record.



91
92
93
# File 'lib/databasedotcom/chatter/record.rb', line 91

def delete(parameters={})
  self.class.delete(self.client, self.id, parameters)
end

#parentObject

A Hash representation of the entity that is the parent of this Record.



86
87
88
# File 'lib/databasedotcom/chatter/record.rb', line 86

def parent
  self.raw_hash["parent"]
end

#reloadObject

Reload this record.



96
97
98
# File 'lib/databasedotcom/chatter/record.rb', line 96

def reload
  self.class.find(self.client, self.id)
end

#userObject

A Hash representation of the User that created this Record.



81
82
83
# File 'lib/databasedotcom/chatter/record.rb', line 81

def user
  self.raw_hash["user"]
end