Class: BrontoConnection

Inherits:
Object
  • Object
show all
Defined in:
app/models/bronto_connection.rb

Overview

COPYRIGHT:

Copyright (c) 2005-2011 North Carolina State University
Developed with funding for the National eXtension Initiative.

LICENSE:

BSD(-compatible)
see LICENSE file or view at http://about.extension.org/wiki/LICENSE

Constant Summary

JITP_DELIVERY_GROUP =
'54e33a5a-145c-4791-8ceb-55f803749335'

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (BrontoConnection) initialize(savon_logging = false)

A new instance of BrontoConnection



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/models/bronto_connection.rb', line 17

def initialize(savon_logging = false)
  HTTPI.log = false
  Savon.configure do |config|
    config.log = savon_logging
  end
  
  self.v3_client_connection = Savon::Client.new  do |wsdl, http|
    wsdl.document = "http://api.bronto.com/?q=mail_3&wsdl"
    http.auth.ssl.verify_mode = :none 
  end

  self.client_connection = Savon::Client.new do |wsdl, http| 
    wsdl.document = "https://api.bronto.com/v4?wsdl"
    http.auth.ssl.verify_mode = :none  
  end
end

Instance Attribute Details

- (Object) client_connection

Returns the value of attribute client_connection



14
15
16
# File 'app/models/bronto_connection.rb', line 14

def client_connection
  @client_connection
end

- (Object) session_id

Returns the value of attribute session_id



14
15
16
# File 'app/models/bronto_connection.rb', line 14

def session_id
  @session_id
end

- (Object) v3_client_connection

Returns the value of attribute v3_client_connection



13
14
15
# File 'app/models/bronto_connection.rb', line 13

def v3_client_connection
  @v3_client_connection
end

- (Object) v3_session_id

Returns the value of attribute v3_session_id



13
14
15
# File 'app/models/bronto_connection.rb', line 13

def v3_session_id
  @v3_session_id
end

Instance Method Details

- (Object) connect



34
35
36
37
38
# File 'app/models/bronto_connection.rb', line 34

def connect
  self.connect_v3
  self.connect_v4
  {:v3 => self.v3_session_id, :v4 => self.session_id}
end

- (Object) connect_v3



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/models/bronto_connection.rb', line 40

def connect_v3
  begin
    response =  self.v3_client_connection.request(:v3, :login) do
      soap.body = {
        :username => AppConfig.configtable['bronto_username'],
        :password => AppConfig.configtable['bronto_password'],
        :sitename => AppConfig.configtable['bronto_sitename']
      }
    end

    if(response)
      self.v3_session_id = response.to_hash[:login_response][:return][:session_id]
    end
    response
  rescue
    return nil
  end
end

- (Object) connect_v4



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'app/models/bronto_connection.rb', line 59

def connect_v4
  begin
    response =  self.client_connection.request(:v4, :login) do
      soap.body = {
        :api_token => AppConfig.configtable['bronto_api_key']
      }
    end

    if(response)
      self.session_id = response.to_hash[:login_response][:return]
    end
    response
  rescue
    return nil
  end
end

- (Object) read_click_activities_after(activity_date = Date.yesterday)



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'app/models/bronto_connection.rb', line 77

def read_click_activities_after(activity_date = Date.yesterday)  
  if(!self.session_id)
    self.connect
  end
  
  response = self.client_connection.request(:v4, :read_activities) do
    soap.header = { 
      "v4:sessionHeader" => {
        :session_id => self.session_id 
      }
    }
  
    soap.body = {
      :filter => {
        :start => activity_date,
        :size => 5000,  # completely arbitrary value that will hopefully get everything
        :types => ['click']
      }
    }
  end
  
  results = response.to_hash[:read_activities_response][:return]
  results
end

- (Object) read_contact_for_id(contact_id)



262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'app/models/bronto_connection.rb', line 262

def read_contact_for_id(contact_id)
  if(!self.session_id)
    self.connect
  end
  
  filter = {
    :id => contact_id,
  }

  response = self.client_connection.request(:v4, :read_contacts) do
    soap.header = { 
      "v4:sessionHeader" => {
        :session_id => self.session_id 
      }
    }
    soap.body = {
      :filter => filter,
    }
  end
  
  results = response[:read_contacts_response][:return]
  results
