diff --git a/lib/expertsender_api.rb b/lib/expertsender_api.rb index 586f395..45f2eb0 100644 --- a/lib/expertsender_api.rb +++ b/lib/expertsender_api.rb @@ -15,6 +15,7 @@ require 'expertsender_api/result' require 'expertsender_api/expertsender_error' require 'expertsender_api/activity' +require 'expertsender_api/data_table' require 'expertsender_api/api' diff --git a/lib/expertsender_api/api.rb b/lib/expertsender_api/api.rb index cdd02f4..f82e467 100644 --- a/lib/expertsender_api/api.rb +++ b/lib/expertsender_api/api.rb @@ -1,6 +1,7 @@ module ExpertSenderApi class API include HTTParty + include ExpertSenderApi::DataTable class << self attr_accessor :api_key, :api_endpoint, :throws_exceptions @@ -28,6 +29,8 @@ def initialize(key: nil, **parameters) @newsletters_url = api_endpoint + '/Api/Newsletters' @transactionals_url = api_endpoint + '/Api/Transactionals' @activities_url = api_endpoint + '/Api/Activities' + @add_multi_row_url = api_endpoint + '/Api/DataTablesAddMultipleRows' + @clear_tbl_url = api_endpoint + '/Api/DataTablesClearTable' end end @@ -68,6 +71,37 @@ def get_subscriber_info(option: SUBSCRIBER_INFO_OPTION_FULL, email: nil) handle_response(response) end + def add_multi_data_to_tbl(tbl_name, data) + builder = Nokogiri::XML::Builder.new do |xml| + xml.ApiRequest(XML_NAMESPACES) do + xml.ApiKey api_key + xml.TableName tbl_name + xml.Data do + data.each { |row| add_row_to_xml(row, xml) } + end + end + end + + xml = builder.to_xml save_with: Nokogiri::XML::Node::SaveOptions::NO_DECLARATION + response = self.class.post(@add_multi_row_url, body: xml) + + handle_response(response) + end + + def clear_tbl(tbl_name) + builder = Nokogiri::XML::Builder.new do |xml| + xml.ApiRequest(XML_NAMESPACES) do + xml.ApiKey api_key + xml.TableName tbl_name + end + end + + xml = builder.to_xml save_with: Nokogiri::XML::Node::SaveOptions::NO_DECLARATION + response = self.class.post(@clear_tbl_url, body: xml) + + handle_response(response) + end + def update_subscriber(email, subscriber) result = get_subscriber_info(email: email) diff --git a/lib/expertsender_api/data_table.rb b/lib/expertsender_api/data_table.rb new file mode 100644 index 0000000..66adb82 --- /dev/null +++ b/lib/expertsender_api/data_table.rb @@ -0,0 +1,22 @@ +module ExpertSenderApi + module DataTable + def add_row_to_xml(row, xml) + xml.Row do + insert_row_to_xml(row, xml) + end + end + + def insert_row_to_xml(row, xml) + xml.Columns do + row.each_pair { |col_name, col_value| insert_col_to_xml(col_name, col_value, xml) } + end + end + + def insert_col_to_xml(col_name, col_value, xml) + xml.Column do + xml.Name col_name + xml.Value col_value + end + end + end +end diff --git a/spec/expertsender_api/api_spec.rb b/spec/expertsender_api/api_spec.rb index 176942e..97981b6 100644 --- a/spec/expertsender_api/api_spec.rb +++ b/spec/expertsender_api/api_spec.rb @@ -8,6 +8,11 @@ let(:subscriber_attributes) { { id: 1, list_id: 52, email: "test@httplab.ru" } } let(:subscribers_url) { "#{api_endpoint}/Api/Subscribers" } + let(:data_table) { 'SeriesTest' } + let(:dt_entry) { [{ id: 1, name: 'TestName', email: 'test@httplab.ru' }, { id: 2, name: 'TestName_2', email: 'test_2@httplab.ru' }] } + let(:add_multiple_data_dt_url) { "#{api_endpoint}/Api/DataTablesAddMultipleRows" } + let(:clear_dt_url) { "#{api_endpoint}/Api/DataTablesClearTable" } + let(:recipients) { ExpertSenderApi::Email::Recipients.new(recipients_attributes) } let(:recipients_attributes) { { subscriber_lists: [52, 53] } } @@ -200,6 +205,37 @@ subject.get_activities(date: Date.today, type: ExpertSenderApi::Activity::Clicks) end + + + its '#add multiple data to data table call' do + builder = Nokogiri::XML::Builder.new do |xml| + xml.ApiRequest(ExpertSenderApi::API::XML_NAMESPACES) do + xml.ApiKey api_key + xml.TableName data_table + xml.Data do + dt_entry.each { |row| subject.add_row_to_xml(row, xml) } + end + end + end + xml = builder.to_xml save_with: Nokogiri::XML::Node::SaveOptions::NO_DECLARATION + + expect_post(add_multiple_data_dt_url, xml) + subject.add_multi_data_to_tbl(data_table, dt_entry) + end + + its '#clear data table calls' do + builder = Nokogiri::XML::Builder.new do |xml| + xml.ApiRequest(ExpertSenderApi::API::XML_NAMESPACES) do + xml.ApiKey api_key + xml.TableName data_table + end + end + + xml = builder.to_xml save_with: Nokogiri::XML::Node::SaveOptions::NO_DECLARATION + + expect_post(clear_dt_url, xml) + subject.clear_tbl(data_table) + end end context 'when has wrong api key' do