module Silverpops
Attributes
configuration[RW]
Public Class Methods
add_list_member(database_id, key_data, update_if_found = true, allow_html = true, send_auto_reply = true)
click to toggle source
# File lib/silverpops.rb, line 119 def self.add_list_member(database_id, key_data, update_if_found = true, allow_html = true, send_auto_reply = true) builder = Nokogiri::XML::Builder.new do |xml| xml.Envelope { xml.Body { xml.AddRecipient { xml.LIST_ID database_id xml.CREATED_FROM 1 xml.SEND_AUTOREPLY send_auto_reply xml.UPDATE_IF_FOUND update_if_found xml.ALLOW_HTML allow_html xml.CONTACT_LISTS '' key_data.each do |key, value| xml.COLUMN { xml.NAME key xml.VALUE value } end } } } end faraday = Faraday.new(:url => get_api_base_uri(), :proxy => Silverpops.configuration.proxy) do |faraday| faraday.request :url_encoded faraday.adapter Faraday.default_adapter end response = faraday.post do |req| req.url '/XMLAPI' req.headers["Content-Type"] = "text/xml" req.headers["Accept"] = "text/xml" req.body = builder.to_xml req.params['jsessionid'] = get_token() end doc = Nokogiri::XML(response.body) doc.xpath("//Envelope/Body/RESULT/SUCCESS").each do |node| return node.content.downcase == "true" end end
configure() { |configuration| ... }
click to toggle source
# File lib/silverpops.rb, line 168 def self.configure self.configuration ||= Configuration.new yield(configuration) end
find_list_member(database_id, email, fields = {})
click to toggle source
# File lib/silverpops.rb, line 67 def self.find_list_member(database_id, email, fields = {}) builder = Nokogiri::XML::Builder.new do |xml| xml.Envelope { xml.Body { xml.SelectRecipientData { xml.LIST_ID database_id xml.EMAIL email } } } end faraday = Faraday.new(:url => get_api_base_uri(), :proxy => Silverpops.configuration.proxy) do |faraday| faraday.request :url_encoded faraday.adapter Faraday.default_adapter end response = faraday.post do |req| req.url '/XMLAPI' req.headers["Content-Type"] = "text/xml" req.headers["Accept"] = "text/xml" req.body = builder.to_xml req.params['jsessionid'] = get_token() end doc = Nokogiri::XML(response.body) doc.xpath("//Envelope/Body/RESULT/SUCCESS").each do |node| if node.content.downcase == "true" record_data = {} doc.xpath("//Envelope/Body/RESULT").each do |data| record_data["Email"] = data.xpath("EMAIL")[0].content record_data["RecipientId"] = data.xpath("RecipientId")[0].content.to_i end doc.xpath("//Envelope/Body/RESULT/COLUMNS/COLUMN").each do |data| record_data[data.xpath("NAME")[0].content] = data.xpath("VALUE")[0].content end return record_data else return false end end end
get_api_base_uri()
click to toggle source
# File lib/silverpops.rb, line 12 def self.get_api_base_uri return 'http://api' + Silverpops.configuration.server_number.to_s + '.silverpop.com/XMLAPI' end
get_token(clear_cache = false)
click to toggle source
# File lib/silverpops.rb, line 16 def self.get_token(clear_cache = false) tries ||= 2 builder = Nokogiri::XML::Builder.new do |xml| xml.Envelope { xml.Body { xml.Login { xml.USERNAME Silverpops.configuration.username xml.PASSWORD Silverpops.configuration.password } } } end faraday = Faraday.new(:url => get_api_base_uri(), :proxy => Silverpops.configuration.proxy) do |faraday| faraday.request :url_encoded faraday.adapter Faraday.default_adapter end response = faraday.post do |req| req.url '/XMLAPI' req.headers["Content-Type"] = "text/xml" req.headers["Accept"] = "text/xml" req.body = builder.to_xml end if response.status == 200 doc = Nokogiri::XML(response.body) doc.xpath("//Envelope/Body/RESULT/SUCCESS").each do |node| doc.xpath("//Envelope/Body/RESULT/SESSIONID").each do |session| return session.content end end return false else raise RetryException, response.status.to_s + " " + response.body if response.status == 200 end rescue RetryException => e if (tries -= 1) > 0 retry else raise e end end