Class: ExactTarget
- Inherits:
-
Object
- Object
- ExactTarget
- Defined in:
- lib/exacttarget.rb,
lib/exacttarget/job.rb,
lib/exacttarget/list.rb,
lib/exacttarget/email.rb,
lib/exacttarget/group.rb,
lib/exacttarget/image.rb
Overview
ExactTarget XML API wrapper.
Constant Summary collapse
- MSG =
Error/warning message header:
'[ExactTarget]'
- ERROR =
For error messages:
"#{MSG} Error:"
- WARN =
For warning messages:
"#{MSG} Warning:"
- FTP_STANDARD_NAME =
The standard FTP name:
'ExactTargetFTP'
- FTP_STANDARD_URI =
The standard FTP URI:
'ftp.exacttarget.com'
- FTP_STANDARD_PATH =
The standard FTP path (directory):
'/'
- FTP_ENHANCED_NAME =
The enhanced FTP name:
'ExactTargetEnhancedFTP'
- FTP_ENHANCED_URI =
The enhanced FTP URI:
'ftp1.exacttarget.com'
- FTP_ENHANCED_PATH =
The enhanced FTP path (directory):
'/import'
Instance Attribute Summary collapse
-
#:config ⇒ hash
readonly
Configuration hash.
-
#config ⇒ Object
readonly
Returns the value of attribute config.
Instance Method Summary collapse
-
#email_create(name, subject, html, text = false) ⇒ Object
Create an email.
-
#email_find(options = {}) ⇒ Object
Find all emails who’s attributes match the selected options.
-
#email_find_all(body = false) ⇒ Object
Find all emails.
-
#email_find_by_id(id, options = {}) ⇒ Object
Retrieve an email by its ID.
-
#email_find_by_name(name, options = {}) ⇒ Object
Find all emails who’s name includes the given keyword.
-
#email_find_by_subject(subject, options = {}) ⇒ Object
Find all emails who’s subject includes the given keyword.
-
#group_find(options = {}) ⇒ Object
(also: #group_find_all)
Find all groups who’s attributes match the selected options.
-
#group_find_by_desc(desc, options = {}) ⇒ Object
Retrieve a group by its description.
-
#group_find_by_id(id, options = {}) ⇒ Object
Retrieve a group by its ID.
-
#group_find_by_name(name, options = {}) ⇒ Object
Retrieve a group by its name.
-
#image_import(file_path) ⇒ Object
Upload an image (note: uses the FTP and cleans up afterward).
-
#initialize(config) ⇒ ExactTarget
constructor
Create a new ExactTarget API client.
-
#job_send(options) ⇒ Object
(also: #email_send)
Send an email to a collection of lists or groups.
-
#list_find(options = {}) ⇒ Object
(also: #list_find_all)
Find all lists who’s attributes match the selected options.
-
#list_find_by_id(id, options = {}) ⇒ Object
Retrieve a list by its ID.
-
#list_find_by_name(name, options = {}) ⇒ Object
Find all lists who’s name includes the given keyword.
-
#list_find_by_type(type, options = {}) ⇒ Object
Find all lists who’s type includes the given keyword.
Constructor Details
#initialize(config) ⇒ ExactTarget
Create a new ExactTarget API client.
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/exacttarget.rb', line 74 def initialize(config) @config = { :username => nil, :password => nil, :api_uri => 'https://api.dc1.exacttarget.com/integrate.asp', :ftp_username => 'import', :ftp_password => 'import', :ftp_name => FTP_STANDARD_NAME, :ftp_uri => FTP_STANDARD_URI, :ftp_path => FTP_STANDARD_PATH }.merge(config) # Sanity check: if @config[:username].nil? || @config[:password].nil? raise "#{ERROR} username and password required!" end ftp_connect @uri = URI.parse(@config[:api_uri]) @api = Net::HTTP.new(@uri.host, @uri.port) @api.use_ssl = true end |
Instance Attribute Details
#:config ⇒ hash (readonly)
Configuration hash
22 23 24 |
# File 'lib/exacttarget.rb', line 22
def :config
@:config
end
|
#config ⇒ Object (readonly)
Returns the value of attribute config.
45 46 47 |
# File 'lib/exacttarget.rb', line 45 def config @config end |
Instance Method Details
#email_create(name, subject, html, text = false) ⇒ Object
Create an email.
116 117 118 119 120 |
# File 'lib/exacttarget/email.rb', line 116 def email_create(name, subject, html, text = false) id = email_add_html(name, subject, html) email_add_text(id, text) if text id end |
#email_find(options = {}) ⇒ Object
Find all emails who’s attributes match the selected options.
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 |
# File 'lib/exacttarget/email.rb', line 60 def email_find( = {}) @action = 'retrieve' @sub_action = 'all' @type = '' @value = '' id = [:id] || false name = [:name] || false subject = [:subject] || false start_date = [:start] || false end_date = [:end] || false get_body = [:body] || false list = [] Nokogiri::Slop(send(render(:email))) .exacttarget .system .email .emaillist.each do |email| (next if email.emailid.content != id.to_s) if id (next if !email.emailname.content.include? name.to_s) if name (next if !email.emailsubject.content.include? subject.to_s) if subject date = Date.strptime(email.emailcreateddate.content, '%m/%d/%Y') (next if date < start_date) if start_date && start_date.instance_of?(Date) (next if date > end_date) if end_date && end_date.instance_of?(Date) body = email_get_body(email.emailid.content) if get_body email.instance_eval do new = { :id => emailid.content, :name => emailname.content, :subject => emailsubject.content, :date => date, :category_id => categoryid.content } new[:body] = body if get_body list << new end end list end |
#email_find_all(body = false) ⇒ Object
Find all emails.
9 10 11 |
# File 'lib/exacttarget/email.rb', line 9 def email_find_all(body = false) email_find({ :body => body }) end |
#email_find_by_id(id, options = {}) ⇒ Object
Retrieve an email by its ID.
19 20 21 22 23 24 |
# File 'lib/exacttarget/email.rb', line 19 def email_find_by_id(id, = {}) email_find({ :id => id, :body => true }.merge()) end |
#email_find_by_name(name, options = {}) ⇒ Object
Find all emails who’s name includes the given keyword.
32 33 34 35 36 |
# File 'lib/exacttarget/email.rb', line 32 def email_find_by_name(name, = {}) email_find({ :name => name, }.merge()) end |
#email_find_by_subject(subject, options = {}) ⇒ Object
Find all emails who’s subject includes the given keyword.
44 45 46 47 48 |
# File 'lib/exacttarget/email.rb', line 44 def email_find_by_subject(subject, = {}) email_find({ :subject => subject, }.merge()) end |
#group_find(options = {}) ⇒ Object Also known as: group_find_all
Find all groups who’s attributes match the selected options.
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 |
# File 'lib/exacttarget/group.rb', line 48 def group_find( = {}) id = [:id] || false name = [:name] || false desc = [:desc] || false groups = [] Nokogiri::Slop(send(render(:group))) .exacttarget .system .list .groups.each do |group| (next if group.groupID.content != id.to_s) if id (next if !group.groupName.content.include? name.to_s) if name (next if !group.description.content.include? desc.to_s) if desc group.instance_eval do groups << { :id => groupID.content, :name => groupName.content, :desc => description.content } end end groups end |
#group_find_by_desc(desc, options = {}) ⇒ Object
Retrieve a group by its description.
35 36 37 38 39 |
# File 'lib/exacttarget/group.rb', line 35 def group_find_by_desc(desc, = {}) group_find({ :desc => desc }.merge()) end |
#group_find_by_id(id, options = {}) ⇒ Object
Retrieve a group by its ID.
11 12 13 14 15 |
# File 'lib/exacttarget/group.rb', line 11 def group_find_by_id(id, = {}) group_find({ :id => id }.merge()) end |
#group_find_by_name(name, options = {}) ⇒ Object
Retrieve a group by its name.
23 24 25 26 27 |
# File 'lib/exacttarget/group.rb', line 23 def group_find_by_name(name, = {}) group_find({ :name => name }.merge()) end |
#image_import(file_path) ⇒ Object
Upload an image (note: uses the FTP and cleans up afterward).
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 |
# File 'lib/exacttarget/image.rb', line 9 def image_import(file_path) @name = File.basename(file_path) ftp_put(file_path.to_s) result = Nokogiri::XML(send(render(:image))) desc = result.xpath('//filemanagement-description').text total = result.xpath('//filemanagement-totalmoved').text.to_i name = result.xpath('//filemanagement-info').text error = desc.include?('not exist') ? 'File not found' : total < 1 ? 'Unsupported file type' : nil count = 0 limit = 15 begin sleep 0.5 ftp_delete(@name) rescue count += 1 retry if count < limit end { :file_path => file_path.to_s, :old_name => @name, :new_name => name, :error => error } end |
#job_send(options) ⇒ Object Also known as: email_send
Send an email to a collection of lists or groups.
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 |
# File 'lib/exacttarget/job.rb', line 19 def job_send() @options = { :id => nil, :include => [], :exclude => [], :from_name => nil, :from_email => nil, :additional => nil, :when => nil, :multipart => false, :track => true, :test => false }.merge() # Sanity check: if @options[:id].nil? || @options[:include].empty? raise "#{ERROR} id and include array/string required!" end @date = (@options[:when].strftime('%-m/%-d/%Y') if @options[:when].instance_of?(Date) || @options[:when].instance_of?(DateTime)) || 'immediate' @time = (@options[:when].strftime('%H:%M') if @options[:when].instance_of?(DateTime)) || '' result = Nokogiri::XML(send(render(:job))) info = result.xpath('//job_info').text desc = result.xpath('//job_description').text raise "#{ERROR} job send failed !" if !info.include? 'success' desc end |
#list_find(options = {}) ⇒ Object Also known as: list_find_all
Find all lists who’s attributes match the selected options.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/exacttarget/list.rb', line 50 def list_find( = {}) id = [:id] || false name = [:name] || false type = [:type] || false start_date = [:start] || false end_date = [:end] || false list = list_get_all list.select do |item| (next if item[:id] != id.to_s) if id (next if !item[:name].include? name.to_s) if name (next if !item[:type].include? type.to_s) if type (next if item[:modified] < start_date) if start_date && start_date.instance_of?(DateTime) (next if item[:modified] > end_date) if end_date && end_date.instance_of?(DateTime) true end end |
#list_find_by_id(id, options = {}) ⇒ Object
Retrieve a list by its ID.
11 12 13 14 15 |
# File 'lib/exacttarget/list.rb', line 11 def list_find_by_id(id, = {}) list_find({ :id => id }.merge()) end |
#list_find_by_name(name, options = {}) ⇒ Object
Find all lists who’s name includes the given keyword.
23 24 25 26 27 |
# File 'lib/exacttarget/list.rb', line 23 def list_find_by_name(name, = {}) list_find({ :name => name, }.merge()) end |
#list_find_by_type(type, options = {}) ⇒ Object
Find all lists who’s type includes the given keyword.
35 36 37 38 39 |
# File 'lib/exacttarget/list.rb', line 35 def list_find_by_type(type, = {}) list_find({ :type => subject, }.merge()) end |