Class: ExpertSenderApi::API

Inherits:
Object
  • Object
show all
Includes:
HTTParty
Defined in:
lib/expertsender_api/api.rb

Constant Summary collapse

SUBSCRIBER_INFO_OPTION_SHORT =
1
SUBSCRIBER_INFO_OPTION_MEDIUM =
2
SUBSCRIBER_INFO_OPTION_FULL =
3
XML_NAMESPACES =
{ 'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
'xmlns:xs' => 'http://www.w3.org/2001/XMLSchema' }

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key: nil, **parameters) ⇒ API

Returns a new instance of API.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# 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

Class Attribute Details

.api_endpointObject

Returns the value of attribute api_endpoint.



6
7
8
# File 'lib/expertsender_api/api.rb', line 6

def api_endpoint
  @api_endpoint
end

.api_keyObject

Returns the value of attribute api_key.



6
7
8
# File 'lib/expertsender_api/api.rb', line 6

def api_key
  @api_key
end

.throws_exceptionsObject

Returns the value of attribute throws_exceptions.



6
7
8
# File 'lib/expertsender_api/api.rb', line 6

def throws_exceptions
  @throws_exceptions
end

Instance Attribute Details

#api_endpointObject

Returns the value of attribute api_endpoint.



9
10
11
# File 'lib/expertsender_api/api.rb', line 9

def api_endpoint
  @api_endpoint
end

#api_keyObject

Returns the value of attribute api_key.



9
10
11
# File 'lib/expertsender_api/api.rb', line 9

def api_key
  @api_key
end

#throws_exceptionsObject

Returns the value of attribute throws_exceptions.



9
10
11
# File 'lib/expertsender_api/api.rb', line 9

def throws_exceptions
  @throws_exceptions
end

Instance Method Details

#add_or_update_subscriber(email, subscriber) ⇒ Object



88
89
90
91
92
93
94
# 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) ⇒ Object



33
34
35
# 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) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# 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) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# 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 = {}) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# 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) ⇒ Object



62
63
64
65
66
67
68
# 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) ⇒ Object



53
54
55
56
57
58
59
60
# 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) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# 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) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# 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