An open source, stand-alone, customisable public spending data web app.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

121 line
3.2 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.gsub(/(\d)(?=(\d{3})+$)/,'\1,')
  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.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/?' do
  80. @services = Service.all( :order => ['name'] )
  81. haml :services
  82. end
  83. get '/payments/:id' do
  84. @payment = Payment.get(params[:id])
  85. haml :payment
  86. end
  87. get '/error' do
  88. haml :error
  89. end
  90. get '/about' do
  91. haml :about
  92. end
  93. not_found do
  94. haml :not_found
  95. end