Created
June 25, 2012 18:42
-
-
Save jcuffe/2990451 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SOAPReport | |
def initialize(options) | |
# initialize instance variables | |
@path = { | |
:pdf => File.join BASE_REPORT_PATH, '.pdf', | |
:xls => File.join BASE_REPORT_PATH, '.xls' | |
} | |
@options = options | |
@me = self.class.name | |
@response = soap_request | |
#if the soap request was successful, attempt to build documents | |
unless @response.nil? | |
begin | |
# write a document for each filetype in @path | |
write_documents(@path.keys) | |
rescue | |
return false | |
end | |
end | |
return true | |
end | |
# uses @options to generate a soap request; returns the soap response | |
def soap_request | |
# set the date of the request | |
date = Date.today | |
# make the request for the given report | |
soap_client = LolSoap::Client.new(ACS_SOAP[:definitions][:report_data_service]) | |
soap_request = soap_client.request('GetMonthlyReportData') | |
soap_request.body do |b| | |
b.ReportName @options[:report_name] | |
b.ReportYear date.year | |
b.ReportMonth date.strftime('%m') | |
end | |
# initialize the http client | |
request_uri = URI.parse(soap_request.url) | |
http_client = Patron::Session.new | |
http_client.timeout = 10 | |
http_client.base_url = request_uri.host | |
# magically turn a POST into parsed data. keys => (r)aw, (s)oap, (p)arsed | |
response[:r] = http_client.post(request_uri.path, soap_request.content, soap_request.headers).body | |
response[:s] = soap_client.response(soap_request, response[:r]) | |
response[:p] = JSON.parse(response[:s].body_hash[@options[:report_name] + 'Result']) | |
rescue | |
# alert caller that something has gone awry | |
response[:p] = nil | |
ensure | |
return response[:p] | |
end | |
# accepts an array of document types to output. delegates document creation to | |
# classes by adding filetype to the classname of invoking class | |
def write_documents(types) | |
types.each do |type| | |
klass = @me + type.to_s.capitalize | |
klass.constantize.new(response, @path[type], @options) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment