module Cupid class Session def retrieve_email_folders(account=nil, properties=nil) account ||= @account properties ||= ['ID', 'Name', 'ParentFolder.ID', 'ParentFolder.Name'] filters = '' + 'ContentType' + 'like' + 'email' + '' soap_body = build_retrieve(account.to_s, 'DataFolder', properties, filters) response = build_request('Retrieve', 'RetrieveRequestMsg', soap_body) response = Nokogiri::XML(response.http.body).remove_namespaces! all_folders = response.css('Results').map{|f| {f.css('Name').to_a.map(&:text).join('/') => f.css('ID')[0].text}} end def retrieve_email_copies(name, account=nil, properties=nil) account ||= @account properties ||= ['ID', 'Name'] filters = '' + 'Name' + 'like' + '' + name + '' + '' soap_body = build_retrieve(account.to_s, 'Email', properties, filters) response = build_request('Retrieve', 'RetrieveRequestMsg', soap_body) response = Nokogiri::XML(response.http.body).remove_namespaces! all_copies = response.css('Results').map{|f| {f.css('Name').to_a.map(&:text).join('/') => f.css('ID')[0].text}} end def retrieve_emails_from_folder(folder, account=nil, properties=nil) account ||= @account properties ||= ['ID', 'Name'] filters = '' + 'CategoryID' + 'equals' + '' + folder + '' + '' soap_body = build_retrieve(account.to_s, 'Email', properties, filters) response = build_request('Retrieve', 'RetrieveRequestMsg', soap_body) response = Nokogiri::XML(response.http.body).remove_namespaces! all_copies = response.css('Results').map{|f| {f.css('Name').to_a.map(&:text).join('/') => f.css('ID')[0].text}} end def create_email(subject, body, *args) soap_body = prepare_email(subject, body, args) response = build_request('Create', 'CreateRequest', soap_body) response = Nokogiri::XML(response.http.body).remove_namespaces! created_email_id = response.css('NewID').text end def create_folder(title, *args) soap_body = prepare_folder(title, args) response = build_request('Create', 'CreateRequest', soap_body) response = Nokogiri::XML(response.http.body).remove_namespaces! created_folder_id = response.css('NewID').text end def send_email_to_list(email_id, list_id, account=nil) soap_body = prepare_send_object(email_id, list_id, account) response = build_request('Create', 'CreateRequest', soap_body) response = Nokogiri::XML(response.http.body).remove_namespaces! created_send_id = response.css('NewID').text end def create_emails(subject_bodies, *args) soap_body = '' subject_bodies.each{ |subject, body| soap_body += prepare_email(subject, body, args) } response = build_request('Create', 'CreateRequest', soap_body) response = Nokogiri::XML(response.http.body).remove_namespaces! response.css('Results').map{ |f| f.css('NewID').text } end def create_folders(titles, *args) soap_body = titles.map{ |title| prepare_folder(title, args) }.join('') response = build_request('Create', 'CreateRequest', soap_body) response = Nokogiri::XML(response.http.body).remove_namespaces! response.css('Results').map{ |f| f.css('NewID').text } end def send_emails_to_lists(emails_lists, account=nil) soap_body = '' emails_lists.each{ |email_id, list_id| soap_body += prepare_send(email_id, list_id, account) } response = build_request('Create', 'CreateRequest', soap_body) response = Nokogiri::XML(response.http.body).remove_namespaces! response.css('Results').map{ |f| f.css('NewID').text } end def delete_email(email_id, account=nil) soap_body = prepare_delete_email(email_id, account) response = build_request('Delete', 'DeleteRequest', soap_body) response = Nokogiri::XML(response.http.body).remove_namespaces! response.css('Results').css('StatusCode').text == 'OK' end def delete_emails(emails, account=nil) soap_body = emails.map{ |email_id| prepare_delete_email(email_id, account) }.join('') response = build_request('Delete', 'DeleteRequest', soap_body) response = Nokogiri::XML(response.http.body).remove_namespaces! response.css('Results').map{ |f| f.css('StatusCode').text == 'OK' } end private def prepare_email(subject, body, args) options = args.extract_options! options[:subject] = CGI.escapeHTML subject.to_s options[:body] = CGI.escapeHTML body.to_s options[:client_id] ||= @account options[:email_type] ||= 'HTML' options[:is_html_paste] ||= 'true' # ?? options[:character_set] ||= 'utf-8' create_email_object(options) end def create_email_object(options) email_object = '' email_object += '' + options[:client_id].to_s + '' if options[:client_id] email_object += '' + options[:category_id].to_s + '' if options[:category_id] email_object += '' + options[:name].to_s + '' if options[:name] email_object += '' + options[:description].to_s + '' if options[:description] email_object += '' + options[:subject] + '' + '' + options[:body] + '' + '' + options[:email_type] + '' + '' + options[:is_html_paste] + '' + '' + options[:character_set] + '' end def prepare_folder(title, args) options = args.extract_options! options[:title] = CGI.escapeHTML title.to_s options[:description] ||= 'description' options[:client_id] ||= @account options[:content_type] ||= 'email' options[:is_active] ||= 'true' options[:is_editable] ||= 'true' options[:allow_children] ||= 'true' create_folder_object(options) end def create_folder_object(options) folder_object = '' folder_object += '' + options[:client_id].to_s + '' if options[:client_id] folder_object += '' + options[:title].to_s + '' if options[:title] folder_object += '' + options[:title].to_s + '' + '' + options[:description].to_s + '' + '' + options[:content_type].to_s + '' + '' + options[:is_active].to_s + '' + '' + options[:is_editable].to_s + '' + '' + options[:allow_children].to_s + '' if options[:parent] folder_object += ' ' + options[:parent].to_s + ' ' end folder_object += '' end def prepare_send(email_id, list_id, account=nil) account ||= @account create_send_object(email_id, list_id, account) end def create_send_object(email_id, list_id, account) '' + '' + '' + account.to_s + '' + '' + '' + '' + email_id.to_s + '' + '' + '' + '' + '' + '' + '' + list_id.to_s + '' + '' + '' end def prepare_delete_email(email_id, account=nil) account ||= @account create_delete_email_object(email_id, account) end def create_delete_email_object(email_id, account) '' + '' + '' + account + '' + '' + '' + email_id + '' + '' + '' end end end