lib/sailthru.rb in sailthru-client-1.09 vs lib/sailthru.rb in sailthru-client-1.10
- old
+ new
@@ -1,15 +1,16 @@
+require 'rubygems'
require 'net/http'
require 'uri'
require 'cgi'
-require 'rubygems'
require 'json'
require 'digest/md5'
+require 'net/http/post/multipart'
module Sailthru
- Version = VERSION = '1.09'
+ Version = VERSION = '1.10'
class SailthruClientException < Exception
end
module Helpers
@@ -603,19 +604,76 @@
# Make Stats API Request
def stats(data)
api_get(:stats, data)
end
+
+ # params
+ # job, String
+ # options, hash
+ # report_email, String
+ # postback_url, String
+ # binary_key, String
+ #
+ # interface for making request to job call
+ def process_job(job, options = {}, report_email = nil, postback_url = nil, binary_key = nil)
+ data = options
+ data['job'] = job
+ if !report_email.nil?
+ data['report_email'] = report_email
+ end
+
+ if !postback_url.nil?
+ data['postback_url'] = postback_url
+ end
+ api_post(:job, data, binary_key)
+ end
+
+ # implementation for import_job
+ def process_import_job(list, emails, report_email = nil, postback_url = nil)
+ data = {}
+ data['list'] = list
+ data['emails'] = emails
+ process_job(:import, data, report_email, postback_url)
+ end
+
+ # implementation for import job using file upload
+ def process_import_job_from_file(list, file_path, report_email = nil, postback_url = nil)
+ data = {}
+ data['list'] = list
+ data['file'] = file_path
+ process_job(:import, data, report_email, postback_url, 'file')
+ end
+
+ # implementation for snapshot job
+ def process_snapshot_job(query = {}, report_email = nil, postback_url = nil)
+ data = {}
+ data['query'] = query
+ process_job(:snapshot, data, report_email, postback_url)
+ end
+
+ # implementation for export list job
+ def process_export_list_job(list, report_email = nil, postback_url = nil)
+ data = {}
+ data['list'] = list
+ process_job(:export_list_data, data, report_email, postback_url)
+ end
+
+ # get status of a job
+ def get_job_status(job_id)
+ api_get(:job, {'job_id' => job_id})
+ end
+
# Perform API GET request
def api_get(action, data)
api_request(action, data, 'GET')
end
# Perform API POST request
- def api_post(action, data)
- api_request(action, data, 'POST')
+ def api_post(action, data, binary_key = nil)
+ api_request(action, data, 'POST', binary_key)
end
#Perform API DELETE request
def api_delete(action, data)
api_request(action, data, 'DELETE')
@@ -630,15 +688,26 @@
# returns:
# Hash
#
# Perform an API request, using the shared-secret auth hash.
#
- def api_request(action, data, request_type)
+ def api_request(action, data, request_type, binary_key = nil)
+
data[:api_key] = @api_key
data[:format] ||= 'json'
+
+ if (!binary_key.nil?)
+ binary_key_data = data[binary_key]
+ data.delete(binary_key)
+ end
+
data[:sig] = get_signature_hash(data, @secret)
- _result = self.http_request("#{@api_uri}/#{action}", data, request_type)
+
+ if (!binary_key.nil?)
+ data[binary_key] = binary_key_data
+ end
+ _result = self.http_request("#{@api_uri}/#{action}", data, request_type, binary_key)
# NOTE: don't do the unserialize here
if data[:format] == 'json'
begin
@@ -656,24 +725,33 @@
# uri, String
# data, Hash
# method, String "GET" or "POST"
# returns:
# String, body of response
- def http_request(uri, data, method = 'POST')
+ def http_request(uri, data, method = 'POST', binary_key = nil)
data = flatten_nested_hash(data, false)
- if method == 'POST'
- post_data = data
- else
+
+ if method != 'POST'
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)
+ if (!binary_key.nil?)
+ binary_data = data[binary_key]
+ data[binary_key] = UploadIO.new(File.open(binary_data), "text/plain")
+ #req = Net::HTTP::Post::Multipart.new(_uri.path, binary_key => UploadIO.new(File.open(binary_data), "text/plain"))
+ req = Net::HTTP::Post::Multipart.new(_uri.path, data)
+ else
+ req = Net::HTTP::Post.new(_uri.path, headers)
+ req.set_form_data(data)
+ end
+
else
request_uri = "#{_uri.path}?#{_uri.query}"
if method == 'DELETE'
req = Net::HTTP::Delete.new(request_uri, headers)
else
@@ -686,14 +764,19 @@
http.request(req)
}
rescue Exception => e
raise SailthruClientException.new("Unable to open stream: #{_uri.to_s}");
end
-
+
if response.body
return response.body
else
raise SailthruClientException.new("No response received from stream: #{_uri.to_s}")
end
+ end
+
+ def http_multipart_request(uri, data)
+ req = Net::HTTP::Post::Multipart.new url.path,
+ "file" => UploadIO.new(data['file'], "application/octet-stream")
end
end
end