An open source, stand-alone, customisable public spending data web app.
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 

129 rindas
3.4 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. @total = @service.payments.sum(:amount)
  82. haml :servicepayments
  83. end
  84. get '/services/?' do
  85. @services = Service.all( :order => ['name'] )
  86. haml :services
  87. end
  88. get '/payments/:id' do
  89. @payment = Payment.get(params[:id])
  90. haml :payment
  91. end
  92. get '/error' do
  93. haml :error
  94. end
  95. get '/about' do
  96. haml :about
  97. end
  98. not_found do
  99. haml :not_found
  100. end