lib/sailthru.rb in sailthru-client-1.02 vs lib/sailthru.rb in sailthru-client-1.03
- old
+ new
@@ -5,11 +5,11 @@
require 'json'
require 'md5'
module Sailthru
- Version = VERSION = '1.02'
+ Version = VERSION = '1.03'
class SailthruClientException < Exception
end
module Helpers
@@ -131,11 +131,11 @@
post[:options] = options
if vars.length > 0
post[:vars] = vars
end
-
+
if schedule_time != nil
post[:schedule_time] = schedule_time
end
return self.api_post(:send, post)
end
@@ -167,11 +167,11 @@
# Get the status of a send.
def get_send(send_id)
self.api_get(:send, {:send_id => send_id.to_s})
end
-
+
def cancel_send(send_id)
self.api_delete(:send, {:send_id => send_id.to_s})
end
# params:
@@ -196,32 +196,92 @@
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)
+ api_post(:blast, post)
end
+ # params
+ # blast_id, Fixnum | String
+ # name, String
+ # list, String
+ # schedule_time, String
+ # from_name, String
+ # from_email, String
+ # subject, String
+ # content_html, String
+ # content_text, String
+ # options, hash
+ #
+ # updates existing blast
+ def update_blast(blast_id, name = nil, list = nil, schedule_time = nil, from_name = nil, from_email = nil, subject = nil, content_html = nil, content_text = nil, options = {})
+ data = options ? options : {}
+ data[:blast_id] = blast_id
+ if name != nil
+ data[:name] = name
+ end
+ if list != nil
+ data[:list] = list
+ end
+ if schedule_time != nil
+ data[:schedule_time] = schedule_time
+ end
+ if from_name != nil
+ data[:from_name] = from_name
+ end
+ if from_email != nil
+ data[:from_email] = from_email
+ end
+ if subject != nil
+ data[:subject] = subject
+ end
+ if content_html != nil
+ data[:content_html] = content_html
+ end
+ if content_text != nil
+ data[:content_text] = content_text
+ end
+ api_post(:blast, data)
+ end
+
+
# params:
- # blast_id, Fixnum
+ # blast_id, Fixnum | String
# returns:
# Hash, response data from server
#
# Get information on a previously scheduled email blast
def get_blast(blast_id)
- self.api_get(:blast, {:blast_id => blast_id.to_s})
+ api_get(:blast, {:blast_id => blast_id.to_s})
end
# params:
+ # blast_id, Fixnum | String
+ #
+ # Cancel a scheduled Blast
+ def cancel_blast(blast_id)
+ api_post(:blast, {:blast_id => blast_id, :schedule_time => ''})
+ end
+
+ # params:
+ # blast_id, Fixnum | String
+ #
+ # Delete a Blast
+ def delete_blast(blast_id)
+ api_delete(:blast, {:blast_id => blast_id})
+ end
+
+ # params:
# email, String
# returns:
# Hash, response data from server
#
# Return information about an email address, including replacement vars and lists.
def get_email(email)
- self.api_get(:email, {:email => email})
+ api_get(:email, {:email => email})
end
# params:
# email, String
# vars, Hash
@@ -315,38 +375,218 @@
#
# Record that a user has made a purchase, or has added items to their purchase total.
def purchase(email, items, incomplete = nil, message_id = nil)
data = {}
data[:email] = email
-
+
if verify_purchase_items(items)
data[:items] = items
end
if incomplete != nil
data[:incomplete] = incomplete.to_i
end
-
+
if message_id != nil
data[:message_id] = message_id
end
api_post(:purchase, data)
end
+
+ # <b>DEPRECATED:</b> Please use either stats_list or stats_blast
# params:
# stat, String
#
# returns:
# hash, response from server
# Request various stats from Sailthru.
def get_stats(stat)
+ warn "[DEPRECATION] `get_stats` is deprecated. Please use `stats_list` and `stats_blast` instead"
api_get(:stats, {:stat => stat})
end
+ # params
+ # list, String
+ # date, String
+ #
+ # returns:
+ # hash, response from server
+ # Retrieve information about your subscriber counts on a particular list, on a particular day.
+ def stats_list(list = nil, date = nil)
+ data = {}
+ if list != nil
+ data[:list] = list
+ end
+ if date != nil
+ data[:date] = date
+ end
+ data[:stat] = 'list'
+
+ stats(data)
+ end
+
+
+ # params
+ # blast_id, String
+ # start_date, String
+ # end_date, String
+ # options, Hash
+ #
+ # returns:
+ # hash, response from server
+ # Retrieve information about a particular blast or aggregated information from all of blasts over a specified date range
+ def stats_blast(blast_id = nil, start_date = nil, end_date = nil, options = {})
+ data = options
+ if blast_id != nil
+ data[:blast_id] = blast_id
+ end
+ if start_date != nil
+ data[:start_date] = start_date
+ end
+ if end_date != nil
+ data[:end_date] = end_date
+ end
+ data[:stat] = 'blast'
+ stats(data)
+ end
+
+
+ # params
+ # title, String
+ # url, String
+ # date, String
+ # tags, Array or Comma separated string
+ # vars, Hash
+ #
+ # Push a new piece of content to Sailthru, triggering any applicable alerts.
+ # http://docs.sailthru.com/api/content
+ def push_content(title, url, date = nil, tags = nil, vars = {})
+ data = {}
+ data[:title] = title
+ data[:url] = url
+ if date != nil
+ data[:date] = date
+ end
+ if tags != nil
+ if tags.class == Array
+ tags = tags.join(',')
+ end
+ data[:tags] = tags
+ end
+ if vars.length > 0
+ data[:vars] = vars
+ end
+ api_post(:content, data)
+ end
+
+ # params
+ # list, String
+ # format, String
+ #
+ # Download a list. Obviously, this can potentially be a very large download.
+ # 'txt' is default format since, its more compact as compare to others
+ def get_list(list, format = 'txt')
+ return api_get(:list, {:list => list, :format => format})
+ end
+
+
+ # params
+ # list, String
+ # emails, String | Array
+ # Upload a list. The list import job is queued and will happen shortly after the API request.
+ def save_list(list, emails)
+ data = {}
+ data[:list] = list
+ data[:emails] = (emails.class == Array) ? emails.join(',') : emails
+ return api_post(:list, data)
+ end
+
+
+ # params
+ # list, String
+ #
+ # Deletes a list
+ def delete_list(list)
+ api_delete(:list, {:list => list})
+ end
+
+ # params
+ # email, String
+ # hid_only, Boolean
+ #
+ # gets horizon data
+ def get_horizon(email, hid_only = false)
+ data = {}
+ data[:email] = email
+ if hid_only == true
+ data[:hid_only] = 1
+ end
+ api_get(:horizon, data)
+ end
+
+
+ # params
+ # email, String
+ # tags, String | Array
+ #
+ # sets horizon data
+ def set_horizon(email, tags)
+ data = {}
+ data[
+ :email] = email
+ data[:tags] = (tags.class == Array) ? tags.join(',') : tags
+ api_post(:horizon, data)
+ end
+
+ # params
+ # email, String
+ #
+ # get user alert data
+ def get_alert(email)
+ api_get(:alert, {:email => email})
+ end
+
+ # params
+ # email, String
+ # type, String
+ # template, String
+ # _when, String
+ # options, hash
+ #
+ # Add a new alert to a user. You can add either a realtime or a summary alert (daily/weekly).
+ # _when is only required when alert type is weekly or daily
+ def save_alert(email, type, template, _when = nil, options = {})
+ data = options
+ data[:email] = email
+ data[:type] = type
+ data[:template] = template
+ if (type == 'weekly' || type == 'daily')
+ data[:when] = _when
+ end
+ api_post(:alert, data)
+ end
+
+
+ # params
+ # email, String
+ # alert_id, String
+ #
+ # delete user alert
+ def delete_alert(email, alert_id)
+ data = {:email => email, :alert_id => alert_id}
+ api_delete(:alert, data)
+ end
+
protected
+ # Make Stats API Request
+ def stats(data)
+ api_get(:stats, data)
+ end
+
# Perform API GET request
def api_get(action, data)
api_request(action, data, 'GET')
end
@@ -375,12 +615,19 @@
data[:sig] = 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
+ if data[:format] == 'json'
+ begin
+ unserialized = JSON.parse(_result)
+ return unserialized ? unserialized : _result
+ rescue JSON::JSONError => e
+ return {'error' => e}
+ end
+ end
+ return _result
end
# params:
# uri, String
@@ -395,11 +642,11 @@
else
uri += "?" + data.map{ |key, value| "#{CGI::escape(key.to_s)}=#{CGI::escape(value.to_s)}" }.join("&")
end
req = nil
headers = {"User-Agent" => "Sailthru 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
@@ -422,8 +669,8 @@
if response.body
return response.body
else
raise SailthruClientException.new("No response received from stream: #{_uri.to_s}")
end
- end
+ end
end
end
\ No newline at end of file