File: databasedotcom/chatter/record.rb

Overview
Module Structure
Class Hierarchy
Code

Overview

Module Structure

  module: <Toplevel Module>
  module: Databasedotcom#3
  module: Chatter#4
  class: Record#6
inherits from
  Object ( Builtin-Module )
has properties
attribute: raw_hash [R] #7
attribute: name [R] #7
attribute: id [R] #7
attribute: url [R] #7
attribute: type [R] #7
attribute: client [R] #7
method: initialize / 2 #10
class method: find / 3 #20
class method: search / 3 #45
class method: all / 2 #50
class method: delete (1/2) / 3 #68
method: user #81
method: parent #86
method: delete (2/E) / 1 #91
method: reload #96
class method: resource_name #103
class method: total_size_of_collection / 1 #109
class method: collection_from_response / 1 #113
class method: search_parameter_name #117

Class Hierarchy

Code

   1  require 'json'
   2 
   3  module Databasedotcom
   4    module Chatter
   5      # Superclasses all Chatter resources except feeds. Some methods may not be supported by the Force.com API for certain subclasses.
   6      class Record
   7        attr_reader :raw_hash, :name, :id, :url, :type, :client
   8 
   9        # 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        def initialize(client, response)
  11          @client = client
  12          @raw_hash = response.is_a?(Hash) ? response : JSON.parse(response)
  13          @name = @raw_hash["name"]
  14          @id = @raw_hash["id"]
  15          @url = @raw_hash["url"]
  16          @type = @raw_hash["type"]
  17        end
  18 
  19        # Find a single Record or a Collection of records by id. _resource_id_ can be a single id or a list of ids.
  20        def self.find(client, resource_id, parameters={})
  21          if resource_id.is_a?(Array)
  22            resource_ids = resource_id.join(',')
  23            url = "/services/data/v#{client.version}/chatter/#{self.resource_name}/batch/#{resource_ids}"
  24            response = JSON.parse(client.http_get(url, parameters).body)
  25            good_results = response["results"].select { |r| r["statusCode"] == 200 }
  26            collection = Databasedotcom::Collection.new(client, good_results.length)
  27            good_results.each do |result|
  28              collection << self.new(client, result["result"])
  29            end
  30            collection
  31          else
  32            path_components = ["/services/data/v#{client.version}/chatter"]
  33            if parameters.has_key?(:user_id)
  34              path_components << "users/#{parameters[:user_id]}"
  35              parameters.delete(:user_id)
  36            end
  37            path_components << "#{self.resource_name}/#{resource_id}"
  38            url = path_components.join('/')
  39            response = JSON.parse(client.http_get(url, parameters).body)
  40            self.new(client, response)
  41          end
  42        end
  43 
  44        # Return a Collection of records that match the _query_.
  45        def self.search(client, query, parameters={})
  46          self.all(client, parameters.merge(self.search_parameter_name => query))
  47        end
  48 
  49        # Return a Collection of all records.
  50        def self.all(client, parameters={})
  51          path_components = ["/services/data/v#{client.version}/chatter"]
  52          if parameters.has_key?(:user_id)
  53            path_components << "users/#{parameters[:user_id]}"
  54            parameters.delete(:user_id)
  55          end
  56          path_components << self.resource_name
  57          url = path_components.join('/')
  58          result = client.http_get(url, parameters)
  59          response = JSON.parse(result.body)
  60          collection = Databasedotcom::Collection.new(client, self.total_size_of_collection(response), response["nextPageUrl"], response["previousPageUrl"], response["currentPageUrl"])
  61          self.collection_from_response(response).each do |resource|
  62            collection << self.new(client, resource)
  63          end
  64          collection
  65        end
  66 
  67        # Delete the Record identified by _resource_id_.
  68        def self.delete(client, resource_id, parameters={})
  69          path_components = ["/services/data/v#{client.version}/chatter"]
  70          if parameters.has_key?(:user_id)
  71            path_components << "users/#{parameters[:user_id]}"
  72            parameters.delete(:user_id)
  73          end
  74          path_components << self.resource_name
  75          path_components << resource_id
  76          path = path_components.join('/')
  77          client.http_delete(path, parameters)
  78        end
  79 
  80        # A Hash representation of the User that created this Record.
  81        def user
  82          self.raw_hash["user"]
  83        end
  84 
  85        # A Hash representation of the entity that is the parent of this Record.
  86        def parent
  87          self.raw_hash["parent"]
  88        end
  89 
  90        # Delete this record.
  91        def delete(parameters={})
  92          self.class.delete(self.client, self.id, parameters)
  93        end
  94 
  95        # Reload this record.
  96        def reload
  97          self.class.find(self.client, self.id)
  98        end
  99 
 100        # The REST resource name of this Record.
 101        #
 102        #    GroupMembership.resource_name  #=>  group-memberships
 103        def self.resource_name
 104          (self.name.split('::').last).resourcerize + "s"
 105        end
 106 
 107        protected
 108 
 109        def self.total_size_of_collection(response)
 110          response["total"] || response["totalMemberCount"]
 111        end
 112 
 113        def self.collection_from_response(response)
 114          response[self.resource_name]
 115        end
 116 
 117        def self.search_parameter_name
 118          :q
 119        end
 120      end
 121    end
 122  en