Guest User

Untitled

a guest
Mar 16th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2. require 'rubygems'
  3. require 'ramaze'
  4. require 'active_record'
  5.  
  6. # model setup
  7. ActiveRecord::Base.establish_connection(
  8. :adapter => "postgresql",
  9. :host => "localhost",
  10. :username => "creata",
  11. :password => "creata",
  12. :database => "creata_dev"
  13. )
  14. # Image model
  15. class Image < ActiveRecord::Base
  16. class << self
  17. def s_find(id=:all, status='approved')
  18. self.find(id, :conditions => [ "status = ?", status ])
  19. end
  20. end
  21. end
  22.  
  23. # controller '/'
  24. class MainController < Ramaze::Controller
  25. # route handling for .xml (mainly requested by ActiveResource)
  26. Ramaze::Route[ %r!^/(.+)\.xml$! ] = "/%s"
  27.  
  28. # '/'
  29. def index; redirect R('/images'); end
  30.  
  31. # '/error' for error handling
  32. def error
  33. respond 'Error', 401
  34. end
  35. end
  36.  
  37. # controller '/images'
  38. class ImagesController < Ramaze::Controller
  39. # basic HTTP authentication
  40. # user: creata pass: creata_pas123
  41. LOGINS = ["creata:creata_pass123"].pack('m').strip unless defined? LOGINS
  42.  
  43. # require the aspect helper
  44. helper :aspect
  45.  
  46. # this block executes before all actions (hence the block name)
  47. before_all do
  48. response['WWW-Authenticate'] = %(Basic realm="Login Required")
  49. respond 'Unauthorized', 401 unless auth = request.env['HTTP_AUTHORIZATION'] and
  50. LOGINS.include? auth.split.last
  51. end
  52.  
  53. # '/images[/*(.xml)]' return xml for images table or id specific image
  54. def index(id=nil)
  55. @img = (id.nil? or id.empty?) ? Image.s_find : Image.s_find(id.to_s)
  56. respond @img.to_xml, 200, 'Content-Type' => 'text/xml' unless @img.nil?
  57. end
  58. end
  59.  
  60.  
  61. # run ramaze with: mongrel - port 3008
  62. Ramaze.start :adapter => :mongrel, :port => 3008
Add Comment
Please, Sign In to add comment