Before we begin I have to bring up the fact that the Steelers did beat the Jets. I'm not on the same level of Nostradamus yet...but I'm getting close. When we left off at our last post we created four new Ruby files in our Salesforce directory. And as I previously mentioned we will be using code from Andrew (Super-Size) Interwebdiary to generate an easy login in function.
The following is the code for the extra file we will be using for the simple login:
client_auth_header_handler.rb
1 require 'soap/header/simplehandler'
2
3 class ClientAuthHeaderHandler < SOAP::Header::SimpleHandler
4 SessionHeader = XSD::QName.new("rn:enterprise.soap.sforce.com", "SessionHeader")
5
6 attr_accessor :sessionid
7 def initialize
8 super(SessionHeader)
9 @sessionid = nil
10 end
11
12 def on_simple_outbound
13 if @sessionid
14 {"sessionId" => @sessionid}
15 end
16 end
17
18 def on_simple_inbound(my_header, mustunderstand)
19 @sessionid = my_header["sessionid"]
20 end
21 end
22
For the final portion of code used in the login you will need to change the username, password, and security token to fit your specifications (changes needed are highlighted in red). This code will fetch records from the Account object and post raw output on the screen.
test.rb
1 require 'rubygems'
2 gem 'soap4r'
3 require 'soap/soap'
4 require 'defaultDriver'
5 require 'client_auth_header_handler'
6 require 'soap/wsdlDriver'
7
8
9 d = Soap.new
10 d.wiredump_dev = STDOUT
11
12 h = ClientAuthHeaderHandler.new
# Create a new handler
13
14 l = d.login(:username => "USERNAME", :password => "PASSWORD" + "SECURITY_TOKEN")
15
16 d.endpoint_url = l.result.serverUrl
# Change the endpoint to what login tells us it should be
17 h.sessionid = l.result.sessionId
# Tell the header handler what the session id is
18 d.headerhandler << h
# Add the header handler to the Array of headerhandlers
19
20 d.getUserInfo("")
21 d.query(:queryString=> "select id,name from account")
As always you are going to want to test the code. Do this by going to the Ruby console, then the 'sfdc' folder and input the following command:
ruby test.rb
If you run into errors you are going to have look at more API calls in the API docs....which kind of drives me crazy. But sometimes that what it takes when learning new languages.
Next post we will start exploring Ruby and RoR.