Class: SalesforceCache::RestClient

Inherits:
Object
  • Object
show all
Defined in:
lib/salesforce_cache/rest_client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dParamsGeneral = {}, dParamsToken = {}) ⇒ RestClient

Returns a new instance of RestClient.


10
11
12
13
14
15
16
17
18
# File 'lib/salesforce_cache/rest_client.rb', line 10

def initialize(dParamsGeneral={},dParamsToken={})
  @dParamsToken = dParamsToken
  @sSfTokenUrl  = dParamsGeneral.has_key?(:token_url)   ? dParamsGeneral[:token_url] \
                : 'https://login.salesforce.com/services/oauth2/token'
  @sApiFqdn     = dParamsGeneral.has_key?(:api_fqdn)    ? dParamsGeneral[:api_fqdn] : 'na5.salesforce.com'
  @sApiVersion  = dParamsGeneral.has_key?(:api_version) ? dParamsGeneral[:api_version] : '27.0'
  @dTokenRes    = {}
  self.loadToken()
end

Instance Attribute Details

#dTokenResObject

Returns the value of attribute dTokenRes


8
9
10
# File 'lib/salesforce_cache/rest_client.rb', line 8

def dTokenRes
  @dTokenRes
end

#sSfTokenUrlObject

Returns the value of attribute sSfTokenUrl


7
8
9
# File 'lib/salesforce_cache/rest_client.rb', line 7

def sSfTokenUrl
  @sSfTokenUrl
end

Instance Method Details

#getAttr(yKey = nil) ⇒ Object


26
27
28
29
30
# File 'lib/salesforce_cache/rest_client.rb', line 26

def getAttr(yKey=nil)
  yKey  = yKey.to_sym if yKey.is_a?(String)
  xxVal = @dTokenRes.has_key?(yKey) ? @dTokenRes[yKey] : nil
  return xxVal
end

#getHeadersObject


32
33
34
35
# File 'lib/salesforce_cache/rest_client.rb', line 32

def getHeaders()
  dHeaders = { 'Authorization' => 'Bearer ' + self.getAttr(:access_token) }
  return dHeaders
end

#getSfUrlForSfidAndType(sSfid = nil, sType = nil) ⇒ Object


46
47
48
49
# File 'lib/salesforce_cache/rest_client.rb', line 46

def getSfUrlForSfidAndType(sSfid=nil,sType=nil)
  sUrl  = "https://#{@sApiFqdn}/services/data/v#{@sApiVersion}/sobjects/#{sType}/#{sSfid}/"
  return sUrl
end

#getSobjectForSfidAndType(sSfid = nil, sType = nil) ⇒ Object


51
52
53
54
55
56
57
# File 'lib/salesforce_cache/rest_client.rb', line 51

def getSobjectForSfidAndType(sSfid=nil,sType=nil)
  sUrl  = self.getSfUrlForSfidAndType(sSfid,sType)
  sHead = self.getHeaders()
  oRes  = Faraday.get(sUrl,{},sHead)
  dRes  = JSON.parse(oRes.body,:symbolize_names=>true)
  return dRes
end

#getSoqlResults(sSoql = nil) ⇒ Object


37
38
39
40
41
42
43
44
# File 'lib/salesforce_cache/rest_client.rb', line 37

def getSoqlResults(sSoql=nil)
  dQry  = { :q => sSoql}
  sUrl  = "https://#{@sApiFqdn}/services/data/v#{@sApiVersion}/query"
  dHead = self.getHeaders()
  oRes  = Faraday.get(sUrl,dQry,dHead)
  dRes  = JSON.parse(oRes.body,:symbolize_names=>true)
  return dRes
end

#loadTokenObject


20
21
22
23
24
# File 'lib/salesforce_cache/rest_client.rb', line 20

def loadToken()
  oRes       = Faraday.post(@sSfTokenUrl,@dParamsToken) 
  @dTokenRes = JSON.parse(oRes.body,:symbolize_names=>true)
  return self
end