Class: Databasedotcom::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/databasedotcom_additions/client.rb,
lib/databasedotcom_additions/proxy_patch.rb

Constant Summary collapse

SERIALIZE_PROPERTIES =
%w(host username password version oauth_token instance_url session_id server_url)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#cache_mgrObject

Returns the value of attribute cache_mgr.



10
11
12
# File 'lib/databasedotcom_additions/client.rb', line 10

def cache_mgr
  @cache_mgr
end

#login_failed_user_messageObject

Returns the value of attribute login_failed_user_message.



11
12
13
# File 'lib/databasedotcom_additions/client.rb', line 11

def 
  @login_failed_user_message
end

#server_urlObject

Returns the value of attribute server_url.



8
9
10
# File 'lib/databasedotcom_additions/client.rb', line 8

def server_url
  @server_url
end

#session_idObject

Returns the value of attribute session_id.



7
8
9
# File 'lib/databasedotcom_additions/client.rb', line 7

def session_id
  @session_id
end

Class Method Details

.client_from_serialized(properties) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/databasedotcom_additions/client.rb', line 50

def self.client_from_serialized(properties)
  client = Databasedotcom::Client.new :verify_mode => OpenSSL::SSL::VERIFY_NONE
  SERIALIZE_PROPERTIES.each do |prop|
    client.send("#{prop}=", properties[prop])
  end
  client
end

.username_password_login(opt) ⇒ Object

opt username, password, sandbox



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/databasedotcom_additions/client.rb', line 18

def self.(opt)
  client = Databasedotcom::Client.new :verify_mode => OpenSSL::SSL::VERIFY_NONE
  client.host = opt[:sandbox] ? "test.salesforce.com" : "login.salesforce.com"
  begin
    binding = RForce::Binding.new("https://#{client.host}/services/Soap/u/20.0", nil)
    response = binding.(opt[:username], opt[:password])
    token = response.loginResponse.result.sessionId
    serverUrl = response.loginResponse.result.serverUrl
    uri = URI(serverUrl)
    client.username = opt[:username]
    client.password = opt[:password]
    client.version = '22.0'
    client.oauth_token = token
    client.instance_url = "#{uri.scheme}://#{uri.host}"

    client.session_id = token
    client.server_url = serverUrl

  rescue => e
    client. = e.message
  end
  client
end

Instance Method Details

#cacheObject



58
59
60
61
62
63
64
65
66
67
# File 'lib/databasedotcom_additions/client.rb', line 58

def cache

  if ! defined?(@global_cache)
    cache_file_name = 'cache/global-cache.json'
    @global_cache ||= UtilityPack::PersistentHash.new cache_file_name
  end

  @global_cache

end

#count(soql_expr) ⇒ Object

support for count query - SELECT COUNT() FROM Account_vod__c



70
71
72
73
# File 'lib/databasedotcom_additions/client.rb', line 70

def count(soql_expr)
  result = http_get("/services/data/v#{self.version}/query", :q => soql_expr)
  JSON.parse(result.body)['totalSize']
end

#https_request(host = nil) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/databasedotcom_additions/proxy_patch.rb', line 10

def https_request(host=nil)

  if proxy
    http = Net::HTTP::Proxy(proxy.host, proxy.port).new(host || URI.parse(self.instance_url).host, 443)
  else
    http = Net::HTTP.new(host || URI.parse(self.instance_url).host, 443)
  end

  http.tap do |http|
    http.use_ssl = true
    http.ca_file = self.ca_file if self.ca_file
    http.verify_mode = self.verify_mode if self.verify_mode
  end
end

#next_page_raw(path) ⇒ Object



122
123
124
125
# File 'lib/databasedotcom_additions/client.rb', line 122

def next_page_raw(path)
  result = http_get(path)
  JSON.parse(result.body)
end

#proxyObject



5
6
7
8
# File 'lib/databasedotcom_additions/proxy_patch.rb', line 5

def proxy
  http_proxy = ENV["http_proxy"]
  URI.parse(http_proxy) rescue nil
end

#query(soql) ⇒ Object



75
76
77
78
79
80
81
82
83
84
# File 'lib/databasedotcom_additions/client.rb', line 75

def query(soql)
  start_time = Time.now
  
  resp = query_orig(soql)

  elapsed_time = Time.now - start_time
  resp['meta'] = {'query' => soql, 'elapsed_time' => elapsed_time}
  
  resp
end

#query_all(soql_expr) ⇒ Object



127
128
129
130
131
132
133
134
135
# File 'lib/databasedotcom_additions/client.rb', line 127

def query_all(soql_expr)
  all_records = []
  records = query(soql_expr)
  while records.total_size > 0
    all_records.concat(records)
    records = records.next_page
  end
  all_records
end

#query_origObject



13
# File 'lib/databasedotcom_additions/client.rb', line 13

alias_method :query_orig, :query

#query_raw(soql) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/databasedotcom_additions/client.rb', line 86

def query_raw(soql)

  start_time = Time.now

  cache_key = "#{self.username}:#{soql}"

  if self.cache_mgr != nil
    cache_value = self.cache_mgr.get(cache_key)
    if cache_value
      puts "cache get - #{cache_key}"
      return JSON.parse(cache_value)
    end
  end


  result = http_get("/services/data/v#{self.version}/query", :q => soql)
  resp = JSON.parse(result.body)

  next_resp = resp
  while next_resp['done'] == false
    puts "*** nextRecordsUrl = #{next_resp['nextRecordsUrl']} ***"
    next_resp = next_page_raw(next_resp['nextRecordsUrl'])
    resp['records'] = resp['records'].concat(next_resp['records'])
  end

  if self.cache_mgr != nil
    puts "cache put - #{cache_key}"
    self.cache_mgr.set(cache_key, resp.to_json)
  end

  elapsed_time = Time.now - start_time
  resp['meta'] = {'query' => soql, 'elapsed_time' => elapsed_time}

  resp
end

#serializeObject



42
43
44
45
46
47
48
# File 'lib/databasedotcom_additions/client.rb', line 42

def serialize
  o = {}
  SERIALIZE_PROPERTIES.each do |prop|
    o[prop] = self.send(prop.to_sym)
  end
  o
end