Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env ruby
- require 'rubygems'
- require 'ramaze'
- require 'active_record'
- # model setup
- ActiveRecord::Base.establish_connection(
- :adapter => "postgresql",
- :host => "localhost",
- :username => "creata",
- :password => "creata",
- :database => "creata_dev"
- )
- # Image model
- class Image < ActiveRecord::Base
- class << self
- def s_find(id=:all, status='approved')
- self.find(id, :conditions => [ "status = ?", status ])
- end
- end
- end
- # controller '/'
- class MainController < Ramaze::Controller
- # route handling for .xml (mainly requested by ActiveResource)
- Ramaze::Route[ %r!^/(.+)\.xml$! ] = "/%s"
- # '/'
- def index; redirect R('/images'); end
- # '/error' for error handling
- def error
- respond 'Error', 401
- end
- end
- # controller '/images'
- class ImagesController < Ramaze::Controller
- # basic HTTP authentication
- # user: creata pass: creata_pas123
- LOGINS = ["creata:creata_pass123"].pack('m').strip unless defined? LOGINS
- # require the aspect helper
- helper :aspect
- # this block executes before all actions (hence the block name)
- before_all do
- response['WWW-Authenticate'] = %(Basic realm="Login Required")
- respond 'Unauthorized', 401 unless auth = request.env['HTTP_AUTHORIZATION'] and
- LOGINS.include? auth.split.last
- end
- # '/images[/*(.xml)]' return xml for images table or id specific image
- def index(id=nil)
- @img = (id.nil? or id.empty?) ? Image.s_find : Image.s_find(id.to_s)
- respond @img.to_xml, 200, 'Content-Type' => 'text/xml' unless @img.nil?
- end
- end
- # run ramaze with: mongrel - port 3008
- Ramaze.start :adapter => :mongrel, :port => 3008
Add Comment
Please, Sign In to add comment