Class: ActiveRecord::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/asf_adapter.rb

Constant Summary collapse

@@cache =
{}

Class Method Summary collapse

Class Method Details

.activesalesforce_connection(config) ⇒ Object

Establishes a connection to the database that’s used by all Active Record objects.

Raises:

  • (ActiveSalesforce::ASFError)


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/asf_adapter.rb', line 49

def self.activesalesforce_connection(config) # :nodoc:
  debug("\nUsing ActiveSalesforce connection\n")
  
  # Default to production system using 8.0 API
  url = config[:url]
  url = "https://www.salesforce.com" unless url

  uri = URI.parse(url)
  uri.path = "/services/Soap/u/8.0"
  url = uri.to_s      
  
  sid = config[:sid]
  client_id = config[:client_id]
  username = config[:username].to_s
  password = config[:password].to_s
  
  # Recording/playback support      
  recording_source = config[:recording_source]
  recording = config[:recording]
  
  if recording_source
    recording_source = File.open(recording_source, recording ? "w" : "r")
    binding = ActiveSalesforce::RecordingBinding.new(url, nil, recording != nil, recording_source, logger)
    binding.client_id = client_id if client_id
    binding.(username, password) unless sid
  end

  # Check to insure that the second to last path component is a 'u' for Partner API
  raise ActiveSalesforce::ASFError.new(logger, "Invalid salesforce server url '#{url}', must be a valid Parter API URL") unless url.match(/\/u\//mi)
  
  if sid
    binding = @@cache["sid=#{sid}"] unless binding
    
    unless binding
      debug("Establishing new connection for [sid='#{sid}']")
      
      binding = RForce::Binding.new(url, sid)
      @@cache["sid=#{sid}"] = binding
      
      debug("Created new connection for [sid='#{sid}']")
    else
      debug("Reused existing connection for [sid='#{sid}']")
    end
  else
    binding = @@cache["#{url}.#{username}.#{password}.#{client_id}"] unless binding
    
    unless binding
      debug("Establishing new connection for ['#{url}', '#{username}, '#{client_id}'")
      
      seconds = Benchmark.realtime {
        binding = RForce::Binding.new(url, sid)
        binding.(username, password)
        
        @@cache["#{url}.#{username}.#{password}.#{client_id}"] = binding
      }
      
      debug("Created new connection for ['#{url}', '#{username}', '#{client_id}'] in #{seconds} seconds")
    end
  end

  ConnectionAdapters::SalesforceAdapter.new(binding, logger, config)

end

.debug(msg) ⇒ Object



40
41
42
# File 'lib/asf_adapter.rb', line 40

def self.debug(msg)
  logger.debug(msg) if logger
end

.flush_connectionsObject



44
45
46
# File 'lib/asf_adapter.rb', line 44

def self.flush_connections()
  @@cache = {}
end