An open source, stand-alone, customisable public spending data web app.
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 

140 righe
3.8 KiB

  1. require 'rubygems'
  2. require 'sinatra'
  3. require 'sinatra-helpers/haml/partials'
  4. require 'haml'
  5. require 'lib/models'
  6. helpers do
  7. def commify(amount)
  8. amount.to_s.reverse.gsub(/(\d\d\d)(?=\d)(?!\d*\.)/,'\1,').reverse
  9. end
  10. end
  11. get '/' do
  12. @directorates = Directorate.all( :order => ['name'] )
  13. @results = repository(:default).adapter.query("SELECT COUNT(*) FROM payments")
  14. @payments_count = @results[0]
  15. @results = repository(:default).adapter.query("SELECT COUNT(*) FROM suppliers")
  16. @suppliers_count = @results[0]
  17. @results = repository(:default).adapter.query("SELECT COUNT(*) FROM services")
  18. @services_count = @results[0]
  19. haml :home
  20. end
  21. get '/directorates/:slug' do
  22. @directorate = Directorate.first(:slug => params[:slug])
  23. haml :directorate
  24. end
  25. get '/suppliers/:slug.csv' do
  26. @supplier = Supplier.first(:slug => params[:slug])
  27. headers "Content-Disposition" => "attachment;filename=supplier-#{@supplier.slug}.csv",
  28. "Content-Type" => "application/octet-stream"
  29. result = "Date,Ref.,URL,Directorate,Service,Amount ex. VAT\n"
  30. for payment in @supplier.payments
  31. result += "#{payment.d.strftime("%d %b %Y")},#{payment.id},#{payment.url},\"#{payment.service.directorate.name}\",#{payment.service.name},#{sprintf("%0.2f", payment.amount)}\n"
  32. end
  33. result
  34. end
  35. get '/suppliers/:slug' do
  36. @supplier = Supplier.first(:slug => params[:slug])
  37. @total = @supplier.payments.sum(:amount)
  38. @count = @supplier.payments.size
  39. @avg = @supplier.payments.avg(:amount)
  40. @max = @supplier.payments.max(:amount)
  41. @min = @supplier.payments.min(:amount)
  42. haml :supplier
  43. end
  44. get '/suppliers/?' do
  45. @suppliers = Supplier.all( :order => ['name'] )
  46. haml :suppliers
  47. end
  48. get '/services/:slug/payments.csv' do
  49. @service = Service.first(:slug => params[:slug])
  50. headers "Content-Disposition" => "attachment;filename=service-#{@service.slug}.csv",
  51. "Content-Type" => "application/octet-stream"
  52. result = "Date,Ref.,URL,Directorate,Supplier,Amount ex. VAT\n"
  53. for payment in @service.payments
  54. result += "#{payment.d.strftime("%d %b %Y")},#{payment.id},#{payment.url},\"#{payment.service.directorate.name}\",#{payment.supplier.name},#{sprintf("%0.2f", payment.amount)}\n"
  55. end
  56. result
  57. end
  58. get '/services/:slug.json' do
  59. @service = Service.first(:slug => params[:slug])
  60. headers "Content-Type" => "application/json"
  61. @service.to_json(:relationships => { :payments => { :include => :all }, :directorate => { :include => :all } })
  62. end
  63. get '/services/:slug' do
  64. @service = Service.first(:slug => params[:slug])
  65. @total = @service.payments.sum(:amount)
  66. @count = @service.payments.size
  67. @avg = @service.payments.avg(:amount)
  68. @max = @service.payments.max(:amount)
  69. @min = @service.payments.min(:amount)
  70. @results = repository(:default).adapter.query("
  71. SELECT s.name AS supplier_name, s.slug AS supplier_slug, SUM(p.amount) AS total
  72. FROM payments p, suppliers s
  73. WHERE p.supplier_id = s.id
  74. AND p.service_id = #{@service.id}
  75. GROUP BY s.name, s.slug
  76. ORDER BY total DESC")
  77. haml :service
  78. end
  79. get '/services/:slug/payments' do
  80. @service = Service.first(:slug => params[:slug])
  81. @payments = Payment.all(:service_id => @service.id, :amount.gte => 0, :order => [ 'd' ])
  82. @total = @payments.sum(:amount)
  83. haml :servicepayments
  84. end
  85. get '/services/:slug/paymentsdetail' do
  86. @service = Service.first(:slug => params[:slug])
  87. min = 500
  88. if params[:min].to_i > 0
  89. min = params[:min].to_i
  90. end
  91. @payments = Payment.all(:service_id => @service.id, :amount.gte => min, :order => [ 'd' ])
  92. @total = @payments.sum(:amount)
  93. haml :servicepaymentsdetail, :layout => false
  94. end
  95. get '/services/?' do
  96. @services = Service.all( :order => ['name'] )
  97. haml :services
  98. end
  99. get '/payments/:id' do
  100. @payment = Payment.get(params[:id])
  101. haml :payment
  102. end
  103. get '/error' do
  104. haml :error
  105. end
  106. get '/about' do
  107. haml :about
  108. end
  109. not_found do
  110. haml :not_found
  111. end