Class: ExactishTarget
- Inherits:
-
Object
- Object
- ExactishTarget
- Defined in:
- lib/exactish_target.rb
Instance Attribute Summary collapse
-
#end_point ⇒ Object
readonly
Returns the value of attribute end_point.
-
#password ⇒ Object
readonly
Returns the value of attribute password.
-
#username ⇒ Object
readonly
Returns the value of attribute username.
Instance Method Summary collapse
- #create_http ⇒ Object
- #create_request(body) ⇒ Object
- #create_subscriber(email, country, source) ⇒ Object
- #create_subscriber_request_xml(email, country, source) ⇒ Object
- #initialize(username, password, end_point) ⇒ ExactishTarget constructor
- #uri ⇒ Object
Constructor Details
#initialize(username, password, end_point) ⇒ ExactishTarget
13 14 15 16 17 |
# File 'lib/exactish_target.rb', line 13 def initialize(username, password, end_point) @username = username @password = password @end_point = end_point end |
Instance Attribute Details
#end_point ⇒ Object (readonly)
Returns the value of attribute end_point.
9 10 11 |
# File 'lib/exactish_target.rb', line 9 def end_point @end_point end |
#password ⇒ Object (readonly)
Returns the value of attribute password.
8 9 10 |
# File 'lib/exactish_target.rb', line 8 def password @password end |
#username ⇒ Object (readonly)
Returns the value of attribute username.
7 8 9 |
# File 'lib/exactish_target.rb', line 7 def username @username end |
Instance Method Details
#create_http ⇒ Object
23 24 25 26 27 28 29 30 |
# File 'lib/exactish_target.rb', line 23 def create_http http = Net::HTTP.new(uri.host, uri.port) http.open_timeout = 3 # in seconds http.read_timeout = 3 # in seconds http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE http end |
#create_request(body) ⇒ Object
32 33 34 35 36 37 38 |
# File 'lib/exactish_target.rb', line 32 def create_request(body) request = Net::HTTP::Post.new(uri.request_uri) request.add_field "SOAPAction", "Create" request.add_field "Content-Type", "text/xml; charset=utf-8" request.body = body request end |
#create_subscriber(email, country, source) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/exactish_target.rb', line 40 def create_subscriber(email, country, source) http = create_http request = create_request(create_subscriber_request_xml(email, country, source)) begin response = http.request(request) status_code = $1 if response.body =~ %r{<StatusCode>\s*([^<]+)\s*</StatusCode>} unless status_code raise StandardError, "Invalid response from Exact Target\n#{response.body}" else unless status_code.downcase == "ok" = $1 if response.body =~ %r{<StatusMessage>\s*([^<]+)\s*</StatusMessage>} raise StandardError, "Exact Target Error: #{ || "Unknown error"}" end end rescue Exception => e raise StandardError, "Subscription request for email: \"#{email}\", country: \"#{country}\", source: \"#{source}\" failed. Newsletter subscription has not been created.\nException: #{e.inspect}" end end |
#create_subscriber_request_xml(email, country, source) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/exactish_target.rb', line 59 def create_subscriber_request_xml(email, country, source) <<-XML.strip <?xml version="1.0" encoding="utf-8" ?> <env:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"> <env:Header><n1:Security env:mustUnderstand="0" xmlns:n1="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"><n1:UsernameToken><n1:Username>#{username}</n1:Username><n1:Password>#{password}</n1:Password></n1:UsernameToken></n1:Security></env:Header> <env:Body> <n2:CreateRequest xmlns:n2="http://exacttarget.com/wsdl/partnerAPI"> <n2:Options xsi:type="n2:CreateOptions" xsi:nil="true"></n2:Options> <n2:Objects xsi:type="n2:Subscriber"> <n2:EmailAddress>#{email.to_xs}</n2:EmailAddress> <n2:CustomerKey>#{email.to_xs}</n2:CustomerKey> <n2:Attributes><n2:Name>Country</n2:Name><n2:Value>#{country.to_xs}</n2:Value></n2:Attributes> <n2:Attributes><n2:Name>Source</n2:Name><n2:Value>#{source.to_xs}</n2:Value></n2:Attributes> <n2:Attributes><n2:Name>Lonely Planet Newsletters</n2:Name><n2:Value>true</n2:Value></n2:Attributes> <n2:Attributes><n2:Name>Lonely Planet Marketing</n2:Name><n2:Value>true</n2:Value></n2:Attributes> </n2:Objects> </n2:CreateRequest> </env:Body> </env:Envelope> XML end |
#uri ⇒ Object
19 20 21 |
# File 'lib/exactish_target.rb', line 19 def uri @uri ||= URI.parse(end_point) end |