Class: ActiveRecord::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record/connection_adapters/activesalesforce_adapter.rb

Overview

Overrides the ActiveRecord::Base class to provide Salesforce Web Services services Particularly important are 1) getting a ‘binding’ and 2) ‘api_version’

Direct Known Subclasses

Salesforce::SfBase

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.



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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/active_record/connection_adapters/activesalesforce_adapter.rb', line 61

def self.activesalesforce_connection(config) # :nodoc:
  debug("\nUsing ActiveSalesforce connection\n")

  database_com_url = ENV["DATABASE_COM_URL"]
  raise "Set DATABASE_COM_URL" if database_com_url.nil?
  database_com_uri = URI.parse(database_com_url)

  # Default to production system using 20.0 API
  #url = config[:url]
  #url = database_com_uri.path
  url = "https://www.salesforce.com" #unless url

  #Take the API version from the Config file e.g. 'database.yml' -> 'salesforce-default-realm'
  api_version = config[:api_version] ? config[:api_version] : "20.0"
  uri = URI.parse(url)
  #uri.path = "/services/Soap/u/20.0"
  uri.path = "/services/Soap/u/" + (api_version).to_s
  url = uri.to_s

  sid = config[:sid]
  client_id = config[:client_id]
  #username = config[:username].to_s
  #password = config[:password].to_s
  username = URI.unescape(database_com_uri.user)
  password = URI.unescape(database_com_uri.password)

  # 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



52
53
54
# File 'lib/active_record/connection_adapters/activesalesforce_adapter.rb', line 52

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

.flush_connectionsObject



56
57
58
# File 'lib/active_record/connection_adapters/activesalesforce_adapter.rb', line 56

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