lib/sailthru.rb in sailthru-client-1.13 vs lib/sailthru.rb in sailthru-client-1.14

- old
+ new

@@ -7,11 +7,11 @@ require 'digest/md5' require 'net/http/post/multipart' module Sailthru - Version = VERSION = '1.13' + Version = VERSION = '1.14' class SailthruClientException < Exception end module Helpers @@ -85,11 +85,11 @@ f[_key] = value end end return f end - + end class SailthruClient include Helpers @@ -99,15 +99,17 @@ # params: # api_key, String # secret, String # api_uri, String # - # Instantiate a new client; constructor optionally takes overrides for key/secret/uri. - def initialize(api_key, secret, api_uri = nil) + # Instantiate a new client; constructor optionally takes overrides for key/secret/uri and proxy server settings. + def initialize(api_key, secret, api_uri=nil, proxy_host=nil, proxy_port=nil) @api_key = api_key @secret = secret @api_uri = if api_uri.nil? then 'https://api.sailthru.com' else api_uri end + @proxy_host = proxy_host + @proxy_port = proxy_port @verify_ssl = true end # params: # template_name, String @@ -198,12 +200,12 @@ post[:copy_blast] = blast_id #post[:name] = name post[:schedule_time] = schedule_time api_post(:blast, post) end - + # params # blast_id, Fixnum | String # name, String # list, String # schedule_time, String @@ -297,26 +299,10 @@ data[:templates] = templates unless templates.empty? self.api_post(:email, data) end # 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 - 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 - - - # params: # template_name, String # returns: # Hash of response data. # # Get a template. @@ -335,11 +321,11 @@ def save_template(template_name, template_fields) data = template_fields data[:template] = template_name self.api_post(:template, data) end - + # params: # template_name, String # returns: # Hash of response data. # @@ -488,31 +474,33 @@ 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}) + # Get information about a list. + def get_list(list) + return api_get(:list, {:list => list}) end + # params + # + # Get information about all lists + def get_lists() + return api_get(:list, {}) + 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 = {} + # options, Hash + # Create a list, or update a list. + def save_list(list, options = {}) + data = options 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) @@ -588,11 +576,11 @@ # Make Stats API Request def stats(data) api_get(:stats, data) end - + # params # job, String # options, hash # report_email, String # postback_url, String @@ -603,53 +591,55 @@ 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 + # params + # emails, String | Array # implementation for import_job def process_import_job(list, emails, report_email = nil, postback_url = nil) data = {} data['list'] = list - data['emails'] = emails + data['emails'] = Array(emails).join(',') 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 @@ -660,11 +650,11 @@ #Perform API DELETE request def api_delete(action, data) api_request(action, data, 'DELETE') end - + protected # params: # action, String # data, Hash @@ -685,14 +675,14 @@ else data[:api_key] = @api_key data[:format] ||= 'json' data[:sig] = get_signature_hash(data, @secret) end - + if (!binary_key.nil?) data[binary_key] = binary_key_data - end + 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 @@ -712,61 +702,61 @@ # method, String "GET" or "POST" # returns: # String, body of response def http_request(uri, data, method = 'POST', binary_key = nil) data = flatten_nested_hash(data, false) - + 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' - if (!binary_key.nil?) + 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, 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 req = Net::HTTP::Get.new(request_uri, headers) end end begin - http = Net::HTTP.new(_uri.host, _uri.port) - + http = Net::HTTP::Proxy(@proxy_host, @proxy_port).new(_uri.host, _uri.port) + if _uri.scheme == 'https' http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE if @verify_ssl != true # some openSSL client doesn't work without doing this end response = http.start { http.request(req) } - + rescue Exception => e raise SailthruClientException.new("Unable to open stream: #{_uri}\n#{e}"); end - + if response.body return response.body else raise SailthruClientException.new("No response received from stream: #{_uri}") 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