Last active
December 14, 2015 18:59
-
-
Save EtienneDepaulis/5132967 to your computer and use it in GitHub Desktop.
Proxy pour télécharger les factures de Facturation.pro
This file contains 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
# /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 |
This file contains 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
mount FacturationProxy => '/factures' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment