Class: Salesforce::Rest::AsfRest

Inherits:
ActiveResource::Base
  • Object
show all
Includes:
Authenticate, CachedCalls, CallRemote, HTTParty, OrgModel
Defined in:
lib/Salesforce/rest/asf_rest.rb

Overview

This is the mother class of all Salesforce REST objects all subclasses need to set the collection name. In ActiveResource convention, pluralized elements has the ending ‘s’; whereas, in Force.com REST, that ‘s’ is not there. e.g. set_collection_name “User”

TODO cannot do “SObject.find(:all)” due to a defect in the ActiveResource framework, see -> ActiveResource::Base line # 885

def instantiate_collection(collection, prefix_options = {})
      collection.collect! { |record| instantiate_record(record, prefix_options) }
end

As Ruby Hash has not collect! method, only Array, We we get back from Salesforce is a hash <sobject><objectDescribe><.….></objectDescribe><recentItems>…</recentItems></sobject>

Defined Under Namespace

Classes: TrackRequest

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CachedCalls

included

Methods included from OrgModel

included

Methods included from CallRemote

included

Class Method Details

.collection_path(prefix_options = {}, query_options = nil) ⇒ Object

removing .…../UID.json



267
268
269
270
# File 'lib/Salesforce/rest/asf_rest.rb', line 267

def collection_path(prefix_options = {}, query_options = nil)
  prefix_options, query_options = split_options(prefix_options) if query_options.nil?
  "#{prefix(prefix_options)}#{collection_name}#{query_string(query_options)}"
end

.delete(id, 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")) ⇒ Object

Again the delete feature from ActiveResource does not work out of the box. Using custom delete function



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/Salesforce/rest/asf_rest.rb', line 140

def self.delete(id, 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}/#{id}"
  target = rest_svr + path
  resp = call_rest_svr("DELETE", target, header)

  # HTTP code 204 means it was successfully deleted.
  if resp.code != 204
    message = ActiveSupport::JSON.decode(resp.body)[0]["message"]
    Salesforce::Rest::ErrorManager.raise_error("HTTP code " + resp.code.to_s + ": " + message, resp.code.to_s)
  else
    return resp
  end
end

.element_path(id, prefix_options = {}, query_options = nil) ⇒ Object

removing .…../UID.xml



262
263
264
265
# File 'lib/Salesforce/rest/asf_rest.rb', line 262

def element_path(id, prefix_options = {}, query_options = nil)
  prefix_options, query_options = split_options(prefix_options) if query_options.nil?
  "#{prefix(prefix_options)}#{collection_name}/#{id}#{query_string(query_options)}"
end

.run_soql(query, 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")) ⇒ Object

Run SOQL, automatically CGI::escape the query for you.



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/Salesforce/rest/asf_rest.rb', line 191

def self.run_soql(query, 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, "")
  safe_query = CGI::escape(query)
  path = "/services/data/#{api_version}/query?q=#{safe_query}"
  target = rest_svr+path
  resp = call_rest_svr("GET", target, header)
  #resp = get(path, options)
  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.to_s)
  end
  return resp
end

.run_soql_with_credential(query, security_token, rest_svr, api_version) ⇒ Object

Run SOQL, automatically CGI::escape the query for you. This is with given credentials -> query, security_token, rest_svr, version the path with appropriate api_version, CGI escaping the query string is included in this method.



211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/Salesforce/rest/asf_rest.rb', line 211

def self.run_soql_with_credential(query, security_token, rest_svr, api_version)
  header = { "Authorization" => "OAuth " + security_token, "content-Type" => 'application/json' }
  #set the path with appropriate api_version, include CGI escaping the query string
  safe_query = CGI::escape(query)
  path = "/services/data/#{api_version}/query?q=#{safe_query}"
  target = rest_svr + path
  #get the result
  resp = call_rest_svr("GET", target, header)
  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.to_s)
  end
  return resp
end

.run_sosl(search, 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")) ⇒ Object

Run SOSL, do not use CGI::escape -> SF will complain about missing braces



228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/Salesforce/rest/asf_rest.rb', line 228

def self.run_sosl(search, 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"))
  options = { :query => {:q => search}}
  class_name = self.name.gsub(/\S+::/mi, "")
  path = URI.escape("/services/data/#{api_version}/search/?q=#{search}")
  target = rest_svr + path
  resp = call_rest_svr("GET", target, header)
  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.to_s)
  end
  return resp
end

.run_sosl_with_credential(search, security_token, rest_svr, api_version) ⇒ Object

Run SOSL, do not use CGI::escape -> SF will complain about missing braces This is with given credentials -> Search_query, security_token, rest_svr, version



245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/Salesforce/rest/asf_rest.rb', line 245

def self.run_sosl_with_credential(search, security_token, rest_svr, api_version)
  header = { "Authorization" => "OAuth " + security_token, "content-Type" => 'application/json' }
  #set the path with appropriate api_version, with the search string
  path = URI.escape("/services/data/#{api_version}/search/?q=#{search}")
  target = rest_svr + path
  #get the result
  resp = call_rest_svr("GET", target, header)
  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.to_s)
  end
  return resp
end

.set_headers(auth_setting) ⇒ Object

set header for httparty



52
53
54
# File 'lib/Salesforce/rest/asf_rest.rb', line 52

def self.set_headers (auth_setting)
  headers (auth_setting)
end

.setup(oauth_token, rest_svr, api_version) ⇒ Object

We are mocking OAuth type authentication. In our case, we use the SessionID obtained from the initial SOAP Web Services call - ‘login()’ OAuth2 is geared toward website to website authentication. In our case, we are the background data interchange between RoR app and Force.com database. Therefore, we use security id. example: connection.set_header(“Authorization”, ‘OAuth 00DA0000000XwIQ!AQIAQD_BX.pdxMz0YBKdkz45PijY0gMxH65JwvV6Yj4.hf44WJYqO9ug7DfhNbnxuO9buhbftiX9Qv5DyBLHauaJhqTh79vi’)

self.abstract_class = true

Setup the adapter



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/Salesforce/rest/asf_rest.rb', line 83

def self.setup(oauth_token, rest_svr, api_version)
  @@oauth_token = oauth_token
  @@rest_svr = rest_svr
  @@api_version = api_version ? api_version : "v21.0"  #take a dynamic api server version
  @@rest_svr_url = rest_svr + "/services/data/#{api_version}/sobjects"
  @@ssl_port = 443  # TODO, right SF use port 443 for all HTTPS traffic.

  #ActiveResource setting
  #self.site = "https://" +  @@rest_svr_url
  self.site = @@rest_svr_url
  connection.set_header("Authorization", "OAuth " + @@oauth_token)

  # To be used by HTTParty
  @@auth_header = {
    "Authorization" => "OAuth " + @@oauth_token,
    "content-Type" => 'application/json'
  }
  # either application/xml or application/json
  base_uri rest_svr
  self.format = :json

  return self
end

.update(id, serialized_data_json, 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")) ⇒ Object

Update an object # TODO to use the call_rest_svr method



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/Salesforce/rest/asf_rest.rb', line 165

def self.update(id, serialized_data_json, 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"))

  #Again the delete feature from ActiveResource does not work out of the box.
  #Providing a custom update function
  svr_url_4_http = rest_svr.gsub(/https:\/\//mi, "" )  #strip http:// prefix from the url. Otherwise, it will fail.
  http = Net::HTTP.new(svr_url_4_http, @@ssl_port)
  http.use_ssl = true
  class_name = self.name.gsub(/\S+::/mi, "")
  path = "/services/data/#{api_version}/sobjects/#{class_name}/#{id}"
  code = serialized_data_json
  #   format -> Net::HTTPGenericRequest.new(m, reqbody, resbody, path, initheader)
  req = Net::HTTPGenericRequest.new("PATCH", true, true, path, header)
  resp = http.request(req, code) { |response|  }

  # HTTP code 204 means it was successfully updated. 204 for httparty, '204' for Net::HTTP
  if resp.code != '204'
    message = ActiveSupport::JSON.decode(resp.body)[0]["message"]
    Salesforce::Rest::ErrorManager.raise_error("HTTP code " + resp.code.to_s + ": " + message, resp.code.to_s)
  else
    return resp
  end
end

Instance Method Details

#save(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")) ⇒ Object

Save the Object, Note: there is an inconsistency between the Salesforce REST JSON create object, which is just “Name1”:“value1”,“Name2”:“value2” where as the ‘save’ method of the ActiveResource produces a JSON of Name”:{“Name1”:“value1”,“Name2”:“value2”}. The Extra/missing ‘Object Name’ causes this to break. When this consistency is resolved, this method should be removed. header = {

  "Authorization" => "OAuth " + @@oauth_token,
  "content-Type" => 'application/json'
}

rest_svr = ‘na7.salesforce.com’ api_version = ‘v21.0’ with v prefix



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/Salesforce/rest/asf_rest.rb', line 119

def save(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.class.name.gsub(/\S+::/mi, "")
  path = "/services/data/#{api_version}/sobjects/#{class_name}/"
  target = rest_svr + path
  data = ActiveSupport::JSON::encode(attributes)

  resp = call_rest_svr("POST", target, header, data)

  # HTTP code 201 means it was successfully saved.
  if resp.code != 201
    message = ActiveSupport::JSON.decode(resp.body)[0]["message"]
    Salesforce::Rest::ErrorManager.raise_error("HTTP code " + resp.code.to_s + ": " + message, resp.code.to_s)
  else
    return resp
  end
end