forms 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #!/usr/bin/ruby
  2. #
  3. # This file is part of centurio.work/ing/commands.
  4. #
  5. # centurio.work/ing/commands is free software: you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or (at your
  8. # option) any later version.
  9. #
  10. # centurio.work/ing/commands is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
  13. # Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License along with
  16. # centurio.work/ing/commands (file COPYING in the main directory). If not, see
  17. # <http://www.gnu.org/licenses/>.
  18. require 'rubygems'
  19. require 'json'
  20. require 'xml/smart'
  21. require 'riddl/server'
  22. require 'fileutils'
  23. require 'typhoeus'
  24. # process:
  25. # https://centurio.work/customers/evva/flow/?monitor=https://centurio.work/flow-test/engine/729/
  26. class NewInstance < Riddl::Implementation
  27. def response
  28. entries = Dir.entries(File.join('data')).size
  29. Dir.mkdir(File.join('data',entries.to_s)) rescue nil
  30. Riddl::Parameter::Complex.new('text','text/plain',entries.to_s)
  31. end
  32. end
  33. class Index < Riddl::Implementation
  34. def response
  35. Riddl::Parameter::Complex.new('url','text/html',File.open(File.join(__dir__,'template','index.html')))
  36. end
  37. end
  38. class Builder < Riddl::Implementation
  39. def response
  40. Riddl::Parameter::Complex.new('url','text/html',File.open(File.join(__dir__,'template','builder.html')))
  41. end
  42. end
  43. class DisplayForm < Riddl::Implementation
  44. def response
  45. #Riddl::Parameter::Complex.new('url','text/html',File.open(File.join('data',@r[0],'form_min.html')))
  46. Riddl::Parameter::Complex.new('url','text/html',File.open(File.join(__dir__,'template','form.html')))
  47. end
  48. end
  49. class SaveForm < Riddl::Implementation
  50. def response
  51. File.write(File.join('data',@r[0],'form.json'),@p[0].value.read)
  52. end
  53. end
  54. class SaveHtmlForm < Riddl::Implementation
  55. def response
  56. file = File.read(File.join(__dir__,'template','form_min.html'))
  57. File.write(File.join('data',@r[0],'form_min.html'),(file.sub! '<!--FormComesHere-->', @p[0].value.read))
  58. end
  59. end
  60. class GetJson < Riddl::Implementation #{{{
  61. def response
  62. fname = File.join('data',@r[-2],'form.json')
  63. if File.exists? fname
  64. Riddl::Parameter::Complex.new('value','application/json',File.read(fname))
  65. else
  66. @status = 404
  67. end
  68. end
  69. end #}}}
  70. server = Riddl::Server.new(File.join(__dir__,'/forms.xml'), :host => 'localhost') do |opts|
  71. accessible_description true
  72. cross_site_xhr true
  73. on resource do
  74. run Index if get
  75. run NewInstance if post
  76. on resource do |r|
  77. run DisplayForm if get
  78. on resource 'builder' do
  79. run Builder if get
  80. run SaveForm if post 'form'
  81. run SaveHtmlForm if post 'htmlform'
  82. end
  83. on resource 'json' do
  84. run GetJson if get
  85. end
  86. end
  87. end
  88. end.loop!