#!/usr/bin/ruby require 'base64' require 'net/http' require 'openssl' ACCESS_KEY_ID="xxxxxxxxxxxxxxxxxxxx" SECRET_ACCESS_KEY="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" # Downloads the next document from PDF Alchemy. # Returns the documentId (String) on success, or nil on failure. def downloadNext # Generate the HMAC signature. date=Time.now.getutc.strftime('%a, %d %b %Y %H:%M:%S GMT') request="/#{ACCESS_KEY_ID}/outqueue/next" clearText="GET\n#{ACCESS_KEY_ID}\n#{date}\n#{request}" signature=Base64.encode64(OpenSSL::HMAC.digest(OpenSSL::Digest::Digest.new('sha1'),SECRET_ACCESS_KEY,clearText)).strip # Request the next document from PDF Alchemy. http=Net::HTTP.new('www.pdfalchemy.com') http.start do req=Net::HTTP::Get.new(request) req['Date']=date req['X-PDF-Alchemy-Signature']=signature resp=http.request(req) if resp.is_a?(Net::HTTPSuccess) then filename=resp['content-disposition'].match(/filename="([^"].*)"/)[1] open(filename,'wb') {|io|io.write(resp.body)} puts "Downloaded '#{filename}'." return resp['X-PDF-Alchemy-ID'] else puts "#{resp.code}: #{resp.message}: #{resp.body}" return nil end end end # Removes documentId from the PDF Alchemy outqueue. def deleteDocument(documentId) # Generate the HMAC signature. date=Time.now.getutc.strftime('%a, %d %b %Y %H:%M:%S GMT') request="/#{ACCESS_KEY_ID}/outqueue/#{URI::escape(documentId)}" clearText="DELETE\n#{ACCESS_KEY_ID}\n#{date}\n#{request}" signature=Base64.encode64(OpenSSL::HMAC.digest(OpenSSL::Digest::Digest.new('sha1'),SECRET_ACCESS_KEY,clearText)).strip # Remove the document from PDF Alchemy. http=Net::HTTP.new('www.pdfalchemy.com') http.start do req=Net::HTTP::Delete.new(request) req['Date']=date req['X-PDF-Alchemy-Signature']=signature resp=http.request(req) if resp.is_a?(Net::HTTPSuccess) then puts "Deleted '#{documentId}'" else puts "#{resp.code}: #{resp.message}: #{resp.body}" end end end while (documentId=downloadNext) != nil deleteDocument(documentId) end