Module: OrgModel

Included in:
Salesforce::Rest::AsfRest
Defined in:
lib/Salesforce/rest/asf_rest_org_model.rb

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object

class methods



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/Salesforce/rest/asf_rest_org_model.rb', line 22

def self.included(base)
  class << base
    # Get all available SObjects & their meta-data for an organization
    def describe_global(header=Salesforce::Rest::AsfRest.send(:class_variable_get, "@@auth_header"),
        rest_svr=Salesforce::Rest::AsfRest.send(:class_variable_get, "@@rest_svr"),
        api_version=Salesforce::Rest::AsfRest.send(:class_variable_get, "@@api_version"))
      path = "/services/data/#{api_version}/sobjects/"
      target = rest_svr + path
      resp = call_rest_svr("GET", target, header, nil)
      #resp = get(path)
      if (resp.code != 200) || !resp.success?
        message = ActiveSupport::JSON.decode(resp.body)[0]["message"]
        Salesforce::Rest::ErrorManager.raise_error("HTTP code " + resp.code.to_s + ": " + message, resp.code)
      end
      return resp
    end

    # get available versions for making REST API calls
    def get_version(header=Salesforce::Rest::AsfRest.send(:class_variable_get, "@@auth_header"),
        rest_svr=Salesforce::Rest::AsfRest.send(:class_variable_get, "@@rest_svr"),
        api_version=Salesforce::Rest::AsfRest.send(:class_variable_get, "@@api_version"))
      path = '/services/data/'
      target = rest_svr + path
      resp = call_rest_svr("GET", target, header, nil)
      if (resp.code != 200) || !resp.success?
        message = ActiveSupport::JSON.decode(resp.body)[0]["message"]
        Salesforce::Rest::ErrorManager.raise_error("HTTP code " + resp.code.to_s + ": " + message, resp.code)
      end
      return resp
    end

    # get resources available for REST, e.g. Query, Search, SObjects, Recent
    def list_available_resources(header=Salesforce::Rest::AsfRest.send(:class_variable_get, "@@auth_header"),
        rest_svr=Salesforce::Rest::AsfRest.send(:class_variable_get, "@@rest_svr"),
        api_version=Salesforce::Rest::AsfRest.send(:class_variable_get, "@@api_version"))
      path = "/services/data/#{api_version}/"
      target = rest_svr + path
      resp = call_rest_svr("GET", target, header, nil)
      if (resp.code != 200) || !resp.success?
        message = ActiveSupport::JSON.decode(resp.body)[0]["message"]
        Salesforce::Rest::ErrorManager.raise_error("HTTP code " + resp.code.to_s + ": " + message, resp.code)
      end
      return resp
    end

    #Get detailed info about a single Salesforce SObject, e.g. Name, fields, edit template, etc.
    def get_detail_info(header=Salesforce::Rest::AsfRest.send(:class_variable_get, "@@auth_header"),
        rest_svr=Salesforce::Rest::AsfRest.send(:class_variable_get, "@@rest_svr"),
        api_version=Salesforce::Rest::AsfRest.send(:class_variable_get, "@@api_version"))
      class_name = self.name.gsub(/\S+::/mi, "")
      path = "/services/data/#{api_version}/sobjects/#{class_name}/describe"
      target = rest_svr + path        
      resp = call_rest_svr("GET", target, header, nil)
      if (resp.code != 200) || !resp.success?
        message = ActiveSupport::JSON.decode(resp.body)[0]["message"]
        Salesforce::Rest::ErrorManager.raise_error("HTTP code " + resp.code.to_s + ": " + message, resp.code)
      end
      if (resp.code != 200) || !resp.success?
        message = ActiveSupport::JSON.decode(resp.body)[0]["message"]
        Salesforce::Rest::ErrorManager.raise_error("HTTP code " + resp.code.to_s + ": " + message, resp.code)
      end
      return resp
    end

    #get meta data about an SObject, including rows of recent items
    def (header=Salesforce::Rest::AsfRest.send(:class_variable_get, "@@auth_header"),
        rest_svr=Salesforce::Rest::AsfRest.send(:class_variable_get, "@@rest_svr"),
        api_version=Salesforce::Rest::AsfRest.send(:class_variable_get, "@@api_version"))
      class_name = self.name.gsub(/\S+::/mi, "")
      path = "/services/data/#{api_version}/sobjects/#{class_name}/"
      target = rest_svr + path
      resp = call_rest_svr("GET", target, header, nil)
      if (resp.code != 200) || !resp.success?
        message = ActiveSupport::JSON.decode(resp.body)[0]["message"]
        Salesforce::Rest::ErrorManager.raise_error("HTTP code " + resp.code.to_s + ": " + message, resp.code)
      end
      return resp
    end

  end
end