An open source, stand-alone, customisable public spending data web app.
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

109 lines
2.9 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. @results = repository(:default).adapter.query("SELECT COUNT(*) FROM payments")
  9. @payments_count = @results[0]
  10. @results = repository(:default).adapter.query("SELECT COUNT(*) FROM suppliers")
  11. @suppliers_count = @results[0]
  12. @results = repository(:default).adapter.query("SELECT COUNT(*) FROM services")
  13. @services_count = @results[0]
  14. haml :home
  15. end
  16. get '/directorates/:slug' do
  17. @directorate = Directorate.first(:slug => params[:slug])
  18. haml :directorate
  19. end
  20. get '/suppliers/:slug.csv' do
  21. @supplier = Supplier.first(:slug => params[:slug])
  22. headers "Content-Disposition" => "attachment;filename=supplier-#{@supplier.slug}.csv",
  23. "Content-Type" => "application/octet-stream"
  24. result = "Date,Ref.,URL,Directorate,Service,Amount ex. VAT\n"
  25. for payment in @supplier.payments
  26. result += "#{payment.d.strftime("%d %b %Y")},#{payment.id},#{payment.url},\"#{payment.service.directorate.name}\",#{payment.service.name},#{sprintf("%0.2f", payment.amount)}\n"
  27. end
  28. result
  29. end
  30. get '/suppliers/:slug' do
  31. @supplier = Supplier.first(:slug => params[:slug])
  32. @total = @supplier.payments.sum(:amount)
  33. @count = @supplier.payments.size
  34. @avg = @supplier.payments.avg(:amount)
  35. @max = @supplier.payments.max(:amount)
  36. @min = @supplier.payments.min(:amount)
  37. haml :supplier
  38. end
  39. get '/suppliers/?' do
  40. @suppliers = Supplier.all( :order => ['name'] )
  41. haml :suppliers
  42. end
  43. get '/services/:slug.csv' do
  44. @service = Service.first(:slug => params[:slug])
  45. headers "Content-Disposition" => "attachment;filename=service-#{@service.slug}.csv",
  46. "Content-Type" => "application/octet-stream"
  47. result = "Date,Ref.,URL,Directorate,Supplier,Amount ex. VAT\n"
  48. for payment in @service.payments
  49. result += "#{payment.d.strftime("%d %b %Y")},#{payment.id},#{payment.url},\"#{payment.service.directorate.name}\",#{payment.supplier.name},#{sprintf("%0.2f", payment.amount)}\n"
  50. end
  51. result
  52. end
  53. get '/services/:slug' do
  54. @service = Service.first(:slug => params[:slug])
  55. @total = @service.payments.sum(:amount)
  56. @count = @service.payments.size
  57. @avg = @service.payments.avg(:amount)
  58. @max = @service.payments.max(:amount)
  59. @min = @service.payments.min(:amount)
  60. @results = repository(:default).adapter.query("
  61. SELECT s.name AS supplier_name, s.slug AS supplier_slug, SUM(p.amount) AS total
  62. FROM payments p, suppliers s
  63. WHERE p.supplier_id = s.id
  64. AND p.service_id = #{@service.id}
  65. GROUP BY s.name, s.slug
  66. ORDER BY total DESC")
  67. haml :service
  68. end
  69. get '/services/?' do
  70. @services = Service.all( :order => ['name'] )
  71. haml :services
  72. end
  73. get '/payments/:id' do
  74. @payment = Payment.get(params[:id])
  75. haml :payment
  76. end
  77. get '/error' do
  78. haml :error
  79. end
  80. get '/about' do
  81. haml :about
  82. end
  83. not_found do
  84. haml :not_found
  85. end