Sha256: cd19d206eb88c30b1bbf472fd099d7e7c49c1b91386bcbbd255a16438df474f1

Contents?: true

Size: 1.78 KB

Versions: 1

Compression:

Stored size: 1.78 KB

Contents

module RobotCatcher
  module Models
    extend ActiveSupport::Concern

    module ClassMethods
      def robot_catch(*params)
        @@rc_params = params

        self.class_eval do
          def robot?(params, ip)
            timestamp = params["timestamp"]

            if timestamp.nil? or \
              timestamp.to_i < Time.now.to_i - 300 or timestamp.to_i > Time.now.to_i
              # p "Timestamp: #{timestamp} should be between #{Time.now.to_i - 300} and #{Time.now.to_i}!"
              return true
            end

            spinner = params["spinner"]
            # p "To be spinned (back-end): #{timestamp}#{ip}robotcatcher"
            hash_spinner = Digest::MD5.hexdigest(timestamp + ip.to_s + "robotcatcher")

            if spinner.nil? or spinner != hash_spinner
              # p "Spinner: #{spinner}" is not equal #{hash_spinner}!"
              return true
            end

            object_name   = self.class.name.downcase
            object_params = params.include?(object_name) && \
              params[object_name].is_a?(Hash) ? params[object_name] : params
            
            to_be_validated = object_params.select {|k,v| @@rc_params.include? k.to_sym}

            if to_be_validated.length != @@rc_params.length
              # p "Keys to be validated: did not find all keys!"
              return true
            end
            
            to_be_validated.each do |k, v|
              hash = Digest::MD5.hexdigest(k.to_s + hash_spinner + "robotcatcher")
              if !params.include? hash or !params[hash].empty?
                # p "Key hash: #{k}'s hash was wrong!
                return true
              end
            end
            
            false
          end
        end
      end
    end
  end
end

ActiveRecord::Base.send :include, RobotCatcher::Models

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
robot_catcher-0.0.1 lib/robot_catcher/models.rb