detecting bots by user-agent in rails

First i thought using the browser-gem and browser.bot? would give me the correct answer. But somehow it did not work. So i am now using this litte snippet to detect bots.


class BotDetection

  def self.bot?(agent)
    matches = nil
    matches = agent.match(/(facebook|postrank|voyager|twitterbot|googlebot|slurp|butterfly|pycurl|tweetmemebot|metauri|evrinid|reddit|digg|sitebot|msnbot|robot)/mi) if agent
    return (agent.nil? or matches)
   end

end

In my Base-Controller:


def bot_request?
  uAgent = request.env["HTTP_USER_AGENT"]
  if(uAgent.nil?)
    false
  else
    BotDetection.bot?(uAgent)
  end
end

A list of user-agents may be found at http://www.user-agents.org/

ActiveRecord disallow changes using unchangeable validation

Not allowing changes to be made to e.g. the login of the user only by the UI is in my eyes not enough.
The model should also ensure, that changes cannot be made once assigned.

So i tried to find an validation that does this for me.
But the once i found did not quite work as i expected.

So i wrote my own that i want to share:

class User < ActiveRecord::Base
  validates_unchangeable :login
end

unchangeable_validation.rb

require 'active_support/concern'
require 'active_model'
require 'active_record/base'

module UnchangeableValidation
  extend ActiveSupport::Concern

  class UnchangeableValidator < ActiveModel::EachValidator

    def initialize(options)
      super(options)
    end

    def validate_each(object, attribute, value)
      if(!object.new_record? && value.present?)
        abChanged = attribute.to_s + "_changed?"
        if(object.send(abChanged.to_sym))
          object.errors[attribute] << (options[:message] || "cannot be changed once assigned")
        end
      end
    end
 end

 module ClassMethods

   def validates_unchangeable(*attr_names)
     validates_with UnchangeableValidation::UnchangeableValidator, _merge_attributes(attr_names)
   end
 end
end

ActiveRecord::Base.send(:include, UnchangeableValidation)

unchangeable_validation_test.rb

require 'test_helper'

class UnchangeableValidationTest <  ActiveSupport::TestCase

  def test_login_unchangeable
    instance = User.new
    instance.login = "login"
    instance.save!
    instance.login = "changed"
    instance.save
    assert_equal instance.errors[:login].first, "cannot be changed once assigned"
  end

end

Squirrel SQL Client and SQLite Databases

Working with RubyOnRails i use sqlite in development.

In my dev-environment (windows) i used to use SQLiteBrowser to look into the DB. But the usability was not what i was used to.

So i tried to get Squirrel SQL Client to work with SQLite and had my problems integrating the two together.

Why?

Because i googled and googled and did not find a working answer.

In the end it was easy.

  1. Download Squirrel SQL Client http://squirrel-sql.sourceforge.net/
  2. Get this jdbc-Adapter for SQLite: https://bitbucket.org/xerial/sqlite-jdbc/overview
  3. Throw the lib in the lib-Directory of SQuirrel
  4. Startup Squirrel
  5. Create new Driver
    1. Name: SQLite
    2. Example-Url: jdbc:sqlite:<PATH>/mydatabase.db
    3. Website-Url: https://bitbucket.org/xerial/sqlite-jdbc/wiki/Usage
    4. Class-Name: org.sqlite.JDBC
  6. Add Alises for your DB and connect to them

Manually Creating HeapDumps in Java honoring +XX:HeapDumpPath JVM Option

+XX:HeapDumpPath is a JVM-Option that is honored by the HeapDumpOnOutOfMemory JVM-Option.

With it we can define where HeapDumps are written to the Disk.

Why do we need this?

Well, running multiple servers we may want to have them dumped centrally on a mapped network drive specifically for logfiles or dumps. Also maybe we sized the Partition for the JVM-Process too small so that a dump will not be able to be written into it (ok, disk space is cheap, even with a raid, but often we save money at the wrong end).

But if i trigger an heapdump directly (like with jconsole) i often only write the name of the dump into it or want to have it generated. Normally those dumps will now be written into the working directory of the jvm.

To avoid this, we can use the following Code to have it honored and registered as our own MBean that we will use from now on.

„Manually Creating HeapDumps in Java honoring +XX:HeapDumpPath JVM Option“ weiterlesen

delayed_job and localization (i18n)

delayed_job is a nice gem for executing work in the background in Ruby On Rails.

But what happend to me is that mails did not get send correctly. I was expecting to get them send in german and not english (default locale).

What happend?

„delayed_job and localization (i18n)“ weiterlesen

1blu VServer Neuinstallation und EMails

Mails, das ganz alltägliche Kommunikationsmedium.

Blöd wenn keine Mails ankommen, oder?

Nach Neuinstallation meines 1blu VServers mittels Template sind keine Mails mehr angekommen. Von der Konfiguration her nutzte ich keinen eigenen Mailserver, sondern den default (globaler von 1blu).

Nur warum sind dann Mails die über WordPress gesendet wurden nicht angekommen?

 

Ursache: Das Template mit Plesk hat leider einen kleinen Haken aktiviert, der dafür sorgt dass der VServer selbst sich als Mailserver sieht. Und weil keine Mailaddressen dort eingerichtet waren (warum auch) droppte er diese.

 

Lösung: Im Plesk Control Panel zu folgendem Eintrag navigieren:

E-Mail => E-Mail Einstellungen

Hier den Haken “E-Mail-Dienst auf Domain aktivieren” deaktivieren.

 

Dann schlägt auch nicht:

Vorgehensweise bei E-Mails an nicht-vorhandene Benutzer

mit Standardwert zurückweisen zu.