-
Notifications
You must be signed in to change notification settings - Fork 0
/
zendesk-widgets.rb
162 lines (132 loc) · 3.85 KB
/
zendesk-widgets.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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
##############
#
#Connect to the SP API to return user information
#Zendesk Widget
#
############
require 'nokogiri'
require 'rest-client'
require 'sinatra'
configure do
#allows app to be embeded into iframes for use with Zendesk
set :protection, :except => :frame_options
end
get '/' do
erb :explanation
end
#Form to check email address subscription status
get '/lookup/?' do
erb :lookup_form
end
#Form to unsubscribe user
get '/unsubscribe/?' do
erb :unsubscribe_form
end
#lookups email address in silverpop
post '/lookup/output/?' do
#Silverpop class. Need to get a sessionid before anything else happens
class Silverpop
require 'nokogiri'
require 'rest-client'
attr_accessor :sessionid, :username, :password
def silverpop_request(xml)
return RestClient.get "http://api4.silverpop.com/XMLAPI;jsessionid=" + @sessionid.to_s + "?xml=" + URI::encode(xml.to_xml)
end
def initialize(username, password)
@username = username
@password = password
#set sessionid block
jsession_builder = Nokogiri::XML::Builder.new do
Envelope {
Body {
Login {
USERNAME username
PASSWORD password
}
}
}
end
response = silverpop_request(jsession_builder)
#parse response for session id and set as objects' @sessionid
doc = Nokogiri::XML response
@sessionid = doc.at_xpath('//SESSIONID').content
end
def get_user(email, listid = ENV['SILVERPOP_MAIN_LISTID'])
builder = Nokogiri::XML::Builder.new do
Envelope {
Body {
SelectRecipientData {
LIST_ID listid
EMAIL email
}
}
}
end
response = silverpop_request(builder)
return response.body
end
end
silverpop = Silverpop.new(ENV['SILVERPOP_USERNAME'], ENV['SILVERPOP_PASSWORD'])
#Shows information for the user
user_xml = silverpop.get_user(params[:email])
@output = Nokogiri::XML user_xml
if (@output.at_xpath('//SUCCESS').inner_text == 'false')
erb :lookup_not_found
else
erb :lookup_found
end
end
#Unsubscribe user
post '/unsubscribe/output/?' do
#Silverpop class. Need to get a sessionid before anything else happens
class Silverpop
require 'nokogiri'
require 'rest-client'
attr_accessor :sessionid, :username, :password
def silverpop_request(xml)
return RestClient.get "http://api4.silverpop.com/XMLAPI;jsessionid=" + @sessionid.to_s + "?xml=" + URI::encode(xml.to_xml)
end
def initialize(username, password)
@username = username
@password = password
#set sessionid block
jsession_builder = Nokogiri::XML::Builder.new do
Envelope {
Body {
Login {
USERNAME username
PASSWORD password
}
}
}
end
response = silverpop_request(jsession_builder)
#parse response for session id and set as objects' @sessionid
doc = Nokogiri::XML response
@sessionid = doc.at_xpath('//SESSIONID').content
end
def unsubscribe_user(email, listid = ENV['SILVERPOP_MAIN_LISTID'])
builder = Nokogiri::XML::Builder.new do
Envelope {
Body {
OptOutRecipient {
LIST_ID listid
EMAIL email
}
}
}
end
response = silverpop_request(builder)
return response.body
end
end
silverpop = Silverpop.new(ENV['SILVERPOP_USERNAME'], ENV['SILVERPOP_PASSWORD'])
#Shows information for the user
user_xml = silverpop.unsubscribe_user(params[:email])
@output = Nokogiri::XML user_xml
if (@output.at_xpath('//SUCCESS').inner_text == 'false')
erb :unsubscribe_not_found
else
erb :unsubscribe_found
end
end