webapp.rb 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. require 'sinatra'
  2. require 'data_mapper'
  3. require 'json'
  4. DataMapper::setup(:default, "sqlite3://hlbb/hacklabbo.db")
  5. class Entry
  6. include DataMapper::Resource
  7. property :id, Serial
  8. property :open, Boolean
  9. property :date, DateTime
  10. end
  11. DataMapper.finalize
  12. Entry.auto_upgrade!
  13. set :secret_token, "unsecure_token"
  14. get '/' do
  15. entry = Entry.all(limit: 1, order: [ :id.desc ])
  16. state = {
  17. open: false,
  18. date: nil
  19. }
  20. unless entry.empty?
  21. state[:open] = entry[0][:open]
  22. state[:date] = entry[0][:date]
  23. end
  24. erb :index, locals: { state: state }
  25. end
  26. get '/rss.xml' do
  27. entries = Entry.all(limit: 10, order: [ :id.desc ])
  28. erb :rss, locals: { entries: entries }
  29. end
  30. get '/hacklabbo/state.json' do
  31. entry = Entry.all(limit: 1, order: [ :id.desc ])
  32. state = {
  33. open: false,
  34. date: nil
  35. }
  36. unless entry.empty?
  37. state[:open] = entry[0][:open]
  38. state[:date] = entry[0][:date]
  39. end
  40. content_type :json
  41. state.to_json
  42. end
  43. get '/hacklabbo/space.json' do
  44. entry = Entry.all(limit: 1, order: [ :id.desc ])
  45. state = {
  46. open: false,
  47. date: nil
  48. }
  49. unless entry.empty?
  50. state[:open] = entry[0][:open]
  51. state[:date] = entry[0][:date]
  52. end
  53. content_type :json
  54. {
  55. api: "0.13",
  56. space: "hacklabbo",
  57. logo: "http://thecatapi.com/api/images/get?format=src&type=jpg",
  58. url: "https://hacklabbo.indivia.net",
  59. location: {
  60. address: "XM24, Via Aristotile Fioravanti 24, Bologna",
  61. lat: 44.512243,
  62. lon: 11.341099
  63. },
  64. contact: {
  65. ml: "hacklabbo@indivia.net"
  66. },
  67. state: {
  68. open: state[:open]
  69. }
  70. }.to_json
  71. end
  72. get '/hacklabbo/open/:token' do |token|
  73. if token == settings.secret_token
  74. entry = Entry.create(open: true, date: DateTime.now)
  75. if entry.saved?
  76. 200
  77. else
  78. 500
  79. end
  80. else
  81. 403
  82. end
  83. end
  84. get '/hacklabbo/close/:token' do |token|
  85. if token == settings.secret_token
  86. entry = Entry.create(open: false, date: DateTime.now)
  87. if entry.saved?
  88. 200
  89. else
  90. 500
  91. end
  92. else
  93. 403
  94. end
  95. end