end

- (Object) read_message_for_id(message_id)



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'app/models/bronto_connection.rb', line 238

def read_message_for_id(message_id)
  if(!self.session_id)
    self.connect
  end
  
  filter = {
    :id => message_id,
  }

  response = self.client_connection.request(:v4, :read_messages) do
    soap.header = { 
      "v4:sessionHeader" => {
        :session_id => self.session_id 
      }
    }
    soap.body = {
      :filter => filter,
    }
  end
  
  results = response[:read_messages_response][:return]
  results
end

- (Object) read_messages_for_delivery_group_id(delivery_group_id)



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'app/models/bronto_connection.rb', line 213

def read_messages_for_delivery_group_id(delivery_group_id)
  if(!self.session_id)
    self.connect
  end
  
  filter = {
    :delivery_group_id => delivery_group_id,
    :list_by_type => 'MESSAGEGROUPS'
  }

  response = self.client_connection.request(:v4, :read_delivery_groups) do
    soap.header = { 
      "v4:sessionHeader" => {
        :session_id => self.session_id 
      }
    }
    soap.body = {
      :filter => filter,
    }
  end
  
  results = response[:read_delivery_groups_response][:return][:message_ids]
  results
end

- (Object) read_sends_for_delivery(delivery_id)

gets the sends for a particular delivery this is a v3 api call because as of implementation the v4 equivalent (readActivities, sends) has a bug (according to Bronto) that is causing it to return an error the sends value is included in the readActivities call



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'app/models/bronto_connection.rb', line 148

def read_sends_for_delivery(delivery_id)
  
  if(!self.v3_session_id)
    self.connect
  end
  results = []
  
  filter = {
    :value => {
      :type => 'string',
      :value => delivery_id
    },
    :attribute => 'deliveryId',
    :comparison => '=',
  }    

  response = self.v3_client_connection.request(:api, :read_sends) do
    soap.header = { 
      "api:sessionHeader" => {
        'api:sessionId' => self.v3_session_id 
      }
    }
    soap.body = {
      :filter => {:criteria => filter},
      :attributes => {:created => true}
    }
  end
  
  if(response[:read_sends_response][:return] and response[:read_sends_response][:return][:sends])
    return_response = response[:read_sends_response][:return][:sends]
    if(return_response.is_a?(Array))
      results += return_response
    else
      results << return_response
      return results
    end
  else
    return results
  end
  
  begin
    next_response = self.v3_client_connection.request(:api, :read_next) do
      soap.header = { 
        "api:sessionHeader" => {
          'api:sessionId' => self.v3_session_id 
        }
      }
    end
    if(next_response[:read_next_response][:return] and next_response[:read_next_response][:return][:sends])
      next_response_return = next_response[:read_next_response][:return][:sends]
    else
      next_response_return = nil
    end
    if(next_response_return)
      if(next_response_return.is_a?(array))
        results += next_response_return
      else
        results << next_response_return
      end
    end
  end while not next_response_return.blank?
  results
end

- (Object) read_sent_deliveries_on(delivery_date = Date.yesterday)



102
103
104
105
106
107
108
109
110
111
112
113
114
115
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 'app/models/bronto_connection.rb', line 102

def read_sent_deliveries_on(delivery_date = Date.yesterday)

  if(!self.session_id)
    self.connect
  end
  results = []
  
  filter = {
    :start => {
      :operator => 'SameDay',
      :value => delivery_date
    },
    :status => 'sent',
  }

  page_number = 1
  begin 
    response = self.client_connection.request(:v4, :read_deliveries) do
      soap.header = { 
        "v4:sessionHeader" => {
          :session_id => self.session_id 
        }
      }
      soap.body = {
        :filter => filter,
        # :include_recipients => true,
        :page_number => page_number,
      }
    end
    
    response_return = response[:read_deliveries_response][:return]
    if(response_return)
      results += response_return
      page_number += 1  
    end
  end while not response_return.blank?
  results
end