lib/databasedotcom/client.rb in databasedotcom-1.0.1 vs lib/databasedotcom/client.rb in databasedotcom-1.0.2
- old
+ new
@@ -82,14 +82,16 @@
# * If _options_ contains the key <tt>:provider</tt>, it is assumed to be the hash returned by Omniauth from a successful web-based OAuth2 authentication
# * If _options_ contains the keys <tt>:token</tt> and <tt>:instance_url</tt>, those are assumed to be a valid OAuth2 token and instance URL for a Salesforce account, obtained from an external source
#
# Raises SalesForceError if an error occurs
def authenticate(options = nil)
- if (options[:username] && options[:password])
+ if user_and_pass?(options)
req = Net::HTTP.new(self.host, 443)
req.use_ssl=true
- path = "/services/oauth2/token?grant_type=password&client_id=#{self.client_id}&client_secret=#{client_secret}&username=#{options[:username]}&password=#{options[:password]}"
+ user = self.username || options[:username]
+ pass = self.password || options[:password]
+ path = "/services/oauth2/token?grant_type=password&client_id=#{self.client_id}&client_secret=#{client_secret}&username=#{user}&password=#{pass}"
log_request("https://#{self.host}/#{path}")
result = req.post(path, "")
log_response(result)
raise SalesForceError.new(result) unless result.is_a?(Net::HTTPOK)
json = JSON.parse(result.body)
@@ -414,8 +416,12 @@
end
end
def key_from_label(label)
label.gsub(' ', '_')
+ end
+
+ def user_and_pass?(options)
+ (self.username && self.password) || (options && options[:username] && options[:password])
end
end
end