class SailthruBatchingClient
Attributes
api_key[RW]
api_secret[RW]
batch_size[W]
file_root[W]
Public Class Methods
base_file_name()
click to toggle source
where we store our temp files
# File lib/sailthru_batching_client.rb, line 11 def self.base_file_name "#{self.file_root}/#{Process.pid}-sailthru-list" end
batch_size()
click to toggle source
max size that Sailthru can support
# File lib/sailthru_batching_client.rb, line 16 def self.batch_size @batch_size ||= 10000000 end
clear_tmp_files()
click to toggle source
clear the files we created to push data to Sailthru
# File lib/sailthru_batching_client.rb, line 21 def self.clear_tmp_files FileUtils.rm_rf("#{self.base_file_name}*") end
client()
click to toggle source
instance of sailthru client
# File lib/sailthru_batching_client.rb, line 26 def self.client @client ||= ::Sailthru::SailthruClient.new( self.api_key, self.api_secret, "https://api.sailthru.com" ) end
connected?()
click to toggle source
We are connected if a client is present
# File lib/sailthru_batching_client.rb, line 35 def self.connected? @client.present? end
establish_connection()
click to toggle source
get an instance of sailthru client
# File lib/sailthru_batching_client.rb, line 40 def self.establish_connection self.client end
file_root()
click to toggle source
root at which we are going to write our temp files
# File lib/sailthru_batching_client.rb, line 45 def self.file_root @file_root ||= "/tmp" end
respond_to?(m)
click to toggle source
override respond_to? to also include methods of our instance of SailthruClient
Calls superclass method
# File lib/sailthru_batching_client.rb, line 51 def self.respond_to?(m) return true if super(m) return self.client.respond_to?(m) end
update_users(user_data_array, clear_tmp = true)
click to toggle source
update rows of email data
# File lib/sailthru_batching_client.rb, line 57 def self.update_users(user_data_array, clear_tmp = true) # make sure we have the dir FileUtils.mkdir_p(File.dirname(self.base_file_name)) self.clear_tmp_files if clear_tmp begin self.batch_data_as_json(user_data_array).each_with_index do |rows, i| # write to the file system file = self.write_temp_file(rows, i) # send our file self.send_file(file) end return true ensure self.clear_tmp_files if clear_tmp end end
Protected Class Methods
batch_data_as_json(data)
click to toggle source
# File lib/sailthru_batching_client.rb, line 77 def self.batch_data_as_json(data) # convert to strings so we can count bytes data = data.collect{|r| JSON.unparse(r)} ret = [] # start off 0 size and empty batch size = 0 batch = [] # iterate, breaking into batches data.each do |row, i| # get the number of bytes in our string size += row.bytes.to_a.inject{|sum,x| sum + x} # if we've gone over the limit with this row, we # create a new batch and reset the size to 0 if size > self.batch_size ret << batch batch = [] size = 0 end # add our row to the current batch batch << row end # if we have leftover records, put them here ret << batch unless batch.empty? ret end
method_missing(m, *args, &block)
click to toggle source
proxy everything else to the client
# File lib/sailthru_batching_client.rb, line 104 def self.method_missing(m, *args, &block) self.establish_connection # why Sailthru, would you override "send" @client.__send__(m, *args, &block) end
send_file(file)
click to toggle source
send a file to sailthru
# File lib/sailthru_batching_client.rb, line 111 def self.send_file(file) i = 0 begin SailthruBatchingClient.api_post( :job, {"job" => "update", "file" => file}, "file" ) rescue Exception => e if i < 5 i += 1 retry else raise e end end end
write_temp_file(group, i)
click to toggle source
write a temp filte for a given group so that we can push the file to sailthru
# File lib/sailthru_batching_client.rb, line 131 def self.write_temp_file(group, i) name = "#{self.base_file_name}.#{i}" File.open(name, 'wb') do |f| group.compact.each do |row| f.puts(row) end end name end