Class: ExactTarget
- Inherits:
-
Object
- Object
- ExactTarget
- Defined in:
- lib/exacttarget.rb,
lib/exacttarget/job.rb,
lib/exacttarget/list.rb,
lib/exacttarget/group.rb,
lib/exacttarget/image.rb,
lib/exacttarget/email.rb
Overview
Ruby wrapper for the ExactTarget XML API.
Constant Summary
- 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)
-
- (hash) :config
readonly
Configuration hash.
-
- (Object) config
readonly
Returns the value of attribute config.
Instance Method Summary (collapse)
-
- (Object) email_create(name, subject, html, text = false)
Create an email.
-
- (Object) email_find(options = {})
Find all emails who's attributes match the selected options.
-
- (Object) email_find_all(body = false)
Find all emails.
-
- (Object) email_find_by_id(id, options = {})
Retrieve an email by its ID.
-
- (Object) email_find_by_name(name, options = {})
Find all emails who's name includes the given keyword.
-
- (Object) email_find_by_subject(subject, options = {})
Find all emails who's subject includes the given keyword.
-
- (Object) group_find(options = {})
(also: #group_find_all)
Find all groups who's attributes match the selected options.
-
- (Object) group_find_by_desc(desc, options = {})
Retrieve a group by its description.
-
- (Object) group_find_by_id(id, options = {})
Retrieve a group by its ID.
-
- (Object) group_find_by_name(name, options = {})
Retrieve a group by its name.
-
- (Object) image_import(file_path)
Upload an image (note: uses the FTP and cleans up afterward).
-
- (ExactTarget) initialize(config)
constructor
Create a new ExactTarget API client.
-
- (Object) job_send(options)
(also: #email_send)
Send an email to a collection of lists or groups.
-
- (Object) list_find(options = {})
(also: #list_find_all)
Find all lists who's attributes match the selected options.
-
- (Object) list_find_by_id(id, options = {})
Retrieve a list by its ID.
-
- (Object) list_find_by_name(name, options = {})
Find all lists who's name includes the given keyword.
-
- (Object) list_find_by_type(type, options = {})
Find all lists who's type includes the given keyword.
Constructor Details
- (ExactTarget) initialize(config)
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 97 |
# 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 # Configure/start services: ftp_connect @uri = URI.parse(@config[:api_uri]) @api = Net::HTTP.new(@uri.host, @uri.port) @api.use_ssl = true end |
Instance Attribute Details
- (hash) :config (readonly)
Configuration hash
22 23 24 |
# File 'lib/exacttarget.rb', line 22
def :config
@:config
end
|
- (Object) config (readonly)
Returns the value of attribute config
45 46 47 |
# File 'lib/exacttarget.rb', line 45 def config @config end |
Instance Method Details
- (Object) email_create(name, subject, html, text = false)
Create an email.
Note: The text version is generated automatically by ExactTarget.
Just specify one here to overwrite the default.
131 132 133 134 135 |
# File 'lib/exacttarget/email.rb', line 131 def email_create(name, subject, html, text = false) id = email_add_html(name, subject, html) email_add_text(id, text) if text id end |
- (Object) email_find(options = {})
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 |
- (Object) email_find_all(body = false)
Find all emails.
9 10 11 |
# File 'lib/exacttarget/email.rb', line 9 def email_find_all(body = false) email_find({ :body => body }) end |
- (Object) email_find_by_id(id, options = {})
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 |
- (Object) email_find_by_name(name, options = {})
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 |
- (Object) email_find_by_subject(subject, options = {})
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 |
- (Object) group_find(options = {}) 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 |
- (Object) group_find_by_desc(desc, options = {})
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 |
- (Object) group_find_by_id(id, options = {})
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 |
- (Object) group_find_by_name(name, options = {})
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 |
- (Object) image_import(file_path)
Upload an image (note: uses the FTP and cleans up afterward).
Supported file types:
bmp, gif, jpeg, jpg, png, tif, tiff
Note: Files must be under 200KB.
--- DON'T PANIC ---
This function may spit out FTP deletion failures.
It seems that ExactTarget locks the image file while
it is moving it (the API call). However, it should
delete the file within the retry limit (15).
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 |
# File 'lib/exacttarget/image.rb', line 20 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 |
- (Object) job_send(options) Also known as: email_send
Send an email to a collection of lists or groups.
This function takes its name from the API call it makes,
but it is aliased as "email_send." And for good reason,
as that accurately describes its true function.
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 |
# File 'lib/exacttarget/job.rb', line 37 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 |
- (Object) list_find(options = {}) 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 |
- (Object) list_find_by_id(id, options = {})
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 |
- (Object) list_find_by_name(name, options = {})
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 |
- (Object) list_find_by_type(type, options = {})
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 |