Sha256: 24815f88de92625a1f31f47dee8fd6df2b8e18a06fd1e15d658de982da05edc7
Contents?: true
Size: 1.95 KB
Versions: 4
Compression:
Stored size: 1.95 KB
Contents
%w{ rubygems active_record rack }.each { |m| require m } module Murlsh class UrlServer def initialize(config, db) @config = config @db = db ActiveRecord::Base.default_timezone = :utc Dir['plugins/*.rb'].each { |p| load p } end def get(req) resp = Murlsh::XhtmlResponse.new resp.set_content_type(req.env['HTTP_ACCEPT'], req.env['HTTP_USER_AGENT']) last_db_update = File::Stat.new(@config['db_file']).mtime resp['Last-Modified'] = last_db_update.httpdate resp['ETag'] = "#{last_db_update.to_i}#{req.params.sort}" resp.body = Murlsh::UrlBody.new(@config, @db, req) resp end def post(req) resp = Rack::Response.new url = req.params['url'] unless url.empty? auth = req.params['auth'] if user = auth.empty? ? nil : Murlsh::Auth.new( @config.fetch('auth_file')).auth(auth) ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => @config.fetch('db_file')) content_type = Murlsh.get_content_type(url) mu = Murlsh::Url.new do |u| u.time = Time.now.gmtime u.url = url u.email = user[:email] u.name = user[:name] u.title = Murlsh.get_title(url, :content_type => content_type) u.content_type = content_type end mu.save Murlsh::Plugin.hooks('add_post') { |p| p.run(@config) } resp['Content-Type'] = 'application/json' resp.set_cookie('auth', :expires => Time.mktime(2015, 6, 22), :path => '/', :value => auth) resp.body = [mu].to_json else resp.status = 403 resp['Content-Type'] = 'text/plain' resp.write('Permission denied') end else resp.status = 500 resp['Content-Type'] = 'text/plain' resp.write('No url') end resp end end end
Version data entries
4 entries across 4 versions & 1 rubygems
Version | Path |
---|---|
murlsh-0.2.4 | lib/murlsh/url_server.rb |
murlsh-0.2.3 | lib/murlsh/url_server.rb |
murlsh-0.2.2 | lib/murlsh/url_server.rb |
murlsh-0.2.1 | lib/murlsh/url_server.rb |