class ExpertSenderApi::API

Constants

SUBSCRIBER_INFO_OPTION_FULL
SUBSCRIBER_INFO_OPTION_MEDIUM
SUBSCRIBER_INFO_OPTION_SHORT
XML_NAMESPACES

Attributes

api_endpoint[RW]
api_key[RW]
throws_exceptions[RW]
api_endpoint[RW]
api_key[RW]
throws_exceptions[RW]

Public Class Methods

new(key: nil, **parameters) click to toggle source
# File lib/expertsender_api/api.rb, line 18
def initialize(key: nil, **parameters)
  @api_key = key || self.class.api_key || ENV['EXPERTSENDER_API_KEY']
  @api_key = @api_key.strip if @api_key

  @throws_exceptions = parameters.has_key?(:throws_exceptions) ? parameters.delete(:throws_exceptions) : self.class.throws_exceptions
  @api_endpoint = parameters.delete(:api_endpoint) || self.class.api_endpoint

  unless api_endpoint.nil?
    @subscribers_url = api_endpoint + '/Api/Subscribers'
    @removed_subscribers_url = api_endpoint + '/Api/RemovedSubscribers'
    @newsletters_url = api_endpoint + '/Api/Newsletters'
    @transactionals_url = api_endpoint + '/Api/Transactionals'
  end
end

Public Instance Methods

add_or_update_subscriber(email, subscriber) click to toggle source
# File lib/expertsender_api/api.rb, line 88
def add_or_update_subscriber(email, subscriber)
  result = update_subscriber(email, subscriber)

  return add_subscriber_to_list(subscriber) if result.failed?

  result
end
add_subscriber_to_list(subscriber) click to toggle source
# File lib/expertsender_api/api.rb, line 33
def add_subscriber_to_list(subscriber)
  add_subscribers_to_list([subscriber])
end
add_subscribers_to_list(subscribers) click to toggle source
# File lib/expertsender_api/api.rb, line 37
def add_subscribers_to_list(subscribers)
  builder = Nokogiri::XML::Builder.new do |xml|
    xml.ApiRequest(XML_NAMESPACES) {
      xml.ApiKey api_key
      xml.MultiData {
        subscribers.each { |subscriber| subscriber.insert_to(xml) }
      }
    }
  end

  xml = builder.to_xml save_with: Nokogiri::XML::Node::SaveOptions::NO_DECLARATION
  response = self.class.post(@subscribers_url, body: xml)

  handle_response(response)
end
create_and_send_email(options) click to toggle source
# File lib/expertsender_api/api.rb, line 96
def create_and_send_email(options)
  recipients = options.delete :recipients
  content = options.delete :content

  builder = Nokogiri::XML::Builder.new do |xml|
    xml.ApiRequest(XML_NAMESPACES) {
      xml.ApiKey api_key
      xml.Data {
        recipients.insert_to xml
        content.insert_to xml
      }
    }
  end

  xml = builder.to_xml save_with: Nokogiri::XML::Node::SaveOptions::NO_DECLARATION
  response = self.class.post(@newsletters_url, body: xml)

  handle_response(response)
end
get_deleted_subscribers(options = {}) click to toggle source
# File lib/expertsender_api/api.rb, line 141
def get_deleted_subscribers(options = {})
  params = { apiKey: api_key }

  list_ids =  options[:list_ids]
  remove_types =  options[:remove_types]
  start_date = options[:start_date]
  end_date = options[:end_date]

  params[:listIds] = list_ids.join(',') if list_ids.respond_to?(:any?)
  params[:removeTypes] = remove_types.join(',') if remove_types.respond_to?(:any?)
  params[:startDate] = start_date.to_s unless start_date.nil?
  params[:endDate] = end_date.to_s unless end_date.nil?

  response = self.class.get(@removed_subscribers_url, query: params)

  handle_response(response)
end
get_subscriber_info(option: SUBSCRIBER_INFO_OPTION_FULL, email: nil) click to toggle source
# File lib/expertsender_api/api.rb, line 62
def get_subscriber_info(option: SUBSCRIBER_INFO_OPTION_FULL, email: nil)
  params = { apiKey: api_key, email: email, option: option }

  response = self.class.get(@subscribers_url, query: params)

  handle_response(response)
end
remove_subscriber_from_list(options) click to toggle source
# File lib/expertsender_api/api.rb, line 53
def remove_subscriber_from_list(options)
  email = options.delete :email
  id = options.delete :id

  response = id.nil? ? remove_subscriber_by_email(email, options) : remove_subscriber_by_id(id, options)

  handle_response(response)
end
send_transaction_email(options) click to toggle source
# File lib/expertsender_api/api.rb, line 116
def send_transaction_email(options)
  letter_id = options.delete :letter_id
  receiver = options.delete :receiver
  snippets = options.delete :snippets

  builder = Nokogiri::XML::Builder.new do |xml|
    xml.ApiRequest(XML_NAMESPACES) {
      xml.ApiKey api_key
      xml.Data {
        receiver.insert_to xml
        if snippets.any?
          xml.Snippets {
            snippets.each { |snippet| snippet.insert_to(xml) }
          }
        end
      }
    }
  end

  xml = builder.to_xml save_with: Nokogiri::XML::Node::SaveOptions::NO_DECLARATION
  response = self.class.post("#{@transactionals_url}/#{letter_id}", body: xml)

  handle_response(response)
end
update_subscriber(email, subscriber) click to toggle source
# File lib/expertsender_api/api.rb, line 70
def update_subscriber(email, subscriber)
  result = get_subscriber_info(email: email)

  return result if result.failed?

  expertsender_id = result.parsed_response.xpath('//Data/Id').text
  list_ids = result.parsed_response.xpath('//StateOnList/ListId').map(&:text)

  subscriber.id = expertsender_id

  list_ids.each do |list_id|
    subscriber.list_id = list_id
    res = add_subscriber_to_list(subscriber)
  end

  result
end

Private Instance Methods

handle_response(response) click to toggle source
# File lib/expertsender_api/api.rb, line 161
def handle_response(response)
  result = Result.new(response)

  if should_raise_for_response?(result)
    error = ExpertSenderError.new("ExpertSender API Error: #{result.error_message} (code #{result.error_code})")
    error.code = result.error_code
    raise error
  end

  result
end
remove_subscriber_by_email(email, options = {}) click to toggle source
# File lib/expertsender_api/api.rb, line 177
def remove_subscriber_by_email(email, options = {})
  params = options.merge({ apiKey: api_key, email: email })

  self.class.delete(@subscribers_url, query: params)
end
remove_subscriber_by_id(id, options = {}) click to toggle source
# File lib/expertsender_api/api.rb, line 183
def remove_subscriber_by_id(id, options = {})
  params = options.merge({ apiKey: api_key })

  self.class.delete("#{@subscribers_url}/#{id}", query: params)
end
should_raise_for_response?(result) click to toggle source
# File lib/expertsender_api/api.rb, line 173
def should_raise_for_response?(result)
  @throws_exceptions and result.failed?
end