Class: Sailthru::TriggermailClient

Inherits:
Object
  • Object
show all
Defined in:
lib/sailthru/triggermail_client.rb

Constant Summary collapse

VERSION =
'1.0'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key, secret, api_uri) ⇒ TriggermailClient

params:

api_key, String
secret, String
api_uri, String

Instantiate a new client; constructor optionally takes overrides for key/secret/uri.



60
61
62
63
64
# File 'lib/sailthru/triggermail_client.rb', line 60

def initialize(api_key, secret, api_uri)
  @api_key = api_key
  @secret  = secret
  @api_uri = api_uri
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



50
51
52
# File 'lib/sailthru/triggermail_client.rb', line 50

def api_key
  @api_key
end

#api_uriObject

Returns the value of attribute api_uri.



50
51
52
# File 'lib/sailthru/triggermail_client.rb', line 50

def api_uri
  @api_uri
end

#last_requestObject

Returns the value of attribute last_request.



50
51
52
# File 'lib/sailthru/triggermail_client.rb', line 50

def last_request
  @last_request
end

#secretObject

Returns the value of attribute secret.



50
51
52
# File 'lib/sailthru/triggermail_client.rb', line 50

def secret
  @secret
end

#versionObject

Returns the value of attribute version.



50
51
52
# File 'lib/sailthru/triggermail_client.rb', line 50

def version
  @version
end

Class Method Details

.extract_param_values(params) ⇒ Object

params:

params, Hash

returns:

Array, values of each item in the Hash (and nested hashes)

Extracts the values of a set of parameters, recursing into nested assoc arrays.



326
327
328
329
330
331
332
333
334
335
336
337
# File 'lib/sailthru/triggermail_client.rb', line 326

def self.extract_param_values(params)
  values = []
  params.each do |k, v|
    # puts "k,v: #{k}, #{v}"
    if v.class == Hash
      values.concat Sailthru::TriggermailClient.extract_param_values(v)
    else
      values.push v
    end
  end
  return values
end

.get_signature_hash(params, secret) ⇒ Object

params:

params, Hash
secret, String

returns:

String, an MD5 hash of the secret + sorted list of parameter values for an API call.


345
346
347
348
# File 'lib/sailthru/triggermail_client.rb', line 345

def self.get_signature_hash(params, secret)
  string = secret + self.extract_param_values(params).sort_by{|x| x.to_s}.join("")
  MD5.md5(string) # debuggin
end

Instance Method Details

#api_get(action, data) ⇒ Object

Perform API GET request



230
231
232
# File 'lib/sailthru/triggermail_client.rb', line 230

def api_get(action, data)
  api_request(action, data, 'GET')
end

#api_post(action, data) ⇒ Object

Perform API POST request



235
236
237
# File 'lib/sailthru/triggermail_client.rb', line 235

def api_post(action, data)
  api_request(action, data, 'POST')
end

#api_request(action, data, request_type) ⇒ Object

params:

action, String
data, Hash
request, String "GET" or "POST"

returns:

Hash

Perform an API request, using the shared-secret auth hash.



248
249
250
251
252
253
254
255
256
257
258
# File 'lib/sailthru/triggermail_client.rb', line 248

def api_request(action, data, request_type)
  data[:api_key] = @api_key
  data[:format] ||= 'json'
  data[:sig] = Sailthru::TriggermailClient.get_signature_hash(data, @secret)
  _result = self.http_request("#{@api_uri}/#{action}", data, request_type)


  # NOTE: don't do the unserialize here
  unserialized = JSON.parse(_result)
  return unserialized ? unserialized : _result
end

#flatten_nested_hash(hash, brackets = true) ⇒ Object

Flatten nested hash for GET / POST request.



305
306
307
308
309
310
311
312
313
314
315
316
317
318
# File 'lib/sailthru/triggermail_client.rb', line 305

def flatten_nested_hash(hash, brackets = true)
  f = {}
  hash.each do |key, value|
    _key = brackets ? "[#{key}]" : key.to_s
    if value.class == Hash
      flatten_nested_hash(value).each do |k, v|
        f["#{_key}#{k}"] = v
      end
    else
      f[_key] = value
    end
  end
  return f
end

#get_blast(blast_id) ⇒ Object

params:

blast_id, Fixnum

returns:

Hash, response data from server

Get information on a previously scheduled email blast



131
132
133
# File 'lib/sailthru/triggermail_client.rb', line 131

def get_blast(blast_id)
  self.api_get(:blast, {:blast_id => blast_id.to_s})
end

#get_email(email) ⇒ Object

params:

email, String

returns:

Hash, response data from server

Return information about an email address, including replacement vars and lists.



141
142
143
# File 'lib/sailthru/triggermail_client.rb', line 141

def get_email(email)
  self.api_get(:email, {:email => email})
end

#get_send(send_id) ⇒ Object

params:

send_id, Fixnum

returns:

Hash, response data from server

Get the status of a send.



93
94
95
# File 'lib/sailthru/triggermail_client.rb', line 93

def get_send(send_id)
  self.api_get(:send, {:send_id => send_id.to_s})      
end

#get_template(template_name) ⇒ Object

params:

template_name, String

returns:

Hash of response data.

Get a template.



