Skip to content

Instantly share code, notes, and snippets.

@EtienneDepaulis
Last active December 14, 2015 18:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EtienneDepaulis/5132967 to your computer and use it in GitHub Desktop.
Save EtienneDepaulis/5132967 to your computer and use it in GitHub Desktop.
Proxy pour télécharger les factures de Facturation.pro
# /lib/facturation_proxy.rb
# Exemple : /factures?api_id=67863
require 'net/http'
class FacturationProxy
def self.call(env)
request = Rack::Request.new(env)
params = request.params
session = request.session
user = User.find(session['warden.user.user.key'][1].first)
invoice = user.company.invoices.find_by_api_id(params["api_id"]) if user
if invoice && invoice.url
basename = File.basename(invoice.url)
path_info = "/firms/#{ENV['FACTURATION_FIRM_ID']}/invoices/#{basename}"
Net::HTTP.start("facturation.pro", :use_ssl => true) {|http|
req = Net::HTTP::Get.new(path_info)
req.basic_auth ENV['FACTURATION_LOGIN'], ENV['FACTURATION_PASSWORD']
response = http.request(req)
@response = response
@server_response = response.body
@server_code = response.code
@server_content_type = response.content_type
}
[@server_code, {"Content-Type" => @server_content_type, "Content-Disposition" => "attachment;filename=#{basename}"}, [@server_response]]
else
[404, {"Content-Type" => "text/html"}, ["Not Found"]]
end
end
end
mount FacturationProxy => '/factures'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment