An open source, stand-alone, customisable public spending data web app.
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 

100 řádky
2.6 KiB

  1. require 'rubygems'
  2. require 'sinatra'
  3. require 'sinatra-helpers/haml/partials'
  4. require 'haml'
  5. require 'lib/models'
  6. get '/' do
  7. @directorates = Directorate.all( :order => ['name'] )
  8. # @payments_count = Payment.all.size
  9. # @suppliers_count = Supplier.all.size
  10. @results = repository(:default).adapter.query("SELECT COUNT(*) FROM payments")
  11. @payments_count = @results[0]
  12. @results = repository(:default).adapter.query("SELECT COUNT(*) FROM suppliers")
  13. @suppliers_count = @results[0]
  14. @results = repository(:default).adapter.query("SELECT COUNT(*) FROM services")
  15. @services_count = @results[0]
  16. haml :home
  17. end
  18. get '/directorates/:slug' do
  19. @directorate = Directorate.first(:slug => params[:slug])
  20. @total = @directorate.payments.sum(:amount)
  21. haml :directorate
  22. end
  23. get '/suppliers/:slug.csv' do
  24. @supplier = Supplier.first(:slug => params[:slug])
  25. headers "Content-Disposition" => "attachment;filename=supplier-#{@supplier.slug}.csv",
  26. "Content-Type" => "application/octet-stream"
  27. result = "Date,Ref.,URL,Trans No,Directorate,Service,Amount ex. VAT\n"
  28. for payment in @supplier.payments
  29. result += "#{payment.d.strftime("%d %b %Y")},#{payment.id},#{payment.url},#{payment.trans_no},\"#{payment.directorate.name}\",#{payment.service.name},#{sprintf("%0.2f", payment.amount)}\n"
  30. end
  31. result
  32. end
  33. get '/suppliers/:slug' do
  34. @supplier = Supplier.first(:slug => params[:slug])
  35. @total = @supplier.payments.sum(:amount)
  36. haml :supplier
  37. end
  38. get '/suppliers/?' do
  39. @suppliers = Supplier.all( :order => ['name'] )
  40. haml :suppliers
  41. end
  42. get '/services/:slug.csv' do
  43. @service = Service.first(:slug => params[:slug])
  44. headers "Content-Disposition" => "attachment;filename=service-#{@service.slug}.csv",
  45. "Content-Type" => "application/octet-stream"
  46. result = "Date,Ref.,URL,Trans No,Directorate,Supplier,Amount ex. VAT\n"
  47. for payment in @service.payments
  48. result += "#{payment.d.strftime("%d %b %Y")},#{payment.id},#{payment.url},#{payment.trans_no},\"#{payment.directorate.name}\",#{payment.supplier.name},#{sprintf("%0.2f", payment.amount)}\n"
  49. end
  50. result
  51. end
  52. get '/services/:slug' do
  53. @service = Service.first(:slug => params[:slug])
  54. @total = @service.payments.sum(:amount)
  55. @count = @service.payments.size
  56. @avg = @service.payments.avg(:amount)
  57. @max = @service.payments.max(:amount)
  58. @min = @service.payments.min(:amount)
  59. haml :service
  60. end
  61. get '/services/?' do
  62. @services = Service.all( :order => ['name'] )
  63. haml :services
  64. end
  65. get '/payments/:id' do
  66. @payment = Payment.get(params[:id])
  67. haml :payment
  68. end
  69. get '/error' do
  70. haml :error
  71. end
  72. get '/about' do
  73. haml :about
  74. end
  75. not_found do
  76. haml :not_found
  77. end