Class: Databasedotcom::Client

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#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

#query_result_cache_enabledObject

Returns the value of attribute query_result_cache_enabled.



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

def query_result_cache_enabled
  @query_result_cache_enabled
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

.username_password_login(opt) ⇒ Object

opt username, password, sandbox



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

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



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

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



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

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



102
103
104
105
# File 'lib/databasedotcom_additions/client.rb', line 102

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



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

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



107
108
109
110
111
112
113
114
115
# File 'lib/databasedotcom_additions/client.rb', line 107

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



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
# File 'lib/databasedotcom_additions/client.rb', line 68

def query_raw(soql)

  start_time = Time.now

  if query_result_cache_enabled
    cache_key = "#{self.username}:#{soql}"
    if cache[cache_key]
      puts "cache get - #{cache_key}"
      return cache[cache_key]
    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 query_result_cache_enabled
    puts "cache put - #{cache_key}"
    cache[cache_key] = resp
  end

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

  resp
end