183
184
185
# File 'lib/sailthru/triggermail_client.rb', line 183

def get_template(template_name)
  self.api_get(:template, {:template => template_name})
end

#http_request(uri, data, method = 'POST') ⇒ Object

params:

uri, String
data, Hash
method, String "GET" or "POST"

returns:

String, body of response


267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'lib/sailthru/triggermail_client.rb', line 267

def http_request(uri, data, method = 'POST')
  data = flatten_nested_hash(data, false)
  # puts data.inspect
  if method == 'POST'
    post_data = data
  else
    uri += "?" + data.map{ |key, value| "#{key}=#{value}" }.join("&")
  end

  req = nil
  headers = {"User-Agent" => "Triggermail API Ruby Client #{VERSION}"}

  _uri  = URI.parse(uri)
  if method == 'POST'
    req = Net::HTTP::Post.new(_uri.path, headers)
    req.set_form_data(data)
  else
    req = Net::HTTP::Get.new("#{_uri.path}?#{_uri.query}", headers)
  end

  @last_request = req
  begin
    response = Net::HTTP.start(_uri.host, _uri.port) {|http|
      http.request(req)
    }
  rescue Exception => e
    raise Sailthru::TriggermailClientException.new("Unable to open stream: #{_uri.to_s}");
  end

  if response.body
    return response.body
  else
    raise Sailthru::TriggermailClientException.new("No response received from stream: #{_uri.to_s}")
  end

end

#import_contacts(email, password, with_names = false) ⇒ Object

params:

email, String
password, String
with_names, Boolean

returns:

Hash, response data from server

Fetch email contacts from an address book at one of the major email providers (aol/gmail/hotmail/yahoo) Use the with_names parameter if you want to fetch the contact names as well as emails



170
171
172
173
174
# File 'lib/sailthru/triggermail_client.rb', line 170

def import_contacts(email, password, with_names = false)
  data = { :email => email, :password => password }
  data[:names] = 1 if with_names
  self.api_post(:contacts, data)
end

#receive_verify_post(params, request) ⇒ Object

params:

params, Hash
request, String

returns:

TrueClass or FalseClass, Returns true if the incoming request is an authenticated verify post.


207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/sailthru/triggermail_client.rb', line 207

def receive_verify_post(params, request)
  if request.post?
    [:action, :email, :send_id, :sig].each { |key| return false unless params.has_key?(key) }
  
    return false unless params[:action] == :verify
  
    sig = params[:sig]
    params.delete(:sig)
    return false unless sig == TriggermailClient.get_signature_hash(params, @secret)
  
    _send = self.get_send(params[:send_id])
    return false unless _send.has_key?(:email)
  
    return false unless _send[:email] == params[:email]
  
    return true
  else
    return false
  end
end

#save_template(template_name, template_fields) ⇒ Object

params:

template_name, String
template_fields, Hash

returns:

Hash containg response from the server.

Save a template.



195
196
197
198
199
# File 'lib/sailthru/triggermail_client.rb', line 195

def save_template(template_name, template_fields)
  data = template_fields
  data[:template] = template_name
  self.api_post(:template, data)
end

#schedule_blast(name, list, schedule_time, from_name, from_email, subject, content_html, content_text, options) ⇒ Object

params:

name, String
list, String
schedule_time, String
from_name, String
from_email, String
subject, String
content_html, String
content_text, String
options, Hash

returns:

Hash, response data from server

Schedule a mass mail blast



111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/sailthru/triggermail_client.rb', line 111

def schedule_blast(name, list, schedule_time, from_name, from_email, subject, content_html, content_text, options)
  post = options ? options : {}
  post[:name] = name
  post[:list] = list
  post[:schedule_time] = schedule_time
  post[:from_name] = from_name
  post[:from_email] = from_email
  post[:subject] = subject
  post[:content_html] = content_html
  post[:content_text] = content_text
  self.api_post(:blast, post)
end

#send(template_name, email, vars, options = {}, schedule_time = nil) ⇒ Object

params:

template_name, String
email, String
replacements, Hash
options, Hash
  replyto: override Reply-To header
  test: send as test email (subject line will be marked, will not count towards stats)
schedule_time, String (see schedule_blast schedule_time in API docs)

returns:

Hash, response data from server


76
77
78
79
80
81
82
83
84
# File 'lib/sailthru/triggermail_client.rb', line 76

def send(template_name, email, vars, options = {}, schedule_time = nil)
  post = {}
  post[:template] = template_name
  post[:email] = email
  post[:vars] = vars
  post[:options] = options
  post[:schedule_time] = schedule_time if schedule_time
  result = self.api_post(:send, post)
end

#set_email(email, vars = {}, lists = {}, templates = {}) ⇒ Object

params:

email, String
vars, Hash
lists, Hash mapping list name => 1 for subscribed, 0 for unsubscribed

returns:

Hash, response data from server

Set replacement vars and/or list subscriptions for an email address.



153
154
155
156
157
158
159
# File 'lib/sailthru/triggermail_client.rb', line 153

def set_email(email, vars = {}, lists = {}, templates = {})
  data = {:email => email}
  data[:vars] = vars unless vars.empty?
  data[:lists] = lists unless lists.empty?
  data[:templates] = templates unless templates.empty?
  self.api_post(:email, data)
end