databasedotcom 1.1.4 → 1.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -29,6 +29,8 @@ module Databasedotcom
29
29
  attr_accessor :username
30
30
  # The SalesForce password
31
31
  attr_accessor :password
32
+ # The SalesForce organization id for the authenticated user's Salesforce instance
33
+ attr_reader :org_id
32
34
 
33
35
  # Returns a new client object. _options_ can be one of the following
34
36
  #
@@ -86,8 +88,7 @@ module Databasedotcom
86
88
  # Raises SalesForceError if an error occurs
87
89
  def authenticate(options = nil)
88
90
  if user_and_pass?(options)
89
- req = Net::HTTP.new(self.host, 443)
90
- req.use_ssl=true
91
+ req = https_request(self.host)
91
92
  user = self.username || options[:username]
92
93
  pass = self.password || options[:password]
93
94
  path = "/services/oauth2/token?grant_type=password&client_id=#{self.client_id}&client_secret=#{client_secret}&username=#{user}&password=#{pass}"
@@ -100,7 +101,7 @@ module Databasedotcom
100
101
  parse_auth_response(result.body)
101
102
  elsif options.is_a?(Hash)
102
103
  if options.has_key?("provider")
103
- @user_id = options["extra"]["user_hash"]["user_id"] rescue nil
104
+ parse_user_id_and_org_id_from_identity_url(options["uid"])
104
105
  self.instance_url = options["credentials"]["instance_url"]
105
106
  self.oauth_token = options["credentials"]["token"]
106
107
  self.refresh_token = options["credentials"]["refresh_token"]
@@ -117,6 +118,11 @@ module Databasedotcom
117
118
  self.oauth_token
118
119
  end
119
120
 
121
+ # The SalesForce organization id for the authenticated user's Salesforce instance
122
+ def org_id
123
+ @org_id ||= query_org_id # lazy query org_id when not set by login response
124
+ end
125
+
120
126
  # Returns an Array of Strings listing the class names for every type of _Sobject_ in the database. Raises SalesForceError if an error occurs.
121
127
  def list_sobjects
122
128
  result = http_get("/services/data/v#{self.version}/sobjects")
@@ -327,7 +333,7 @@ module Databasedotcom
327
333
 
328
334
  def ensure_expected_response(expected_result_class)
329
335
  response = yield
330
-
336
+
331
337
  unless response.is_a?(expected_result_class || Net::HTTPSuccess)
332
338
  if response.is_a?(Net::HTTPUnauthorized)
333
339
  if self.refresh_token
@@ -349,13 +355,13 @@ module Databasedotcom
349
355
  end
350
356
 
351
357
  if response.is_a?(Net::HTTPSuccess)
352
- response = yield
358
+ response = yield
353
359
  end
354
360
  end
355
-
356
- raise SalesForceError.new(response) unless response.is_a?(expected_result_class || Net::HTTPSuccess)
361
+
362
+ raise SalesForceError.new(response) unless response.is_a?(expected_result_class || Net::HTTPSuccess)
357
363
  end
358
-
364
+
359
365
  response
360
366
  end
361
367
 
@@ -375,7 +381,7 @@ module Databasedotcom
375
381
  base_url = options[:host] ? "https://#{options[:host]}" : self.instance_url
376
382
  puts "***** REQUEST: #{path.include?(':') ? path : URI.join(base_url, path)}#{options[:data] ? " => #{options[:data]}" : ''}" if self.debugging
377
383
  end
378
-
384
+
379
385
  def uri_escape(str)
380
386
  URI.escape(str.to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
381
387
  end
@@ -469,12 +475,22 @@ module Databasedotcom
469
475
  def user_and_pass?(options)
470
476
  (self.username && self.password) || (options && options[:username] && options[:password])
471
477
  end
472
-
478
+
479
+ def parse_user_id_and_org_id_from_identity_url(identity_url)
480
+ m = identity_url.match(/\/id\/([^\/]+)\/([^\/]+)$/)
481
+ @org_id = m[1] rescue nil
482
+ @user_id = m[2] rescue nil
483
+ end
484
+
473
485
  def parse_auth_response(body)
474
486
  json = JSON.parse(body)
475
- @user_id = json["id"].match(/\/([^\/]+)$/)[1] rescue nil
487
+ parse_user_id_and_org_id_from_identity_url(json["id"])
476
488
  self.instance_url = json["instance_url"]
477
489
  self.oauth_token = json["access_token"]
478
490
  end
491
+
492
+ def query_org_id
493
+ query("select id from Organization")[0]["Id"]
494
+ end
479
495
  end
480
496
  end
@@ -1,3 +1,3 @@
1
1
  module Databasedotcom
2
- VERSION = "1.1.4"
2
+ VERSION = "1.1.5"
3
3
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: databasedotcom
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 1.1.4
5
+ version: 1.1.5
6
6
  platform: ruby
7
7
  authors:
8
8
  - Glenn Gillen, Danny Burkes & Richard Zhao
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-10-12 00:00:00 Z
13
+ date: 2011-10-17 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: multipart-post