class Salesforce::Einstein::Base
Constants
- BASE_URI
Attributes
access_token[RW]
boundary[RW]
email[RW]
private_key[RW]
timeout[RW]
Public Class Methods
new(cert: nil, private_key: nil, password: nil, email:, timeout: 3600)
click to toggle source
# File lib/salesforce/einstein/base.rb, line 15 def initialize(cert: nil, private_key: nil, password: nil, email:, timeout: 3600) if cert.nil? && private_key.nil? raise ArgumentError, 'At least one parameter must be specified: cert or private_key' end if cert cert_contents = File.read(File.expand_path(cert)) pkcs12 = OpenSSL::PKCS12.new(cert_contents, password) @private_key = pkcs12.key else private_key_contents = File.read(File.expand_path(private_key)) @private_key = OpenSSL::PKey::RSA.new(private_key_contents, password) end @email = email @boundary = SecureRandom.hex(10) @timeout = 3600 end
Public Instance Methods
delete_reflesh_token(token)
click to toggle source
# File lib/salesforce/einstein/base.rb, line 42 def delete_reflesh_token(token) delete "/oauth2/token/#{token}" end
get_api_usage()
click to toggle source
# File lib/salesforce/einstein/base.rb, line 38 def get_api_usage get '/apiusage' end
Private Instance Methods
client()
click to toggle source
# File lib/salesforce/einstein/base.rb, line 48 def client @client ||= Faraday.new do |conn| conn.request :multipart conn.request :url_encoded conn.adapter :net_http end end
delete(path)
click to toggle source
# File lib/salesforce/einstein/base.rb, line 74 def delete(path) response = client.delete do |req| req.url "#{BASE_URI}#{path}" req.headers['Content-Type'] = 'multipart/form-data' req.headers['Authorization'] = "Bearer #{access_token}" end response ? JSON.parse(response.body) : nil end
get(path)
click to toggle source
# File lib/salesforce/einstein/base.rb, line 56 def get(path) response = client.get do |req| req.url "#{BASE_URI}#{path}" req.headers['Authorization'] = "Bearer #{access_token}" end response ? JSON.parse(response.body) : nil end
get_access_token()
click to toggle source
# File lib/salesforce/einstein/base.rb, line 83 def get_access_token jwt = JWT.encode({ iss: 'developer.force.com', sub: email, aud: "#{BASE_URI}/oauth2/token", iat: Time.now.to_i, exp: Time.now.to_i + timeout }, private_key, 'RS256') response = client.post do |req| req.url "#{BASE_URI}/oauth2/token" req.headers['Content-Type'] = 'application/x-www-form-urlencoded' req.body = { grant_type: 'urn:ietf:params:oauth:grant-type:jwt-bearer', assertion: jwt } end response ? JSON.parse(response.body) : nil end
post(path, params)
click to toggle source
# File lib/salesforce/einstein/base.rb, line 64 def post(path, params) response = client.post do |req| req.url "#{BASE_URI}#{path}" req.headers['Content-Type'] = 'multipart/form-data' req.headers['Authorization'] = "Bearer #{access_token}" req.body = params end response ? JSON.parse(response.body) : nil end