Skip to content

Instantly share code, notes, and snippets.

@MarkCheshire
Created August 4, 2014 09:30
Show Gist options
  • Save MarkCheshire/da7c0be615cd8a16bdf3 to your computer and use it in GitHub Desktop.
Save MarkCheshire/da7c0be615cd8a16bdf3 to your computer and use it in GitHub Desktop.
3scale utility to export invoice data into an XML file
require 'restclient'
require 'nokogiri'
# Access details to the 3scale API are grabbed from environment variables for security
DOMAIN = ENV['DOMAIN'] # Admin domain in the form: mydomain-admin.3scale.net
PROVIDER_KEY = ENV['PROVIDER_KEY'] # From your 3scale account page in the admin console
def invoices_for_month(month)
done = false
res = String.new
page = 1
while !done # iterate over all pages for this month
puts "Page #{page}" # this can take a while so give updates on where we are
url = "https://#{DOMAIN}/api/invoices.xml?provider_key=#{PROVIDER_KEY}&month=#{month}&page=#{page}&per_page=20"
page += 1
response = RestClient.get url
raise Exception.new("Wrong response code (#{response.code}) in request #{url}") if response.code!=200
document = Nokogiri::XML(response.to_str) # this could also be refactored to use Regex and eliminate use of Nokogiri
done = document.xpath("//pagination/@current_page").text == document.xpath("//pagination/@total_pages").text
response_string=response.to_str.delete("\n") # strip any newlines which would screw up the .split operation
# we cannot just concatenate results from the pagination because the metadata would mess up the xml schema
# strip out the metadata, from the raw invoice content
parsed_response=response_string.split(/(<\?xml version=\"1.0\" encoding=\"UTF-8\"\?><invoices version=\"1.0\"><pagination.*?\/>)(.*)(<\/invoices>)\z/)
invoice_content= parsed_response[2] # grab the raw invoice content from the .split operation
res << invoice_content # concatenate with results from previous pages
end
return res
end
def save_month(month, invoices) # option for intermediate save of a single month of results in case of any network issues
File.open( "#{month}.xml" , "w" ) do |file|
file.puts "<invoices>#{invoices}</invoices>"
end
end
##----------------------------------------------------------
##--Export invoice date from 3scale for multiple months-----
##--Save to an XML file which can easily be read------------
##--into Microsoft Excel------------------------------------
##----------------------------------------------------------
puts "May" # give a progress update since this will take a while
invoices_may = invoices_for_month '2014-05' # get invoices for desired month
save_month "may", invoices_may # do an intermediate save
puts "Jun"
invoices_jun = invoices_for_month '2014-06'
save_month "jun", invoices_jun
puts "Jul"
invoices_jul = invoices_for_month '2014-07'
save_month "jul", invoices_jul
puts "Done"
# after all months are processed contactenate them and write them out to file as XML
# note we need to create the root element for valid XML
File.open( "output.xml" , "w" ) do |file|
file.puts "<invoices>#{invoices_may}#{invoices_jun}#{invoices_jul}</invoices>"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment