Sha256: 5af3135680f98ec367d0cec257e70bcb76d7543ccc1037e8ed4cf0319d26eef8
Contents?: true
Size: 950 Bytes
Versions: 3
Compression:
Stored size: 950 Bytes
Contents
require 'rack' require 'murlsh' module Murlsh # Redirect to a random url from the database. class RandomServer def initialize(config); @config = config; end # Redirect to a random url from the database optionally matching a query. # # Redirect to root url if no urls match. def get(req) if choice = random_url(Murlsh::SearchConditions.new(req['q']).conditions) url = choice.url else url = config.fetch('root_url') end resp = Rack::Response.new("<a href=\"#{url}\">#{url}</a>") resp.redirect(url) resp end # Select a random url from the database optionally matching a query. # # Return nil if no urls match. def random_url(conditions=[]) count = Murlsh::Url.count(:conditions => conditions) if count > 0 Murlsh::Url.first(:conditions => conditions, :offset => rand(count)) end end attr_reader :config end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
murlsh-1.6.1 | lib/murlsh/random_server.rb |
murlsh-1.6.0 | lib/murlsh/random_server.rb |
murlsh-1.5.0 | lib/murlsh/random_server.rb |