Sha256: 9329b5323bf9e4e45dd680acf35badbf00eb35c91f114e78f553c1e48f839a49

Contents?: true

Size: 1.84 KB

Versions: 1

Compression:

Stored size: 1.84 KB

Contents

module RobotCatcher
  module Models
    extend ActiveSupport::Concern

    included do 
      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

            # p object_name
            # p object_params
            # p @@rc_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.5 lib/robot_catcher/models.rb