class Akamai WSDL_URL = 'http://ccuapi.akamai.com/ccuapi-axis.wsdl' CONFIG_PATH = '~/.akamai_config.yml' attr_accessor :cachecontrol_username, :cachecontrol_password attr_accessor :netstorage_username, :netstorage_password def initialize raise ArgumentError, "Config file (#{CONFIG_PATH}) could not be found" unless File.exists?(CONFIG_PATH) config = File.open(CONFIG_PATH) { |yf| YAML::load(yf) } cachecontrol_username = config['cache_control']['username'] cachecontrol_password = config['cache_control']['password'] netstorage_username = config['netstorage']['username'] netstorage_password = config['netstorage']['password'] netstorage_ftp_host = config['netstorage']['ftp_host'] netstorage_public_host = config['netstorage']['public_host'] netstorage_basedir = config['netstorage']['basedir'] end def self.purge(*urls) driver = SOAP::WSDLDriverFactory.new(wsdl).create_rpc_driver driver.options['protocol.http.ssl_config.verify_mode'] = OpenSSL::SSL::VERIFY_NONE driver.options["protocol.http.basic_auth"] << [WSDL_URL, cachecontrol_username, cachecontrol_password] result = driver.purgeRequest(cachecontrol_username, cachecontrol_password, '', [], urls) return result.resultCode == '100' end def self.put(location, filename) tempfile = Tempfile.open(filename) {|f| f.write(yield) } puts "File generated for #{filename}." ftp = Net::FTP::new(netstorage_ftp_host) ftp.passive = true ftp.login(netstorage_username, netstorage_password) ftp.chdir(netstorage_basedir) if netstorage_basedir ftp.chdir(location) ftp.put(tempfile, "#{filename}.new") ftp.delete(filename) unless ftp.ls(filename) ftp.rename("#{filename}.new", filename) ftp.close puts "Akamai upload completed for #{filename}." tempfile.delete puts "Generated file deleted from tmp." puts "Sending purge request" purge_result = Akamai.purge("http://#{netstorage_public_host}/#{location}/#{filename}") puts "Purge request #{ purge_result ? 'was successful' : 'failed' }." end end