-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathruby-silverpop-connect.rb
58 lines (47 loc) · 1.75 KB
/
ruby-silverpop-connect.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
##############
#
#Connecting to the SP XML API
#Create a new Silverpop object with your login information
#
############
require 'rest-client'
require 'nokogiri'
require 'uri'
#Silverpop class. Initializes and finds the jsessionid, which is needed for everything else.
class Silverpop
require 'nokogiri'
require 'rest-client'
attr_accessor :sessionid, :username, :password, :endpoint
def initialize(username, password, endpoint)
@username = username
@password = password
@endpoint = endpoint
#set sessionid block
jsession_builder = Nokogiri::XML::Builder.new do
Envelope {
Body {
Login {
USERNAME username
PASSWORD password
}
}
}
end
response = RestClient.get(ENV['SILVERPOP_ENDPOINT'] + "?xml=" + URI::encode(jsession_builder.to_xml))
#parse response for sessionid (or error) and set as objects' @sessionid
doc = Nokogiri::XML(response)
#If successful sets sessionid appropriately. If not, exits with errorid and fault string
if (doc.at_xpath('//SESSIONID'))
@sessionid = doc.at_xpath('//SESSIONID').inner_text
else
puts "Silverpop Errorid: " + doc.at_xpath('//errorid').inner_text + ". " + doc.at_xpath('//FaultString').inner_text
end
end
#Post a well-formatted bit of xml to Silverpop
def silverpop_request_post(xml)
url = ENV['SILVERPOP_ENDPOINT'] + ";jsessionid=#{@sessionid.to_s}"
return RestClient.post(url, xml.to_xml, :Content_type => "text/xml", :Content_length => xml.to_xml.length)
end
end
#New object with authentication. Find your endpoint in the SP api documentation.
silverpop = Silverpop.new(ENV['SILVERPOP_USERNAME'], ENV['SILVERPOP_PASSWORD'], ENV['SILVERPOP_ENDPOINT'])