Skip to content

Commit

Permalink
Merge 60b3d69 into 98d1e66
Browse files Browse the repository at this point in the history
  • Loading branch information
ErnestK committed Mar 24, 2017
2 parents 98d1e66 + 60b3d69 commit 763555e
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/expertsender_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
34 changes: 34 additions & 0 deletions lib/expertsender_api/api.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module ExpertSenderApi
class API
include HTTParty
include ExpertSenderApi::DataTable

class << self
attr_accessor :api_key, :api_endpoint, :throws_exceptions
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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)

Expand Down
22 changes: 22 additions & 0 deletions lib/expertsender_api/data_table.rb
Original file line number Diff line number Diff line change
@@ -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
36 changes: 36 additions & 0 deletions spec/expertsender_api/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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] } }

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 763555e

Please sign in to comment.